{
  "name": "ezGPT - PostgreSQL orders status summary",
  "active": false,
  "nodes": [
    {
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "id": "pgstatus-0",
      "position": [
        0,
        260
      ]
    },
    {
      "name": "Sample Period",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return [{json:{\"from\":\"2026-10-01\",\"to\":\"2026-10-06\"}}];"
      },
      "id": "pgstatus-1",
      "position": [
        240,
        260
      ]
    },
    {
      "name": "Check Period",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one period item');\nconst p = items[0].json;\nfunction day(s) {\n  if (typeof s !== 'string' || !/^20\\d{2}-\\d{2}-\\d{2}$/.test(s)) throw new Error('Invalid date');\n  const n = Date.parse(s + 'T00:00:00Z');\n  if (!Number.isFinite(n) || new Date(n).toISOString().slice(0,10) !== s) throw new Error('Invalid date');\n  return n;\n}\nif (Object.keys(p).length !== 2 || !Object.hasOwn(p,'from') || !Object.hasOwn(p,'to')) {\n  throw new Error('Invalid period fields');\n}\nconst count = (day(p.to) - day(p.from)) / 86400000;\nif (!Number.isInteger(count) || count < 1 || count > 31) throw new Error('Period must contain 1 to 31 days');\nreturn [{json:{from:p.from,to:p.to}}];"
      },
      "id": "pgstatus-2",
      "position": [
        480,
        260
      ]
    },
    {
      "name": "Status Summary",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "onError": "stopWorkflow",
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "SELECT jsonb_build_object(\n  'total_orders', count(*),\n  'new_orders', count(*) FILTER (WHERE status = 'new'),\n  'paid_orders', count(*) FILTER (WHERE status = 'paid'),\n  'cancelled_orders', count(*) FILTER (WHERE status = 'cancelled'),\n  'paid_kopecks', COALESCE(sum(total_kopecks) FILTER (WHERE status = 'paid'),0)\n) AS summary\nFROM app.orders\nWHERE order_day >= $1::date AND order_day < $2::date;",
        "options": {
          "queryReplacement": "={{ [$json.from, $json.to] }}"
        }
      },
      "id": "pgstatus-3",
      "position": [
        720,
        260
      ]
    },
    {
      "name": "Check Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one summary item');\nconst envelope = items[0].json;\nif (Object.keys(envelope).length !== 1 || !Object.hasOwn(envelope,'summary')) {\n  throw new Error('Invalid summary envelope');\n}\nconst s = envelope.summary;\nconst keys = ['total_orders','new_orders','paid_orders','cancelled_orders','paid_kopecks'];\nif (!s || typeof s !== 'object' || Array.isArray(s) || Object.keys(s).length !== keys.length ||\n    !keys.every(k => Object.hasOwn(s,k) && Number.isSafeInteger(s[k]) && s[k] >= 0)) {\n  throw new Error('Invalid summary numbers');\n}\nconst count = s.new_orders + s.paid_orders + s.cancelled_orders;\nif (!Number.isSafeInteger(count) || count !== s.total_orders) throw new Error('Status counts mismatch');\nif (s.paid_orders === 0 && s.paid_kopecks !== 0) throw new Error('Paid amount without paid orders');\nconst p = $('Check Period').first().json;\nreturn [{json:{from:p.from,to:p.to,...s,verified:true,external_send:false}}];"
      },
      "id": "pgstatus-4",
      "position": [
        960,
        260
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Period",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Period": {
      "main": [
        [
          {
            "node": "Check Period",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Period": {
      "main": [
        [
          {
            "node": "Status Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Status Summary": {
      "main": [
        [
          {
            "node": "Check Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
