{
  "name": "ezGPT - Summarize orders by channel",
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "nodes": [
    {
      "id": "6bd3c384-89d8-4b27-ab8c-034ba7f92c17",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        0,
        0
      ]
    },
    {
      "id": "9164def8-0fab-4b77-a55d-3a644fddc40f",
      "name": "Test Orders",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "parameters": {
        "mode": "raw",
        "jsonOutput": "{\n  \"orders\": [\n    {\n      \"order_id\": \"O1\",\n      \"channel\": \"site\",\n      \"amount_minor\": 100000,\n      \"currency\": \"RUB\"\n    },\n    {\n      \"order_id\": \"O2\",\n      \"channel\": \"telegram\",\n      \"amount_minor\": 50000,\n      \"currency\": \"RUB\"\n    },\n    {\n      \"order_id\": \"O3\",\n      \"channel\": \"site\",\n      \"amount_minor\": 80000,\n      \"currency\": \"RUB\"\n    },\n    {\n      \"order_id\": \"O4\",\n      \"channel\": \"telegram\",\n      \"amount_minor\": 70000,\n      \"currency\": \"RUB\"\n    },\n    {\n      \"order_id\": \"O5\",\n      \"channel\": \"site\",\n      \"amount_minor\": 60000,\n      \"currency\": \"RUB\"\n    }\n  ]\n}",
        "options": {}
      },
      "position": [
        220,
        0
      ]
    },
    {
      "id": "44244993-01a5-4a03-bd12-ff22b992e744",
      "name": "Valid Input",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "df83041d-0c38-407c-a6f5-80f71b872e67",
              "leftValue": "={{ (() => {\n  const rows = $json.orders;\n  if (!Array.isArray(rows) || rows.length === 0) return false;\n  if (!rows.every(o => o !== null && typeof o === 'object'\n    && typeof o.order_id === 'string' && o.order_id.trim() !== ''\n    && o.order_id === o.order_id.trim()\n    && ['site', 'telegram'].includes(o.channel)\n    && o.currency === 'RUB'\n    && Number.isSafeInteger(o.amount_minor) && o.amount_minor >= 0)) return false;\n  return new Set(rows.map(o => o.order_id)).size === rows.length\n    && Number.isSafeInteger(rows.reduce((s, o) => s + o.amount_minor, 0));\n})() }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "position": [
        440,
        0
      ]
    },
    {
      "id": "2ba3cabd-ddbb-4945-8759-4bd11ccf3a02",
      "name": "Split Orders",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "parameters": {
        "fieldToSplitOut": "orders",
        "include": "noOtherFields",
        "options": {}
      },
      "position": [
        660,
        -100
      ]
    },
    {
      "id": "b90ee3b2-3e96-4d5e-bb1d-f931bbfe1f67",
      "name": "Channel Summary",
      "type": "n8n-nodes-base.summarize",
      "typeVersion": 1.1,
      "parameters": {
        "fieldsToSummarize": {
          "values": [
            {
              "aggregation": "count",
              "field": "order_id",
              "includeEmpty": false
            },
            {
              "aggregation": "sum",
              "field": "amount_minor"
            }
          ]
        },
        "fieldsToSplitBy": "channel",
        "options": {
          "outputFormat": "separateItems",
          "skipEmptySplitFields": false
        }
      },
      "position": [
        880,
        -100
      ]
    },
    {
      "id": "11bc31a0-eda6-4161-8bfb-e789894d6498",
      "name": "Report Rows",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f4242c7a-a614-4b23-8f55-0f8c8adc783c",
              "name": "channel",
              "type": "string",
              "value": "={{ $json.channel }}"
            },
            {
              "id": "0e5ec334-d07b-4c80-8a0f-0421a77c822a",
              "name": "orders_count",
              "type": "number",
              "value": "={{ $json.count_order_id }}"
            },
            {
              "id": "7f0cd625-8415-4aea-a105-c9fb22fa126e",
              "name": "amount_minor",
              "type": "number",
              "value": "={{ $json.sum_amount_minor }}"
            },
            {
              "id": "054697fa-003b-45fa-a813-8dfd2c087711",
              "name": "amount_rub",
              "type": "number",
              "value": "={{ $json.sum_amount_minor / 100 }}"
            },
            {
              "id": "8385d08e-8253-4770-818c-f5a6a08dc884",
              "name": "currency",
              "type": "string",
              "value": "RUB"
            },
            {
              "id": "7c0941e6-ef80-40ce-80ba-686e7378158b",
              "name": "status",
              "type": "string",
              "value": "ready"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "position": [
        1100,
        -100
      ]
    },
    {
      "id": "6722c7e5-7fb8-4943-9106-16fbb8bae875",
      "name": "Input Review",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b98dca10-6705-499e-a5e0-4588e8c7818a",
              "name": "status",
              "type": "string",
              "value": "needs_review"
            },
            {
              "id": "14139063-3c9f-4ad3-af1d-786327ea8944",
              "name": "reason",
              "type": "string",
              "value": "input_invalid_or_empty"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "position": [
        660,
        160
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Test Orders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Test Orders": {
      "main": [
        [
          {
            "node": "Valid Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid Input": {
      "main": [
        [
          {
            "node": "Split Orders",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Input Review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Orders": {
      "main": [
        [
          {
            "node": "Channel Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Channel Summary": {
      "main": [
        [
          {
            "node": "Report Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
