{
  "name": "BRIDGING",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "eb40afae-75c1-4083-b5f0-cc76de8d5231",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        100
      ],
      "id": "50a113cc-f19e-4676-800e-bcf1ad65d9f1",
      "name": "Webhook"
    },
    {
      "parameters": {
        "operation": "upsert",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "bank_details",
          "mode": "list",
          "cachedResultName": "bank_details"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "reference": "={{ $('Webhook').item.json.body.reference }}",
            "numero_facture": "={{ $('Webhook').item.json.body.numero_facture }}",
            "debit": "={{ $('Webhook').item.json.body.debit }}",
            "credit": "={{ $('Webhook').item.json.body.credit }}",
            "date_operation": "={{ $('Webhook').item.json.body.date_operation }}",
            "date_valeur": "={{ $('Webhook').item.json.body.date_valeur }}",
            "operation": "={{ $('Webhook').item.json.body.operation }}",
            "client_id": "={{ $('Webhook').item.json.body.client_id }}",
            "project_id": "={{ $('Webhook').item.json.body.project_id }}",
            "fournisseur_id": "={{ $json.body.fournisseur_id }}"
          },
          "matchingColumns": [
            "reference"
          ],
          "schema": [
            {
              "id": "reference",
              "displayName": "reference",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "numero_facture",
              "displayName": "numero_facture",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            },
            {
              "id": "date_operation",
              "displayName": "date_operation",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "date_valeur",
              "displayName": "date_valeur",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "operation",
              "displayName": "operation",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            },
            {
              "id": "debit",
              "displayName": "debit",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "credit",
              "displayName": "credit",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "date_extrait",
              "displayName": "date_extrait",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false,
              "removed": true
            },
            {
              "id": "project_id",
              "displayName": "project_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false,
              "removed": false
            },
            {
              "id": "client_id",
              "displayName": "client_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false,
              "removed": false
            },
            {
              "id": "fournisseur_id",
              "displayName": "fournisseur_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        140,
        100
      ],
      "id": "0245f3db-727a-415c-ac55-96db850e6585",
      "name": "Postgres",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE bank_details AS bd\nSET\n  fournisseur_id = vals.fournisseur_id,\n  numero_facture = vals.numero_facture,\n  date_operation = vals.date_operation::timestamp,\n  date_valeur = vals.date_valeur::timestamp,\n  operation = vals.operation,\n  debit = vals.debit,\n  credit = vals.credit\nFROM (\n  SELECT \n    (item->>'reference')::text AS reference,\n    (item->>'fournisseur_id')::text AS fournisseur_id,\n    (item->>'numero_facture')::text AS numero_facture,\n    (item->>'date_operation')::date AS date_operation,\n    (item->>'date_valeur')::date AS date_valeur,\n    (item->>'operation')::text AS operation,\n    (item->>'debit')::numeric AS debit,\n    (item->>'credit')::numeric AS credit\n  FROM jsonb_array_elements(CAST('{{ JSON.stringify($json.body) }}' AS jsonb)) AS item\n) AS vals\nWHERE bd.reference = vals.reference AND vals.reference != 'DUMMY';",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        140,
        -160
      ],
      "id": "e8e040c3-3def-43f0-9a83-1ef4ad599388",
      "name": "Postgres2",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE facture AS f\nSET montant = montant-vals.debit\nFROM (\n  VALUES\n  {{ $('Webhook1').item.json.body\n    .filter(item => item.numero_facture) // exclude null/empty\n    .map(item => {\n      const numero = `'${item.numero_facture.replace(/'/g, \"''\")}'`;\n      const debit = parseFloat(item.debit) ;\n      return `(${numero}, ${debit})`;\n    }).join(\",\\n\") }}\n) AS vals(numero, debit)\nWHERE f.numero = vals.numero;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        360,
        -160
      ],
      "id": "ba7d40bf-ab6b-46d8-8ff7-991a76d59e05",
      "name": "Postgres3",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "update facture set montant=montant-{{ $json.debit }}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        360,
        100
      ],
      "id": "1e782e00-9228-47f0-a16f-8ef6aee1d156",
      "name": "Postgres1",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "DELETE FROM facture\nWHERE numero IN (\n  {{ $('Webhook1').item.json.body\n    .filter(item => item.numero_facture)\n    .map(item => `'${item.numero_facture.replace(/'/g, \"''\")}'`)\n    .join(', ') }}\n)\nAND montant <= 0;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        580,
        -160
      ],
      "id": "f5341c3c-94fd-416b-a3fe-e87b1e3ded55",
      "name": "Postgres4",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "cccb654f-3a52-4404-9b4e-d297164671d4",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        360
      ],
      "id": "fcdbaf7c-1dbf-46b4-88d8-d1a7a4f22e27",
      "name": "Webhook2"
    },
    {
      "parameters": {
        "operation": "upsert",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "=bank_details",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "reference": "={{ $('Webhook2').item.json.body.reference }}",
            "date_valeur": "={{ $('Webhook2').item.json.body.date_valeur }}",
            "date_operation": "={{ $('Webhook2').item.json.body.date_operation }}",
            "numero_facture": "={{ $('Webhook2').item.json.body.numero_facture }}",
            "operation": "={{ $('Webhook2').item.json.body.operation }}",
            "debit": 0,
            "credit": "={{ $('Webhook2').item.json.body.credit }}",
            "date_extrait": "={{ $('Webhook2').item.json.body.date_extrait }}",
            "project_id": "={{ $('Webhook2').item.json.body.project_id }}",
            "client_id": "={{ $json.body.client_id }}"
          },
          "matchingColumns": [
            "reference"
          ],
          "schema": [
            {
              "id": "reference",
              "displayName": "reference",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "numero_facture",
              "displayName": "numero_facture",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            },
            {
              "id": "date_operation",
              "displayName": "date_operation",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "date_valeur",
              "displayName": "date_valeur",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "operation",
              "displayName": "operation",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            },
            {
              "id": "debit",
              "displayName": "debit",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "credit",
              "displayName": "credit",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "date_extrait",
              "displayName": "date_extrait",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "project_id",
              "displayName": "project_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "client_id",
              "displayName": "client_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": false
            },
            {
              "id": "fournisseur_id",
              "displayName": "fournisseur_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        140,
        360
      ],
      "id": "0b628277-7462-4084-8949-07d55fbbd4bb",
      "name": "Postgres6",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ac9b31c7-85f4-4de2-8576-9950762f5d82",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        620
      ],
      "id": "9eee3c2e-f818-4998-992d-b8e8a4944976",
      "name": "Webhook3"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE bank_details AS bd\nSET\n  client_id = vals.client_id,\n  numero_facture = vals.numero_facture,\n  date_operation = vals.date_operation,\n  date_valeur = vals.date_valeur,\n  operation = vals.operation,\n  debit = vals.debit,\n  credit = vals.credit\nFROM (\n  SELECT \n    (item->>'reference')::text AS reference,\n    NULLIF((item->>'client_id'), '')::integer AS client_id,\n    NULLIF((item->>'numero_facture'), '') AS numero_facture,\n    (item->>'date_operation')::timestamp AS date_operation,\n    (item->>'date_valeur')::timestamp AS date_valeur,\n    (item->>'operation')::text AS operation,\n    COALESCE((item->>'debit')::numeric, 0) AS debit,\n    COALESCE((item->>'credit')::numeric, 0) AS credit\n  FROM jsonb_array_elements(CAST('{{ JSON.stringify($json.body) }}' AS jsonb)) AS item\n) AS vals\nWHERE bd.reference = vals.reference\n  AND vals.reference != 'DUMMY'\n  AND vals.client_id IS NOT NULL;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        140,
        620
      ],
      "id": "9b4d5878-7a03-456d-9953-1deaa94fa452",
      "name": "Postgres7",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE facture AS f\nSET montant = montant-vals.debit\nFROM (\n  VALUES\n  {{ $('Webhook3').item.json.body\n    .filter(item => item.numero_facture) // exclude null/empty\n    .map(item => {\n      const numero = `'${item.numero_facture.replace(/'/g, \"''\")}'`;\n      const credit = parseFloat(item.credit) ;\n      return `(${numero}, ${credit})`;\n    }).join(\",\\n\") }}\n) AS vals(numero, debit)\nWHERE f.numero = vals.numero;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        360,
        620
      ],
      "id": "38659932-e404-42f8-8be3-888890d27e21",
      "name": "Postgres8",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "DELETE FROM facture\nWHERE numero IN (\n  {{ $('Webhook3').item.json.body\n    .filter(item => item.numero_facture)\n    .map(item => `'${item.numero_facture.replace(/'/g, \"''\")}'`)\n    .join(', ') }}\n)\nAND montant <= 0;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        580,
        620
      ],
      "id": "69269f0a-bbea-4cad-b083-f9ca419cd4c3",
      "name": "Postgres9",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "5756a79f-8f83-40b2-aba4-c10a1ad08843",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        -160
      ],
      "id": "f639b0a1-7212-48e7-b1b6-22dde69c4324",
      "name": "Webhook1"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "update facture set montant=montant-{{ $json.debit }}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        360,
        360
      ],
      "id": "e5c634c5-0006-46d3-9856-a73b05cbc91a",
      "name": "Postgres5",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres2": {
      "main": [
        [
          {
            "node": "Postgres3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "Postgres1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres3": {
      "main": [
        [
          {
            "node": "Postgres4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook2": {
      "main": [
        [
          {
            "node": "Postgres6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres6": {
      "main": [
        [
          {
            "node": "Postgres5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook3": {
      "main": [
        [
          {
            "node": "Postgres7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres7": {
      "main": [
        [
          {
            "node": "Postgres8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres8": {
      "main": [
        [
          {
            "node": "Postgres9",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook1": {
      "main": [
        [
          {
            "node": "Postgres2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fddb3f7c-e436-4bbd-96c1-4a23f94ead6c",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "aiD0pJ1QuWxEzpsG",
  "tags": []
}