{
  "name": "ezGPT - Filter Orders",
  "active": false,
  "nodes": [
    {
      "id": "manual-trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "parameters": {}
    },
    {
      "id": "sample-orders",
      "name": "Sample Orders",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const orders = [\n  {\n    \"order_id\": \"O1\",\n    \"status\": \"ready\",\n    \"amount_minor\": 150000\n  },\n  {\n    \"order_id\": \"O2\",\n    \"status\": \"pending\",\n    \"amount_minor\": 180000\n  },\n  {\n    \"order_id\": \"O3\",\n    \"status\": \"ready\",\n    \"amount_minor\": 99999\n  },\n  {\n    \"order_id\": \"O4\",\n    \"status\": \"ready\",\n    \"amount_minor\": 100000\n  }\n];\nreturn orders.map(json => ({ json }));"
      }
    },
    {
      "id": "filter-orders",
      "name": "Filter Orders",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [
        680,
        300
      ],
      "alwaysOutputData": true,
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "status-ready",
              "leftValue": "={{ $json.status }}",
              "rightValue": "ready",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "minimum-amount",
              "leftValue": "={{ $json.amount_minor }}",
              "rightValue": 100000,
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ]
        },
        "looseTypeValidation": false,
        "options": {
          "ignoreCase": false
        }
      }
    },
    {
      "id": "filter-summary",
      "name": "Filter Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const source = $('Sample Orders').all().map(item => item.json);\nconst selected = $input.all()\n  .filter(item => Object.keys(item.json).length > 0)\n  .map(item => item.json);\nconst seen = new Set();\nfor (const row of source) {\n  if (typeof row.order_id !== 'string' || !row.order_id.trim() || seen.has(row.order_id)) {\n    throw new Error('source_id_missing_or_duplicate');\n  }\n  seen.add(row.order_id);\n}\nlet total = 0;\nfor (const row of selected) {\n  if (!seen.has(row.order_id) || !Number.isSafeInteger(row.amount_minor) || row.amount_minor < 0) {\n    throw new Error('selected_order_invalid');\n  }\n  total += row.amount_minor;\n  if (!Number.isSafeInteger(total)) throw new Error('selected_sum_out_of_range');\n}\nreturn [{ json: {\n  result: selected.length ? 'selected' : 'no_matches',\n  input_count: source.length,\n  selected_count: selected.length,\n  excluded_count: source.length - selected.length,\n  selected_ids: selected.map(row => row.order_id),\n  selected_amount_minor: total,\n  selected_amount_rub: total / 100,\n  action_performed: false,\n} }];"
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Orders": {
      "main": [
        [
          {
            "node": "Filter Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Orders": {
      "main": [
        [
          {
            "node": "Filter Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "all",
    "saveDataErrorExecution": "all"
  },
  "pinData": {}
}
