{
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "name": "ezGPT - Save Order Results",
  "nodes": [
    {
      "id": "13faeaaf-8558-4174-8d69-32f73c597120",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        0,
        0
      ]
    },
    {
      "id": "64076b90-d2ec-44b0-b19f-7c32abe5c027",
      "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": "f38e3d63-e016-4806-afc2-94b5b25b5b66",
      "name": "Split Orders",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "parameters": {
        "fieldToSplitOut": "orders",
        "include": "noOtherFields",
        "options": {}
      },
      "position": [
        440,
        0
      ]
    },
    {
      "id": "55e1de5c-2eab-403c-a7aa-4511f52e9663",
      "name": "Prepare Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const rows = $input.all().map(item => item.json);\nconst clean = value => typeof value === 'string'\n  ? value.trim().replace(/\\s+/g, ' ') : '';\nconst ids = rows.map(row => clean(row.order_id));\nif (ids.some(id => !id) || new Set(ids).size !== ids.length)\n  throw new Error('invalid_or_duplicate_order_id');\n\nreturn rows.map((row, index) => {\n  const raw = row.amount_minor;\n  const amount = typeof raw === 'number' ? raw\n    : typeof raw === 'string' && /^\\d+$/.test(raw.trim())\n      ? Number(raw.trim()) : NaN;\n  const errors = [];\n  if (!clean(row.client)) errors.push('client_required');\n  if (clean(row.currency).toUpperCase() !== 'RUB')\n    errors.push('currency_must_be_RUB');\n  if (!Number.isSafeInteger(amount) || amount < 0)\n    errors.push('amount_minor_invalid');\n  return {\n    json: {\n      order_id: ids[index],\n      status: errors.length ? 'needs_review' : 'ready',\n      amount_minor: errors.length ? null : amount,\n      errors_text: errors.join('; '),\n      validation_version: 'v1',\n    },\n    pairedItem: { item: index },\n  };\n});"
      },
      "position": [
        660,
        0
      ]
    },
    {
      "id": "989f8eeb-d953-4d22-9f3a-cb71b712066f",
      "name": "Save Results",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "parameters": {
        "resource": "row",
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "name",
          "value": "ezgpt_order_results"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "order_id",
              "condition": "eq",
              "keyValue": "={{ $json.order_id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {
          "dryRun": false
        }
      },
      "position": [
        880,
        0
      ]
    }
  ],
  "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": "Prepare Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Rows": {
      "main": [
        [
          {
            "node": "Save Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
