{
  "id": "CODWF02ShippingTransit",
  "name": "COD · WF-02 Shipping Transit",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Layer 2 · Shipping Transit\nAuthenticate carrier updates, persist tracking and the canonical order transition through the internal API, then notify the linked customer.",
        "height": 210,
        "width": 1300,
        "color": 6
      },
      "id": "wf02-note",
      "name": "Layer 2 · Shipping Transit",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -760,
        -300
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "shipping-update",
        "authentication": "none",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "wf02-webhook",
      "name": "Mock Carrier Shipping Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -700,
        0
      ],
      "webhookId": "cod-shipping-update-v1"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "wf02-auth",
              "leftValue": "={{ $json.headers['x-api-key'] || '' }}",
              "rightValue": "={{ $env.ORDER_WEBHOOK_KEY }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "wf02-auth-node",
      "name": "Authenticate Carrier Webhook",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -480,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { error: 'UNAUTHORIZED' } }}",
        "options": {
          "responseCode": 401
        }
      },
      "id": "wf02-unauthorized",
      "name": "Respond Carrier Unauthorized",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        -260,
        180
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "wf02-awb",
              "name": "awbNumber",
              "value": "={{ $json.body.awbNumber }}",
              "type": "string"
            },
            {
              "id": "wf02-status",
              "name": "status",
              "value": "={{ $json.body.status }}",
              "type": "string"
            },
            {
              "id": "wf02-location",
              "name": "location",
              "value": "={{ $json.body.location || '' }}",
              "type": "string"
            },
            {
              "id": "wf02-time",
              "name": "occurredAt",
              "value": "={{ $json.body.occurredAt || $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "wf02-parse",
      "name": "Parse Shipping Transit Payload",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -240,
        -40
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/shipments/{{ $json.awbNumber }}/tracking",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ status: $json.status, location: $json.location || undefined, occurredAt: $json.occurredAt, raw: { source: 'mock-carrier' } }) }}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "wf02-tracking",
      "name": "Update Tracking",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        0,
        -40
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": {
          "id": "cred_internal_api",
          "name": "COD Internal API Bearer"
        }
      }
    },
    {
      "parameters": {
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/orders/{{ $json.order.id }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 10000
        }
      },
      "id": "wf02-order",
      "name": "Fetch Updated Shipment Order",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        -40
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": {
          "id": "cred_internal_api",
          "name": "COD Internal API Bearer"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { orderId: $json.id, status: $json.status, awbNumber: $json.awbNumber } }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "wf02-response",
      "name": "Respond Shipping Update",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        480,
        -180
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "wf02-notify-audience",
              "name": "audience",
              "value": "customer",
              "type": "string"
            },
            {
              "id": "wf02-notify-channel",
              "name": "channel",
              "value": "auto",
              "type": "string"
            },
            {
              "id": "wf02-notify-template",
              "name": "template",
              "value": "shipping_update",
              "type": "string"
            },
            {
              "id": "wf02-notify-language",
              "name": "language",
              "value": "ar",
              "type": "string"
            },
            {
              "id": "wf02-notify-order",
              "name": "orderId",
              "value": "={{ $json.id }}",
              "type": "string"
            },
            {
              "id": "wf02-notify-customer",
              "name": "customerId",
              "value": "={{ $json.customer.id }}",
              "type": "string"
            },
            {
              "id": "wf02-notify-chat",
              "name": "chatId",
              "value": "={{ $json.customer.telegramChatId || '' }}",
              "type": "string"
            },
            {
              "id": "wf02-notify-email",
              "name": "email",
              "value": "={{ $json.customer.email || '' }}",
              "type": "string"
            },
            {
              "id": "wf02-notify-vars",
              "name": "vars",
              "value": "={{ { awbNumber: $json.awbNumber, statusText: $json.status === 'SHIPPED' ? '✅ تم شحن طلبك ' + $json.id : ($json.status === 'DELIVERED' ? '✅ تم تسليم طلبك ' + $json.id : '↩️ تم تسجيل مرتجع للطلب ' + $json.id), trackingUrl: $json.trackingUrl } }}",
              "type": "object"
            }
          ]
        },
        "options": {}
      },
      "id": "wf02-notify",
      "name": "Push Live Shipping Update to Customer",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        480,
        0
      ]
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "value": "CODWF00SendNotification",
          "mode": "id"
        },
        "mode": "once",
        "options": {
          "waitForSubWorkflow": true
        }
      },
      "id": "wf02-send",
      "name": "Send Shipping Update",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [
        720,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ orderId: $('Fetch Updated Shipment Order').item.json.id, type: 'SHIPPING_NOTIFICATION_SENT', actor: 'n8n', message: 'Customer shipping update sent', meta: { status: $('Fetch Updated Shipment Order').item.json.status, awbNumber: $('Fetch Updated Shipment Order').item.json.awbNumber } }) }}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "wf02-log",
      "name": "Log Shipping Notification Event",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        960,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": {
          "id": "cred_internal_api",
          "name": "COD Internal API Bearer"
        }
      }
    }
  ],
  "connections": {
    "Mock Carrier Shipping Webhook": {
      "main": [
        [
          {
            "node": "Authenticate Carrier Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Authenticate Carrier Webhook": {
      "main": [
        [
          {
            "node": "Parse Shipping Transit Payload",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Carrier Unauthorized",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Shipping Transit Payload": {
      "main": [
        [
          {
            "node": "Update Tracking",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Tracking": {
      "main": [
        [
          {
            "node": "Fetch Updated Shipment Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Updated Shipment Order": {
      "main": [
        [
          {
            "node": "Respond Shipping Update",
            "type": "main",
            "index": 0
          },
          {
            "node": "Push Live Shipping Update to Customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Push Live Shipping Update to Customer": {
      "main": [
        [
          {
            "node": "Send Shipping Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Shipping Update": {
      "main": [
        [
          {
            "node": "Log Shipping Notification Event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": "CODWF99ErrorHandler",
    "timezone": "Africa/Cairo"
  },
  "versionId": "f0200000-0000-4000-8000-000000000000",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}
