{
  "name": "ezGPT - Code validate orders",
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "nodes": [
    {
      "id": "9bea4a80-f3c7-4d37-b89e-eaf6c63242bf",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        0,
        0
      ]
    },
    {
      "id": "10dc11c2-2173-4c2e-9645-1c2c7fcd11ef",
      "name": "Test Orders",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "parameters": {
        "mode": "raw",
        "jsonOutput": "{\n  \"orders\": [\n    {\n      \"order_id\": \"O1\",\n      \"client\": \"  Анна   Петрова \",\n      \"amount_minor\": \"150000\",\n      \"currency\": \"rub\"\n    },\n    {\n      \"order_id\": \"O2\",\n      \"client\": \"Борис\",\n      \"amount_minor\": 0,\n      \"currency\": \"RUB\"\n    },\n    {\n      \"order_id\": \"O3\",\n      \"client\": \"\",\n      \"amount_minor\": \"сумма\",\n      \"currency\": \"RUB\"\n    }\n  ]\n}",
        "options": {}
      },
      "position": [
        220,
        0
      ]
    },
    {
      "id": "2760f8ed-32bf-4bbf-8d8a-720f436842fd",
      "name": "Split Orders",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "parameters": {
        "fieldToSplitOut": "orders",
        "include": "noOtherFields",
        "options": {}
      },
      "position": [
        440,
        0
      ]
    },
    {
      "id": "a7f226fc-8457-4de4-8216-ebf9df3e07fc",
      "name": "Normalize 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": [
        660,
        0
      ]
    },
    {
      "id": "771694eb-ddf0-430d-beb5-9bc80fe99fb5",
      "name": "Ready Check",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "692c9604-4459-4ca4-86b2-d1bfdce8b726",
              "leftValue": "={{ $json.status }}",
              "rightValue": "ready",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "position": [
        880,
        0
      ]
    },
    {
      "id": "53cee79d-52d3-4bab-b539-6fbb92c1fc18",
      "name": "Ready Orders",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        1100,
        -100
      ]
    },
    {
      "id": "5ef15dc2-7a4d-43d9-a440-91c5b8c6afd9",
      "name": "Input Review",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        1100,
        160
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Test Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test Orders": {
      "main": [
        [
          {
            "node": "Split Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Orders": {
      "main": [
        [
          {
            "node": "Normalize Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Orders": {
      "main": [
        [
          {
            "node": "Ready Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ready Check": {
      "main": [
        [
          {
            "node": "Ready Orders",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Input Review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
