{
  "name": "ezGPT - PostgreSQL active products without paid sales",
  "active": false,
  "nodes": [
    {
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "id": "pgunsold-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": "pgunsold-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": "pgunsold-2",
      "position": [
        480,
        260
      ]
    },
    {
      "name": "Find Unsold",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "onError": "stopWorkflow",
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "WITH missing AS (\n  SELECT p.sku, p.name, p.active\n  FROM app.products p\n  WHERE p.active AND NOT EXISTS (\n    SELECT 1 FROM app.sales s\n    WHERE s.sku = p.sku AND s.status = 'paid'\n      AND s.order_day >= $1::date AND s.order_day < $2::date\n  )\n)\nSELECT count(*)::integer AS product_count,\n       COALESCE(jsonb_agg(to_jsonb(m) ORDER BY m.sku), '[]'::jsonb) AS rows\nFROM missing m;",
        "options": {
          "queryReplacement": "={{ [$json.from, $json.to] }}"
        }
      },
      "id": "pgunsold-3",
      "position": [
        720,
        260
      ]
    },
    {
      "name": "Check Unsold",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one unsold envelope');\nconst d = items[0].json;\nif (Object.keys(d).length !== 2 || !Number.isSafeInteger(d.product_count) ||\n    d.product_count < 0 || !Array.isArray(d.rows) ||\n    d.product_count !== d.rows.length) throw new Error('Unsold envelope mismatch');\nconst seen = new Set();\nlet previous = '';\nfor (const r of d.rows) {\n  if (!r || Object.keys(r).length !== 3 ||\n      typeof r.sku !== 'string' || !/^SKU-[A-Z]$/.test(r.sku) ||\n      typeof r.name !== 'string' || !r.name.trim() || r.name.length > 120 ||\n      r.active !== true) throw new Error('Invalid unsold row');\n  if (seen.has(r.sku)) throw new Error('Duplicate product');\n  if (r.sku <= previous) throw new Error('Product order mismatch');\n  seen.add(r.sku); previous = r.sku;\n}\nconst p = $('Check Period').first().json;\nreturn [{json:{from:p.from,to:p.to,products_without_paid_sales:d.product_count,\n  report:d.rows,verified:true,external_send:false}}];"
      },
      "id": "pgunsold-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": "Find Unsold",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Unsold": {
      "main": [
        [
          {
            "node": "Check Unsold",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
