{
  "name": "Atlas Labs: bridge accepted sales orders to work orders",
  "flow": [
    {
      "id": 1,
      "module": "gateway:CustomWebhook",
      "version": 1,
      "parameters": {},
      "mapper": {},
      "metadata": {
        "designer": { "x": 0, "y": 0, "name": "Catch office.sales_order.accepted" },
        "restore": {},
        "notes": [
          "Create this webhook after import (Make can't set the URL from a blueprint). Subscribe it in Atlas Labs with events: [\"office.sales_order.accepted\"].",
          "The payload's webhook-id/webhook-timestamp/webhook-signature headers should be checked against the whsec_... subscription secret before trusting the body — Make's Custom Webhook module doesn't verify Standard Webhooks signatures itself; add a 'Verify signature' step (e.g. a Make HTTP/Tools function computing HMAC-SHA256, or an external Function/App) if this scenario is exposed publicly."
        ]
      }
    },
    {
      "id": 2,
      "module": "http:ActionSendData",
      "version": 3,
      "parameters": { "handleErrors": false, "useNewZLibDeCompress": true },
      "mapper": {
        "url": "{{ifempty($env.ATLAS_LABS_BASE_URL; \"https://automationatlas.io/labs/api/v1\")}}/office/sales-orders",
        "serializeUrl": false,
        "method": "GET",
        "headers": [{ "name": "Authorization", "value": "Bearer {{$env.ATLAS_LABS_OFFICE_KEY}}" }],
        "qs": [{ "name": "limit", "value": "100" }],
        "bodyType": "empty",
        "parseResponse": true,
        "authUser": "",
        "authPass": "",
        "timeout": "",
        "shareCookies": false
      },
      "metadata": {
        "designer": { "x": 300, "y": 0, "name": "Get sales orders (API gap: no GET by id — see README)" },
        "restore": {}
      }
    },
    {
      "id": 3,
      "module": "builtin:BasicFeeder",
      "version": 1,
      "parameters": {},
      "mapper": { "array": "{{2.data.items}}" },
      "metadata": {
        "designer": { "x": 600, "y": 0, "name": "Iterate sales orders (list endpoints wrap rows as data.items)" },
        "restore": {}
      }
    },
    {
      "id": 4,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "mapper": {},
      "routes": [
        {
          "flow": [
            {
              "id": 5,
              "module": "http:ActionSendData",
              "version": 3,
              "parameters": { "handleErrors": false, "useNewZLibDeCompress": true },
              "mapper": {
                "url": "{{ifempty($env.ATLAS_LABS_BASE_URL; \"https://automationatlas.io/labs/api/v1\")}}/factory/work-orders",
                "serializeUrl": false,
                "method": "POST",
                "headers": [
                  { "name": "Authorization", "value": "Bearer {{$env.ATLAS_LABS_FACTORY_KEY}}" },
                  { "name": "Idempotency-Key", "value": "{{1.data.id}}" },
                  { "name": "Content-Type", "value": "application/json" }
                ],
                "qs": [],
                "bodyType": "raw",
                "contentType": "application/json",
                "data": "{\"sales_order_id\":\"{{3.id}}\",\"quantity\":{{3.quantity}},\"due_date\":{{3.due_sim_day}}}",
                "parseResponse": true,
                "timeout": ""
              },
              "metadata": {
                "designer": { "x": 900, "y": -150, "name": "POST /factory/work-orders" },
                "restore": {}
              }
            },
            {
              "id": 6,
              "module": "builtin:BasicRouter",
              "version": 1,
              "parameters": {},
              "mapper": {},
              "routes": [
                {
                  "flow": [],
                  "filter": {
                    "name": "Bridged (created, or already bridged)",
                    "conditions": [
                      [{ "a": "{{5.statusCode}}", "b": "201", "o": "number:equal" }],
                      [
                        { "a": "{{5.statusCode}}", "b": "409", "o": "number:equal" },
                        { "a": "{{5.data.error.code}}", "b": "already_bridged", "o": "text:equal" }
                      ]
                    ]
                  },
                  "metadata": { "designer": { "x": 1200, "y": -250, "name": "Bridged (success)" } }
                },
                {
                  "flow": [],
                  "metadata": {
                    "designer": { "x": 1200, "y": -50, "name": "Bridge failed (fallback)" },
                    "notes": [
                      "Nothing matched: neither 201 nor 409 already_bridged. Attach a Make error handler (right-click this module → Add error handler) or a notification module here in the real scenario."
                    ]
                  }
                }
              ]
            }
          ],
          "filter": {
            "name": "This is the accepted order",
            "conditions": [[{ "a": "{{3.id}}", "b": "{{1.data.sales_order_id}}", "o": "text:equal" }]]
          },
          "metadata": { "designer": { "x": 900, "y": -150 } }
        },
        {
          "flow": [],
          "metadata": {
            "designer": { "x": 900, "y": 150, "name": "Not the accepted order (skip)" }
          }
        }
      ],
      "metadata": {
        "designer": { "x": 900, "y": 0, "name": "Router: find the accepted order" },
        "restore": {}
      }
    }
  ],
  "metadata": {
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false,
      "confidential": false,
      "dataloss": false,
      "dlq": false,
      "freshVariables": false
    },
    "designer": { "orphans": [] },
    "zone": "eu2.make.com",
    "notes": [
      "$env.* placeholders stand for Make's 'Set variable'/connection values — Make has no literal $env syntax; wire ATLAS_LABS_BASE_URL / ATLAS_LABS_OFFICE_KEY / ATLAS_LABS_FACTORY_KEY as scenario variables or an HTTP/Basic auth connection when you build this in the Make UI. See templates/make/README.md."
    ]
  }
}
