{
  "name": "ezGPT - Read XLSX Orders",
  "active": false,
  "nodes": [
    {
      "id": "manual",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        260
      ],
      "parameters": {}
    },
    {
      "id": "download",
      "name": "Download XLSX",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        220,
        260
      ],
      "parameters": {
        "method": "GET",
        "url": "https://ezgpt.ru/storage/article-files/n8n-orders-demo.xlsx",
        "authentication": "none",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": false,
              "responseFormat": "file",
              "outputPropertyName": "document"
            }
          },
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          },
          "timeout": 15000,
          "allowUnauthorizedCerts": false
        }
      }
    },
    {
      "id": "check-file",
      "name": "Check File",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('one_file_required');\nconst item = items[0];\nconst mime = String(item.json.headers?.['content-type'] ?? '').split(';')[0].trim().toLowerCase();\nconst allowed = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/octet-stream'];\nif (item.json.statusCode !== 200 || !allowed.includes(mime)) throw new Error('xlsx_http_response_required');\nif (Object.keys(item.binary ?? {}).join('|') !== 'document') throw new Error('document_required');\nconst bytes = await this.helpers.getBinaryDataBuffer(0, 'document');\nif (bytes.length < 4 || bytes.length > 100000 || bytes.readUInt32LE(0) !== 0x04034b50) {\n  throw new Error('small_xlsx_package_required');\n}\nreturn [{ json: {}, binary: item.binary }];"
      }
    },
    {
      "id": "read",
      "name": "Read XLSX",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        660,
        260
      ],
      "alwaysOutputData": true,
      "parameters": {
        "operation": "xlsx",
        "binaryPropertyName": "document",
        "options": {
          "sheetName": "Orders",
          "headerRow": true,
          "includeEmptyCells": true
        }
      }
    },
    {
      "id": "check-rows",
      "name": "Check Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        260
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (!items.length || (items.length === 1 && !Object.keys(items[0].json).length)) {\n  throw new Error('no_rows');\n}\nif (items.length > 20) throw new Error('too_many_rows');\nconst seen = new Set();\nreturn items.map(({ json: row }) => {\n  if (Object.keys(row).sort().join('|') !== 'order_id|price_rub|product|quantity') {\n    throw new Error('unexpected_columns');\n  }\n  if (typeof row.order_id !== 'string' || !/^[0-9]{4}$/.test(row.order_id) || seen.has(row.order_id)) {\n    throw new Error('unique_text_id_required');\n  }\n  seen.add(row.order_id);\n  if (typeof row.product !== 'string' || !row.product.trim() || row.product.length > 80) {\n    throw new Error('product_required');\n  }\n  if (!Number.isSafeInteger(row.quantity) || row.quantity < 0 || row.quantity > 1000000) {\n    throw new Error('integer_quantity_required');\n  }\n  const cents = Math.round(row.price_rub * 100);\n  if (typeof row.price_rub !== 'number' || !Number.isFinite(row.price_rub) || row.price_rub < 0 ||\n      row.price_rub > 1000000 || Math.abs(row.price_rub * 100 - cents) > 0.000001) {\n    throw new Error('numeric_price_required');\n  }\n  return { json: { ...row, line_total_rub: row.quantity * cents / 100, verified: true } };\n});"
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Download XLSX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download XLSX": {
      "main": [
        [
          {
            "node": "Check File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check File": {
      "main": [
        [
          {
            "node": "Read XLSX",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read XLSX": {
      "main": [
        [
          {
            "node": "Check Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
