{
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "name": "ezGPT - Reusable Order Check",
  "nodes": [
    {
      "id": "db277393-f4ee-4a93-b024-4fa3f9f3c859",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.2,
      "parameters": {
        "inputSource": "passthrough"
      },
      "position": [
        0,
        0
      ]
    },
    {
      "id": "6638a5b9-99b3-408f-8ed3-8d80a8af5132",
      "name": "Validate Orders",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const items = $input.all();\nconst clean = value => typeof value === 'string'\n  ? value.trim().replace(/\\s+/g, ' ') : '';\nconst ids = items.map(item => clean(item.json.order_id));\nconst counts = new Map();\nfor (const id of ids) counts.set(id, (counts.get(id) ?? 0) + 1);\n\nreturn items.map((item, index) => {\n  const row = item.json;\n  const order_id = ids[index];\n  const client = clean(row.client);\n  const currency = clean(row.currency).toUpperCase();\n  const raw = row.amount_minor;\n  const amount_minor = typeof raw === 'number' ? raw\n    : typeof raw === 'string' && /^\\d+$/.test(raw.trim())\n      ? Number(raw.trim()) : NaN;\n  const errors = [];\n  if (!order_id) errors.push('order_id_required');\n  if (order_id && counts.get(order_id) > 1) errors.push('duplicate_order_id');\n  if (!client) errors.push('client_required');\n  if (currency !== 'RUB') errors.push('currency_must_be_RUB');\n  if (!Number.isSafeInteger(amount_minor) || amount_minor < 0)\n    errors.push('amount_minor_invalid');\n\n  const json = { order_id, client, currency,\n    status: errors.length ? 'needs_review' : 'ready', errors };\n  if (!errors.length) {\n    json.amount_minor = amount_minor;\n    json.amount_rub = amount_minor / 100;\n  }\n  return { json, pairedItem: { item: index } };\n});"
      },
      "position": [
        240,
        0
      ]
    },
    {
      "id": "f67d98de-5669-40f5-a2b5-c3a8748e8e7f",
      "name": "Return Result",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "bcf2cb37-27c0-49a8-9cfd-534d3f315dbb",
              "name": "validation_version",
              "type": "string",
              "value": "v1"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "position": [
        480,
        0
      ]
    }
  ],
  "connections": {
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Validate Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Orders": {
      "main": [
        [
          {
            "node": "Return Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
