{
  "name": "ezGPT - PostgreSQL Version Check",
  "active": false,
  "nodes": [
    {
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "id": "pgupdate-0",
      "position": [
        0,
        260
      ]
    },
    {
      "name": "Sample Update",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return [{ json: { order_id: 'ORD-001', expected_version: 1 } }];"
      },
      "id": "pgupdate-1",
      "position": [
        240,
        260
      ]
    },
    {
      "name": "Check Input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one item');\nconst data = items[0].json;\nconst keys = ['order_id', 'expected_version'];\nif (Object.keys(data).length !== keys.length || !keys.every(key => Object.hasOwn(data, key)) ||\n    typeof data.order_id !== 'string' || !/^ORD-\\d{3}$/.test(data.order_id) ||\n    !Number.isSafeInteger(data.expected_version) || data.expected_version < 1 ||\n    data.expected_version > 1000000) {\n  throw new Error('Expected order_id and integer expected_version from 1 to 1000000');\n}\nreturn [{ json: { ...data } }];"
      },
      "id": "pgupdate-2",
      "position": [
        480,
        260
      ]
    },
    {
      "name": "Update Order",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "WITH changed AS (\n  UPDATE app.orders\n  SET status = 'approved', version = version + 1\n  WHERE order_id = $1 AND version = $2 AND status = 'new'\n  RETURNING order_id, status, version\n)\nSELECT count(*)::integer AS updated_count,\n       max(order_id) AS updated_order_id,\n       max(status) AS updated_status,\n       max(version) AS updated_version\nFROM changed;",
        "options": {
          "queryReplacement": "={{ [$json.order_id, $json.expected_version] }}"
        }
      },
      "id": "pgupdate-3",
      "position": [
        720,
        260
      ]
    },
    {
      "name": "Check Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one update result');\nconst row = items[0].json;\nconst input = $('Check Input').first().json;\nconst keys = ['updated_count', 'updated_order_id', 'updated_status', 'updated_version'];\nif (Object.keys(row).length !== keys.length || !keys.every(key => Object.hasOwn(row, key)) ||\n    ![0, 1].includes(row.updated_count)) {\n  throw new Error('Unexpected update result');\n}\nconst updated = row.updated_count === 1;\nif (updated && (row.updated_order_id !== input.order_id || row.updated_status !== 'approved' ||\n    row.updated_version !== input.expected_version + 1)) {\n  throw new Error('Unexpected changed row');\n}\nif (!updated && [row.updated_order_id, row.updated_status, row.updated_version].some(v => v !== null)) {\n  throw new Error('Unexpected fields for skipped update');\n}\nreturn [{ json: { order_id: input.order_id, updated,\n  reason: updated ? 'updated' : 'condition_not_met',\n  expected_version: input.expected_version, version: row.updated_version,\n  status: row.updated_status, verified: true, external_send: false } }];"
      },
      "id": "pgupdate-4",
      "position": [
        960,
        260
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Update": {
      "main": [
        [
          {
            "node": "Check Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Input": {
      "main": [
        [
          {
            "node": "Update Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Order": {
      "main": [
        [
          {
            "node": "Check Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
