{
  "id": "CODWF06DashboardEvents",
  "name": "COD · WF-06 Dashboard Events",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Dashboard Event Fan-Out\nReceive authenticated dashboard side effects, fetch canonical order/customer data, map the event to the reusable notification contract, and deliver without coupling the UI to providers.",
        "height": 210,
        "width": 1200,
        "color": 2
      },
      "id": "wf06-note",
      "name": "Dashboard Event Gateway",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -700,
        -300
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "dashboard-events",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "wf06-webhook",
      "name": "Dashboard Events Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -640,
        0
      ],
      "webhookId": "cod-dashboard-events-v1",
      "credentials": {
        "httpHeaderAuth": {
          "id": "cred_internal_token",
          "name": "COD Dashboard Event Token"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { accepted: true } }}",
        "options": {
          "responseCode": 202
        }
      },
      "id": "wf06-response",
      "name": "Respond Event Accepted",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        -400,
        -180
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "wf06-status",
                    "leftValue": "={{ $json.body.event }}",
                    "rightValue": "ORDER_STATUS_CHANGED",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Status"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "wf06-resend",
                    "leftValue": "={{ $json.body.event }}",
                    "rightValue": "RESEND_CONFIRMATION",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Resend"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "wf06-address",
                    "leftValue": "={{ $json.body.event }}",
                    "rightValue": "ADDRESS_UPDATED",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Address"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "wf06-stock",
                    "leftValue": "={{ $json.body.event }}",
                    "rightValue": "LOW_STOCK",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Low Stock"
            }
          ]
        },
        "options": {
          "fallbackOutput": "none"
        }
      },
      "id": "wf06-switch",
      "name": "Event Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        -400,
        20
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/orders/{{ $('Dashboard Events Webhook').item.json.body.orderId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 10000
        }
      },
      "id": "wf06-order",
      "name": "Fetch Event Order",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -120,
        20
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "credentials": {
        "httpHeaderAuth": {
          "id": "cred_internal_api",
          "name": "COD Internal API Bearer"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "wf06-event",
              "name": "event",
              "value": "={{ $('Dashboard Events Webhook').item.json.body }}",
              "type": "object"
            },
            {
              "id": "wf06-order-value",
              "name": "order",
              "value": "={{ $json }}",
              "type": "object"
            }
          ]
        },
        "options": {}
      },
      "id": "wf06-context",
      "name": "Merge Dashboard Event & Order",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        120,
        20
      ]
    },
    {
      "parameters": {
        "jsCode": "let __smartCodRun;\n(() => {\n  // n8n/code/dashboard-event.ts\n  function mapDashboardEvent(event, order) {\n    const data = event.data && typeof event.data === \"object\" ? event.data : {};\n    const customer = order.customer && typeof order.customer === \"object\" ? order.customer : {};\n    const base = {\n      audience: \"customer\",\n      channel: \"auto\",\n      language: \"ar\",\n      orderId: String(order.id ?? event.orderId ?? \"\"),\n      customerId: String(order.customerId ?? \"\"),\n      chatId: String(customer.telegramChatId ?? \"\"),\n      email: String(customer.email ?? \"\")\n    };\n    if (event.event === \"RESEND_CONFIRMATION\") {\n      return { ...base, template: \"confirmation\", vars: { orderId: base.orderId, customerName: customer.name, codAmount: order.codAmount, currency: order.currency, confirmationUrl: data.confirmationUrl, telegramLink: data.telegramLink } };\n    }\n    if (event.event === \"ADDRESS_UPDATED\") {\n      return { ...base, template: \"address_confirmation\", vars: { orderId: base.orderId, address: order.addressLine } };\n    }\n    if (event.event === \"LOW_STOCK\") {\n      return { ...base, audience: \"admin\", template: \"low_stock_alert\", vars: { sku: data.sku, available: data.available } };\n    }\n    const status = String(data.to ?? data.status ?? order.status ?? \"\");\n    if (status === \"CONFIRMED\") return { ...base, template: \"receipt\", vars: { orderId: base.orderId } };\n    if (status === \"SHIPPED\" || status === \"DELIVERED\" || status === \"RETURNED\") {\n      return { ...base, template: \"shipping_update\", vars: { awbNumber: order.awbNumber, statusText: data.statusText ?? status, trackingUrl: order.trackingUrl } };\n    }\n    if (status === \"CANCELLED\") return { ...base, template: \"cancellation\", vars: { orderId: base.orderId } };\n    return { ...base, audience: \"admin\", template: \"review_alert\", vars: { orderId: base.orderId, riskScore: order.riskScore } };\n  }\n  function run(items) {\n    return items.map((item) => {\n      const event = item.json.event;\n      const order = item.json.order;\n      return { ...item, json: mapDashboardEvent(event, order) };\n    });\n  }\n\n  // dashboard-event.bundle-entry.ts\n  __smartCodRun = run;\n})();\nreturn __smartCodRun($input.all(), { now: new Date().toISOString() });"
      },
      "id": "wf06-map",
      "name": "Map Event to Notification",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        360,
        20
      ]
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "value": "CODWF00SendNotification",
          "mode": "id"
        },
        "mode": "once",
        "options": {
          "waitForSubWorkflow": true
        }
      },
      "id": "wf06-send",
      "name": "Send Dashboard Event Notification",
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [
        600,
        20
      ]
    }
  ],
  "connections": {
    "Dashboard Events Webhook": {
      "main": [
        [
          {
            "node": "Respond Event Accepted",
            "type": "main",
            "index": 0
          },
          {
            "node": "Event Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Event Switch": {
      "main": [
        [
          {
            "node": "Fetch Event Order",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fetch Event Order",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fetch Event Order",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fetch Event Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Event Order": {
      "main": [
        [
          {
            "node": "Merge Dashboard Event & Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Dashboard Event & Order": {
      "main": [
        [
          {
            "node": "Map Event to Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Event to Notification": {
      "main": [
        [
          {
            "node": "Send Dashboard Event Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": "CODWF99ErrorHandler",
    "timezone": "Africa/Cairo"
  },
  "versionId": "f0600000-0000-4000-8000-000000000000",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}
