{
  "id": "closeouts-monthly-001",
  "name": "Closeouts Sweep \u2014 Monthly (All Suppliers)",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 3 1 * *"
            }
          ]
        }
      },
      "id": "clm-0001",
      "name": "1st of Month 3 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        320
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.API_BASE_URL }}/api/suppliers",
        "options": {}
      },
      "id": "clm-0002",
      "name": "Get Active Suppliers",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        320
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst list = items.flatMap((i) => {\n  const j = i.json;\n  if (Array.isArray(j)) return j;\n  if (j && Array.isArray(j.data)) return j.data;\n  return j && typeof j === 'object' ? [j] : [];\n});\nconst active = list.filter((s) => s && s.is_active && s.adapter_class);\nif (!active.length) throw new Error('No active suppliers with adapter_class set');\nreturn active.map((s) => ({ json: { supplier_id: s.id, supplier_name: s.name } }));"
      },
      "id": "clm-0003",
      "name": "Filter Active",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        320
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.API_BASE_URL }}/api/suppliers/{{$json.supplier_id}}/import",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "mode",
              "value": "closeouts"
            }
          ]
        },
        "options": {}
      },
      "id": "clm-0004",
      "name": "Trigger Closeouts Import",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        320
      ]
    },
    {
      "parameters": {
        "amount": 10,
        "unit": "seconds"
      },
      "id": "clm-0005",
      "name": "Wait 10s",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1120,
        320
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.API_BASE_URL }}/api/sync-jobs/{{$json.sync_job_id}}",
        "options": {}
      },
      "id": "clm-0006",
      "name": "Poll Job Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        320
      ]
    },
    {
      "parameters": {
        "jsCode": "const job = $input.first().json;\nlet supplier_name = job.supplier_name;\nif (!supplier_name) {\n  try { supplier_name = $(\"Filter Active\").item.json.supplier_name; } catch (_) {}\n}\nconst base = { ...job, supplier_name: supplier_name ?? \"unknown\" };\nif (base.status === \"success\" || base.status === \"partial_success\" || base.status === \"failed\") {\n  return [{ json: { ...base, done: true } }];\n}\nreturn [{ json: { ...base, done: false } }];"
      },
      "id": "clm-0007",
      "name": "Check Done",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1560,
        320
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "done-check",
              "leftValue": "={{ $json.done }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equal"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "clm-0008",
      "name": "Is Done?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1780,
        320
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "fail-check",
              "leftValue": "={{ $json.status }}",
              "rightValue": "failed",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "clm-0009",
      "name": "Failed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        2000,
        200
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "text",
              "value": "=:rotating_light: *Monthly Closeouts Sweep FAILED* for supplier *{{$json.supplier_name}}*\nJob: {{$json.id}}\nErrors: {{$json.error_log}}"
            }
          ]
        },
        "options": {}
      },
      "id": "clm-0010",
      "name": "Slack Alert on Failure",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2220,
        100
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "text",
              "value": "=:white_check_mark: *Monthly Closeouts Sweep complete* for supplier *{{$json.supplier_name}}*\nRecords processed: {{$json.records_processed}}"
            }
          ]
        },
        "options": {}
      },
      "id": "clm-0011",
      "name": "Slack Success",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2220,
        400
      ]
    }
  ],
  "connections": {
    "1st of Month 3 AM": {
      "main": [
        [
          {
            "node": "Get Active Suppliers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Active Suppliers": {
      "main": [
        [
          {
            "node": "Filter Active",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Active": {
      "main": [
        [
          {
            "node": "Trigger Closeouts Import",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger Closeouts Import": {
      "main": [
        [
          {
            "node": "Wait 10s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 10s": {
      "main": [
        [
          {
            "node": "Poll Job Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Poll Job Status": {
      "main": [
        [
          {
            "node": "Check Done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Done": {
      "main": [
        [
          {
            "node": "Is Done?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Done?": {
      "main": [
        [
          {
            "node": "Failed?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait 10s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Failed?": {
      "main": [
        [
          {
            "node": "Slack Alert on Failure",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "sync"
    },
    {
      "name": "closeouts"
    }
  ]
}