{
  "id": "CODWF01OrderIngestion",
  "name": "COD · WF-01 Order Ingestion",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Layer 1 · Order Ingestion\nAuthenticate and normalize the storefront payload, enrich phone and geography, score fraud risk, select a carrier manifest, reserve stock atomically through the internal API, respond to the store, then dispatch customer/admin notifications.",
        "height": 240,
        "width": 2120,
        "color": 5
      },
      "id": "wf01-note-layer",
      "name": "Layer 1 · Order Ingestion",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-1260, -420]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "orders",
        "authentication": "none",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "wf01-webhook",
      "name": "Store Order Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [-1200, 0],
      "webhookId": "cod-orders-public-v1"
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
          "conditions": [{ "id": "wf01-auth-key", "leftValue": "={{ $json.headers['x-api-key'] || '' }}", "rightValue": "={{ $env.ORDER_WEBHOOK_KEY }}", "operator": { "type": "string", "operation": "equals" } }],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "wf01-auth",
      "name": "Authenticate Store Webhook Key",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [-1100, 0]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { error: 'UNAUTHORIZED' } }}",
        "options": { "responseCode": 401 }
      },
      "id": "wf01-auth-response",
      "name": "Respond Unauthorized",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [-900, 180]
    },
    {
      "parameters": { "jsCode": "@@code:sanitize" },
      "id": "wf01-sanitize",
      "name": "Sanitize Phone & Input Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [-980, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
          "conditions": [
            { "id": "wf01-required-name", "leftValue": "={{ $json.customer.name }}", "rightValue": "", "operator": { "type": "string", "operation": "notEmpty", "singleValue": true } },
            { "id": "wf01-required-phone", "leftValue": "={{ $json.customer.phone }}", "rightValue": "", "operator": { "type": "string", "operation": "notEmpty", "singleValue": true } },
            { "id": "wf01-required-address", "leftValue": "={{ $json.shipping.address }}", "rightValue": "", "operator": { "type": "string", "operation": "notEmpty", "singleValue": true } },
            { "id": "wf01-required-items", "leftValue": "={{ $json.items.length }}", "rightValue": 0, "operator": { "type": "number", "operation": "gt" } }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "wf01-required",
      "name": "Required Order Fields Present?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [-760, 0]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { error: 'VALIDATION_ERROR', details: 'customer.name, customer.phone, shipping.address and at least one item are required' } }}",
        "options": { "responseCode": 422 }
      },
      "id": "wf01-validation-response",
      "name": "Respond Validation Error",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [-540, 180]
    },
    {
      "parameters": { "jsCode": "@@code:phone" },
      "id": "wf01-phone",
      "name": "Validate Phone Number E.164 Structure",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [-540, -40]
    },
    {
      "parameters": { "jsCode": "@@code:geo" },
      "id": "wf01-geo",
      "name": "Parse Governorate & City Geo-Location",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [-300, -40]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "wf01-address-line", "name": "addressLine", "value": "={{ $json.shipping.address }}", "type": "string" },
            { "id": "wf01-city", "name": "city", "value": "={{ $json.geo.city || $json.shipping.city || '' }}", "type": "string" },
            { "id": "wf01-governorate", "name": "governorate", "value": "={{ $json.geo.governorate?.ar || $json.shipping.governorate || '' }}", "type": "string" },
            { "id": "wf01-country", "name": "country", "value": "={{ $json.geo.country }}", "type": "string" },
            { "id": "wf01-zone", "name": "zone", "value": "={{ $json.geo.zone }}", "type": "string" },
            { "id": "wf01-normalized-address", "name": "addressNormalized", "value": "={{ [$json.shipping.address, $json.geo.city, $json.geo.governorate?.ar].filter(Boolean).join(' ') }}", "type": "string" }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "wf01-address",
      "name": "Enrich Address Details",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [-60, -40]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "wf01-operator", "name": "telecomOperator", "value": "={{ $json.customer.telecomOperator || 'Unknown' }}", "type": "string" }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "wf01-operator-node",
      "name": "Detect Telecom Operator",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [180, -40]
    },
    {
      "parameters": {
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/settings",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": { "timeout": 10000 }
      },
      "id": "wf01-settings",
      "name": "Fetch Store Settings",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [420, -40],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": { "id": "cred_internal_api", "name": "COD Internal API Bearer" }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/risk-context",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ phoneE164: $('Detect Telecom Operator').item.json.customer.phoneE164, addressNormalized: $('Detect Telecom Operator').item.json.addressNormalized, name: $('Detect Telecom Operator').item.json.customer.name, ...($('Detect Telecom Operator').item.json.customer.email ? { email: $('Detect Telecom Operator').item.json.customer.email } : {}) }) }}",
        "options": { "timeout": 10000 }
      },
      "id": "wf01-risk-context",
      "name": "Fetch Risk Context (Dup, Blacklist, History)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [660, -40],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": { "id": "cred_internal_api", "name": "COD Internal API Bearer" }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "wf01-merge-customer", "name": "customer", "value": "={{ $('Detect Telecom Operator').item.json.customer }}", "type": "object" },
            { "id": "wf01-merge-shipping", "name": "shipping", "value": "={{ $('Detect Telecom Operator').item.json.shipping }}", "type": "object" },
            { "id": "wf01-merge-items", "name": "items", "value": "={{ $('Detect Telecom Operator').item.json.items }}", "type": "array" },
            { "id": "wf01-merge-original", "name": "orderInput", "value": "={{ $('Detect Telecom Operator').item.json }}", "type": "object" },
            { "id": "wf01-merge-geo", "name": "geo", "value": "={{ $('Detect Telecom Operator').item.json.geo }}", "type": "object" },
            { "id": "wf01-merge-settings", "name": "settings", "value": "={{ $('Fetch Store Settings').item.json }}", "type": "object" },
            { "id": "wf01-merge-risk-context", "name": "riskContext", "value": "={{ $json }}", "type": "object" },
            { "id": "wf01-merge-phone-valid", "name": "phoneValid", "value": "={{ $('Detect Telecom Operator').item.json.phoneValid }}", "type": "boolean" },
            { "id": "wf01-merge-cod", "name": "codAmount", "value": "={{ $('Detect Telecom Operator').item.json.codAmount }}", "type": "number" }
          ]
        },
        "options": {}
      },
      "id": "wf01-context-merge",
      "name": "Merge Order, Settings & Risk Context",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [900, -40]
    },
    {
      "parameters": { "jsCode": "@@code:risk-score" },
      "id": "wf01-risk-score",
      "name": "Dynamic Risk Scoring Engine (0-100)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1140, -40]
    },
    {
      "parameters": { "jsCode": "@@code:carrier-route" },
      "id": "wf01-carrier-route",
      "name": "Multi-Carrier Destination Router",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1380, -40]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [{ "id": "wf01-route-bosta", "leftValue": "={{ $json.route.carrier }}", "rightValue": "BOSTA", "operator": { "type": "string", "operation": "equals" } }], "combinator": "and" }, "renameOutput": true, "outputKey": "Bosta" },
            { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [{ "id": "wf01-route-aramex", "leftValue": "={{ $json.route.carrier }}", "rightValue": "ARAMEX", "operator": { "type": "string", "operation": "equals" } }], "combinator": "and" }, "renameOutput": true, "outputKey": "Aramex" },
            { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [{ "id": "wf01-route-dhl", "leftValue": "={{ $json.route.carrier }}", "rightValue": "DHL", "operator": { "type": "string", "operation": "equals" } }], "combinator": "and" }, "renameOutput": true, "outputKey": "DHL" }
          ]
        },
        "options": { "fallbackOutput": "none" }
      },
      "id": "wf01-carrier-switch",
      "name": "Carrier Assignment Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [1620, -40]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-bosta-carrier", "name": "carrier", "value": "BOSTA", "type": "string" },
        { "id": "wf01-bosta-service", "name": "carrierService", "value": "={{ $json.route.service }}", "type": "string" },
        { "id": "wf01-bosta-address", "name": "addressLine", "value": "={{ $json.orderInput.shipping.address }}", "type": "string" },
        { "id": "wf01-bosta-city", "name": "city", "value": "={{ $json.geo.city || $json.orderInput.shipping.city }}", "type": "string" },
        { "id": "wf01-bosta-governorate", "name": "governorate", "value": "={{ $json.geo.governorate?.ar || $json.orderInput.shipping.governorate }}", "type": "string" },
        { "id": "wf01-bosta-country", "name": "country", "value": "={{ $json.geo.country }}", "type": "string" },
        { "id": "wf01-bosta-currency", "name": "currency", "value": "={{ $json.orderInput.currency }}", "type": "string" }
      ] }, "includeOtherFields": true, "options": {} },
      "id": "wf01-bosta-assign",
      "name": "Assign Carrier A (Bosta Express)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1880, -200]
    },
    {
      "parameters": { "jsCode": "@@code:manifest" },
      "id": "wf01-bosta-manifest",
      "name": "Format Bosta Packaging Manifest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [2140, -200]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-aramex-carrier", "name": "carrier", "value": "ARAMEX", "type": "string" },
        { "id": "wf01-aramex-service", "name": "carrierService", "value": "={{ $json.route.service }}", "type": "string" },
        { "id": "wf01-aramex-address", "name": "addressLine", "value": "={{ $json.orderInput.shipping.address }}", "type": "string" },
        { "id": "wf01-aramex-city", "name": "city", "value": "={{ $json.geo.city || $json.orderInput.shipping.city }}", "type": "string" },
        { "id": "wf01-aramex-governorate", "name": "governorate", "value": "={{ $json.geo.governorate?.ar || $json.orderInput.shipping.governorate }}", "type": "string" },
        { "id": "wf01-aramex-country", "name": "country", "value": "={{ $json.geo.country }}", "type": "string" },
        { "id": "wf01-aramex-currency", "name": "currency", "value": "={{ $json.orderInput.currency }}", "type": "string" }
      ] }, "includeOtherFields": true, "options": {} },
      "id": "wf01-aramex-assign",
      "name": "Assign Carrier B (Aramex Regional)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1880, -40]
    },
    {
      "parameters": { "jsCode": "@@code:manifest" },
      "id": "wf01-aramex-manifest",
      "name": "Format Aramex Packaging Manifest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [2140, -40]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-dhl-carrier", "name": "carrier", "value": "DHL", "type": "string" },
        { "id": "wf01-dhl-service", "name": "carrierService", "value": "={{ $json.route.service }}", "type": "string" },
        { "id": "wf01-dhl-address", "name": "addressLine", "value": "={{ $json.orderInput.shipping.address }}", "type": "string" },
        { "id": "wf01-dhl-city", "name": "city", "value": "={{ $json.geo.city || $json.orderInput.shipping.city }}", "type": "string" },
        { "id": "wf01-dhl-governorate", "name": "governorate", "value": "={{ $json.geo.governorate?.ar || $json.orderInput.shipping.governorate || $json.geo.city }}", "type": "string" },
        { "id": "wf01-dhl-country", "name": "country", "value": "={{ $json.geo.country }}", "type": "string" },
        { "id": "wf01-dhl-currency", "name": "currency", "value": "={{ $json.orderInput.currency }}", "type": "string" }
      ] }, "includeOtherFields": true, "options": {} },
      "id": "wf01-dhl-assign",
      "name": "Assign Carrier C (DHL Gulf or Intl)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1880, 120]
    },
    {
      "parameters": { "jsCode": "@@code:manifest" },
      "id": "wf01-dhl-manifest",
      "name": "Format DHL Customs Declaration",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [2140, 120]
    },
    {
      "parameters": {
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/products/stock",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "skus", "value": "={{ $json.items.map(item => item.sku).join(',') }}" }] },
        "options": { "timeout": 10000 }
      },
      "id": "wf01-stock",
      "name": "Check Product Stock Availability",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [2400, -40],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": { "id": "cred_internal_api", "name": "COD Internal API Bearer" }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/orders",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ (() => { const data = $('Multi-Carrier Destination Router').item.json; const input = data.orderInput; return JSON.stringify({ externalId: input.externalId, source: input.source, customer: { name: data.customer.name, phoneE164: data.customer.phoneE164, email: data.customer.email, country: data.geo.country, telecomOperator: data.customer.telecomOperator || undefined }, addressRaw: input.shipping.address, addressLine: input.shipping.address, city: data.geo.city || input.shipping.city || undefined, governorate: data.geo.governorate?.ar || input.shipping.governorate || undefined, country: data.geo.country, zone: data.geo.zone, carrier: data.route.carrier, carrierService: data.route.service, initialStatus: data.risk.initialStatus, riskScore: data.risk.score, riskLevel: data.risk.level, riskReasons: data.risk.reasons, items: input.items, shippingFee: input.shippingFee, discount: input.discount, currency: input.currency, notes: input.notes }); })() }}",
        "options": { "timeout": 10000 }
      },
      "id": "wf01-create-order",
      "name": "Create Order & Reserve Inventory",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [2640, -40],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": { "id": "cred_internal_api", "name": "COD Internal API Bearer" }
      }
    },
    {
      "parameters": { "jsCode": "@@code:manifest" },
      "id": "wf01-final-manifest",
      "name": "Preserve Carrier Manifest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [2880, -40]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { orderId: $json.order.id, status: $json.order.status, riskScore: $json.order.riskScore, riskLevel: $json.order.riskLevel, carrier: $json.order.carrier, zone: $json.order.zone, confirmationUrl: $json.confirmationUrl, telegramLink: $json.telegramLink, manifest: $json.manifest } }}",
        "options": { "responseCode": 200 }
      },
      "id": "wf01-response",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [3120, -220]
    },
    {
      "parameters": {
        "rules": { "values": [
          { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [{ "id": "wf01-risk-high", "leftValue": "={{ $json.order.status }}", "rightValue": "BLOCKED", "operator": { "type": "string", "operation": "equals" } }], "combinator": "and" }, "renameOutput": true, "outputKey": "HIGH" },
          { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [{ "id": "wf01-risk-medium", "leftValue": "={{ $json.order.status }}", "rightValue": "NEEDS_REVIEW", "operator": { "type": "string", "operation": "equals" } }], "combinator": "and" }, "renameOutput": true, "outputKey": "MEDIUM" },
          { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [{ "id": "wf01-risk-low", "leftValue": "={{ $json.order.status }}", "rightValue": "PENDING_CONFIRMATION", "operator": { "type": "string", "operation": "equals" } }], "combinator": "and" }, "renameOutput": true, "outputKey": "LOW" }
        ] },
        "options": { "fallbackOutput": "none" }
      },
      "id": "wf01-risk-switch",
      "name": "Risk & Fraud Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [3120, 20]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-fraud-audience", "name": "audience", "value": "admin", "type": "string" },
        { "id": "wf01-fraud-channel", "name": "channel", "value": "auto", "type": "string" },
        { "id": "wf01-fraud-template", "name": "template", "value": "fraud_alert", "type": "string" },
        { "id": "wf01-fraud-language", "name": "language", "value": "ar", "type": "string" },
        { "id": "wf01-fraud-vars", "name": "vars", "value": "={{ { orderId: $json.order.id, riskScore: $json.order.riskScore, riskReasons: ($json.order.riskReasons || []).map(reason => reason.labelAr || reason.code).join('، ') } }}", "type": "object" }
      ] }, "options": {} },
      "id": "wf01-fraud-payload",
      "name": "Prepare High-Risk Fraud Alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [3380, -40]
    },
    {
      "parameters": { "workflowId": { "__rl": true, "value": "CODWF00SendNotification", "mode": "id" }, "mode": "once", "options": { "waitForSubWorkflow": true } },
      "id": "wf01-fraud-alert",
      "name": "Send High-Risk Fraud Alert",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [3620, -40]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-review-audience", "name": "audience", "value": "admin", "type": "string" },
        { "id": "wf01-review-channel", "name": "channel", "value": "auto", "type": "string" },
        { "id": "wf01-review-template", "name": "template", "value": "review_alert", "type": "string" },
        { "id": "wf01-review-language", "name": "language", "value": "ar", "type": "string" },
        { "id": "wf01-review-vars", "name": "vars", "value": "={{ { orderId: $json.order.id, riskScore: $json.order.riskScore } }}", "type": "object" }
      ] }, "options": {} },
      "id": "wf01-review-payload",
      "name": "Prepare Human Review Alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [3380, 100]
    },
    {
      "parameters": { "workflowId": { "__rl": true, "value": "CODWF00SendNotification", "mode": "id" }, "mode": "once", "options": { "waitForSubWorkflow": true } },
      "id": "wf01-review-alert",
      "name": "Send Review Alert",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [3620, 100]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-confirm-audience", "name": "audience", "value": "customer", "type": "string" },
        { "id": "wf01-confirm-channel", "name": "channel", "value": "auto", "type": "string" },
        { "id": "wf01-confirm-template", "name": "template", "value": "confirmation", "type": "string" },
        { "id": "wf01-confirm-language", "name": "language", "value": "ar", "type": "string" },
        { "id": "wf01-confirm-order", "name": "orderId", "value": "={{ $json.order.id }}", "type": "string" },
        { "id": "wf01-confirm-email", "name": "email", "value": "={{ $json.order.customer.email || '' }}", "type": "string" },
        { "id": "wf01-confirm-chat", "name": "chatId", "value": "={{ $json.order.customer.telegramChatId || '' }}", "type": "string" },
        { "id": "wf01-confirm-vars", "name": "vars", "value": "={{ { orderId: $json.order.id, customerName: $json.order.customer.name, codAmount: $json.order.codAmount, currency: $json.order.currency, confirmationUrl: $json.confirmationUrl, telegramLink: $json.telegramLink } }}", "type": "object" },
        { "id": "wf01-confirm-buttons", "name": "buttons", "value": "={{ [{ text: '✅ تأكيد', callbackData: 'act:confirm:' + $json.order.id }, { text: '❌ إلغاء', callbackData: 'act:cancel:' + $json.order.id }] }}", "type": "array" }
      ] }, "options": {} },
      "id": "wf01-confirm-payload",
      "name": "Prepare Customer Confirmation",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [3380, 240]
    },
    {
      "parameters": { "workflowId": { "__rl": true, "value": "CODWF00SendNotification", "mode": "id" }, "mode": "once", "options": { "waitForSubWorkflow": true } },
      "id": "wf01-confirm",
      "name": "Send Customer Confirmation",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [3620, 240]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/orders/{{ $('Preserve Carrier Manifest').item.json.order.id }}/contact-attempt",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ actor: 'n8n' }) }}",
        "options": { "timeout": 10000 }
      },
      "id": "wf01-contact-attempt",
      "name": "Log Contact Attempt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [3860, 240],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": { "id": "cred_internal_api", "name": "COD Internal API Bearer" }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
          "conditions": [{ "id": "wf01-low-stock-count", "leftValue": "={{ $('Preserve Carrier Manifest').item.json.lowStock.length }}", "rightValue": 0, "operator": { "type": "number", "operation": "gt" } }],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "wf01-low-stock-switch",
      "name": "Low-Stock Switch",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [4100, 100]
    },
    {
      "parameters": { "assignments": { "assignments": [
        { "id": "wf01-stock-audience", "name": "audience", "value": "admin", "type": "string" },
        { "id": "wf01-stock-channel", "name": "channel", "value": "auto", "type": "string" },
        { "id": "wf01-stock-template", "name": "template", "value": "low_stock_alert", "type": "string" },
        { "id": "wf01-stock-language", "name": "language", "value": "ar", "type": "string" },
        { "id": "wf01-stock-vars", "name": "vars", "value": "={{ { sku: $('Preserve Carrier Manifest').item.json.lowStock[0].sku, available: $('Preserve Carrier Manifest').item.json.lowStock[0].onHand - $('Preserve Carrier Manifest').item.json.lowStock[0].reserved } }}", "type": "object" }
      ] }, "options": {} },
      "id": "wf01-low-stock-payload",
      "name": "Prepare Low-Stock Restock Alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [4340, 40]
    },
    {
      "parameters": { "workflowId": { "__rl": true, "value": "CODWF00SendNotification", "mode": "id" }, "mode": "once", "options": { "waitForSubWorkflow": true } },
      "id": "wf01-low-stock-alert",
      "name": "Push Low-Stock Restock Alert",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [4580, 40]
    }
  ],
  "connections": {
    "Store Order Webhook": { "main": [[{ "node": "Authenticate Store Webhook Key", "type": "main", "index": 0 }]] },
    "Authenticate Store Webhook Key": { "main": [[{ "node": "Sanitize Phone & Input Data", "type": "main", "index": 0 }], [{ "node": "Respond Unauthorized", "type": "main", "index": 0 }]] },
    "Sanitize Phone & Input Data": { "main": [[{ "node": "Required Order Fields Present?", "type": "main", "index": 0 }]] },
    "Required Order Fields Present?": { "main": [[{ "node": "Validate Phone Number E.164 Structure", "type": "main", "index": 0 }], [{ "node": "Respond Validation Error", "type": "main", "index": 0 }]] },
    "Validate Phone Number E.164 Structure": { "main": [[{ "node": "Parse Governorate & City Geo-Location", "type": "main", "index": 0 }]] },
    "Parse Governorate & City Geo-Location": { "main": [[{ "node": "Enrich Address Details", "type": "main", "index": 0 }]] },
    "Enrich Address Details": { "main": [[{ "node": "Detect Telecom Operator", "type": "main", "index": 0 }]] },
    "Detect Telecom Operator": { "main": [[{ "node": "Fetch Store Settings", "type": "main", "index": 0 }]] },
    "Fetch Store Settings": { "main": [[{ "node": "Fetch Risk Context (Dup, Blacklist, History)", "type": "main", "index": 0 }]] },
    "Fetch Risk Context (Dup, Blacklist, History)": { "main": [[{ "node": "Merge Order, Settings & Risk Context", "type": "main", "index": 0 }]] },
    "Merge Order, Settings & Risk Context": { "main": [[{ "node": "Dynamic Risk Scoring Engine (0-100)", "type": "main", "index": 0 }]] },
    "Dynamic Risk Scoring Engine (0-100)": { "main": [[{ "node": "Multi-Carrier Destination Router", "type": "main", "index": 0 }]] },
    "Multi-Carrier Destination Router": { "main": [[{ "node": "Carrier Assignment Switch", "type": "main", "index": 0 }]] },
    "Carrier Assignment Switch": { "main": [
      [{ "node": "Assign Carrier A (Bosta Express)", "type": "main", "index": 0 }],
      [{ "node": "Assign Carrier B (Aramex Regional)", "type": "main", "index": 0 }],
      [{ "node": "Assign Carrier C (DHL Gulf or Intl)", "type": "main", "index": 0 }]
    ] },
    "Assign Carrier A (Bosta Express)": { "main": [[{ "node": "Format Bosta Packaging Manifest", "type": "main", "index": 0 }]] },
    "Format Bosta Packaging Manifest": { "main": [[{ "node": "Check Product Stock Availability", "type": "main", "index": 0 }]] },
    "Assign Carrier B (Aramex Regional)": { "main": [[{ "node": "Format Aramex Packaging Manifest", "type": "main", "index": 0 }]] },
    "Format Aramex Packaging Manifest": { "main": [[{ "node": "Check Product Stock Availability", "type": "main", "index": 0 }]] },
    "Assign Carrier C (DHL Gulf or Intl)": { "main": [[{ "node": "Format DHL Customs Declaration", "type": "main", "index": 0 }]] },
    "Format DHL Customs Declaration": { "main": [[{ "node": "Check Product Stock Availability", "type": "main", "index": 0 }]] },
    "Check Product Stock Availability": { "main": [[{ "node": "Create Order & Reserve Inventory", "type": "main", "index": 0 }]] },
    "Create Order & Reserve Inventory": { "main": [[{ "node": "Preserve Carrier Manifest", "type": "main", "index": 0 }]] },
    "Preserve Carrier Manifest": { "main": [[{ "node": "Respond to Webhook", "type": "main", "index": 0 }, { "node": "Risk & Fraud Switch", "type": "main", "index": 0 }, { "node": "Low-Stock Switch", "type": "main", "index": 0 }]] },
    "Risk & Fraud Switch": { "main": [
      [{ "node": "Prepare High-Risk Fraud Alert", "type": "main", "index": 0 }],
      [{ "node": "Prepare Human Review Alert", "type": "main", "index": 0 }],
      [{ "node": "Prepare Customer Confirmation", "type": "main", "index": 0 }]
    ] },
    "Prepare High-Risk Fraud Alert": { "main": [[{ "node": "Send High-Risk Fraud Alert", "type": "main", "index": 0 }]] },
    "Send High-Risk Fraud Alert": { "main": [[]] },
    "Prepare Human Review Alert": { "main": [[{ "node": "Send Review Alert", "type": "main", "index": 0 }]] },
    "Send Review Alert": { "main": [[]] },
    "Prepare Customer Confirmation": { "main": [[{ "node": "Send Customer Confirmation", "type": "main", "index": 0 }]] },
    "Send Customer Confirmation": { "main": [[{ "node": "Log Contact Attempt", "type": "main", "index": 0 }]] },
    "Log Contact Attempt": { "main": [[]] },
    "Low-Stock Switch": { "main": [[{ "node": "Prepare Low-Stock Restock Alert", "type": "main", "index": 0 }], []] },
    "Prepare Low-Stock Restock Alert": { "main": [[{ "node": "Push Low-Stock Restock Alert", "type": "main", "index": 0 }]] }
  },
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": "CODWF99ErrorHandler",
    "timezone": "Africa/Cairo"
  },
  "versionId": "f0100000-0000-4000-8000-000000000000",
  "meta": { "templateCredsSetupCompleted": true },
  "tags": []
}
