{
  "name": "ingestion",
  "nodes": [
    {
      "parameters": {
        "url": "http://api:3000/people/v1/enrichments",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpBearerAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "page",
              "value": "={{ $json.page }}"
            },
            {
              "name": "limit",
              "value": "100"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        64,
        208
      ],
      "id": "eb59f9ed-764a-4e9e-a10e-a8299a52e0bb",
      "name": "HTTP Request",
      "retryOnFail": true,
      "maxTries": 5,
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const meta = $json.meta;\nconst currentPage = meta.current_page;\nconst totalPages = meta.total_pages;\n\nreturn [\n  {\n    json: {\n      page: currentPage + 1,\n      total: totalPages,\n    },\n  },\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        208
      ],
      "id": "e5ff7986-1c9d-4484-a182-5aa0c4bd966b",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "operation": "upsert",
        "schema": {
          "__rl": true,
          "value": "bronze",
          "mode": "list",
          "cachedResultName": "bronze"
        },
        "table": {
          "__rl": true,
          "value": "enriquecimentos",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{$json.id}}",
            "payload_json": "={{$json.payload_json}}",
            "dw_ingested_at": "2026-01-17T13:58:00",
            "dw_updated_at": "2026-01-17T13:58:01",
            "execution_id": "={{ $execution.id }}",
            "source_page": "={{ $json.payload_json.meta.current_page }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": true,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "payload_json",
              "displayName": "payload_json",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "object",
              "canBeUsedToMatch": false
            },
            {
              "id": "dw_ingested_at",
              "displayName": "dw_ingested_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "dw_updated_at",
              "displayName": "dw_updated_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "execution_id",
              "displayName": "execution_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": false
            },
            {
              "id": "source_page",
              "displayName": "source_page",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        240,
        0
      ],
      "id": "195c02e8-bb1d-4bd3-ae78-ce3c5bb88d61",
      "name": "Insert or update rows in a table",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const output = [];\n\nfor (const item of $input.all()) {\n  for (const data of item.json.data) {\n    output.push({\n      json: {\n        id: data.id,\n        payload_json: {\n          meta: item.json.meta,\n          data,\n        },\n      },\n    });\n  }\n}\n\nreturn output;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "id": "b32840db-37f9-4fb9-bc01-ba6cd6aa5090",
      "name": "Code in JavaScript1"
    },
    {
      "parameters": {
        "jsCode": "return [\n  {\n    json: {\n      execution_id: $execution.id,\n      page: 1,\n      started_at: new Date().toISOString(),\n      status: 'RUNNING',\n    },\n  },\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -224,
        272
      ],
      "id": "cc067bb2-e77b-4df1-b1e0-949aa41a74a8",
      "name": "Code in JavaScript2"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO control.api_ingestion_runs (\n  execution_id,\n  started_at,\n  status\n)\nVALUES (\n  {{ $json.execution_id }},\n  now(),\n  'RUNNING'\n);",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        -416,
        112
      ],
      "id": "efcb89f2-f58d-49ab-a2f0-17576dde1755",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE control.api_ingestion_runs\nSET\n  finished_at = now(),\n  status = 'SUCCESS'\nWHERE execution_id = '{{ $execution.id }}';",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        720,
        208
      ],
      "id": "4c2990fc-2d07-418d-b580-cd286e4c8e18",
      "name": "Execute a SQL query1",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "be078374-2840-422e-badb-de405f724a54",
              "leftValue": "={{ $json.page }}",
              "rightValue": "={{ $json.total + 1 }}",
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        448,
        208
      ],
      "id": "befea1d1-4864-4a86-9261-5136de84dced",
      "name": "If"
    },
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        -400,
        272
      ],
      "id": "68aff0c8-ee3d-4b94-a539-95e5b3224995",
      "name": "When Executed by Another Workflow"
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          },
          {
            "node": "Code in JavaScript1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript1": {
      "main": [
        [
          {
            "node": "Insert or update rows in a table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript2": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        []
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Execute a SQL query1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Code in JavaScript2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "835987c0-412f-4873-af40-3e078ec51432",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "Uxm-SlghMh4gTtgUcR252",
  "tags": []
}