{
  "name": "Veille CGP — Queue API (webhooks)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "GET",
        "path": "veille-cgp-queue",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "d1111111-0000-0000-0000-000000000001",
      "name": "Webhook GET file CGP",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [448, 80],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REMPLACER_PAR_CRED_HEADER",
          "name": "Fidulia webhook token"
        }
      },
      "notes": "GET /webhook/veille-cgp-queue. Réutilise le même credential Header Auth (X-Fidulia-Token) que la file veille LinkedIn — un webhook sans auth est une faille critique (UKP n8n Fondamentaux Axe 2). Renvoie les items PENDING pour la routine ROUTINE-VEILLE-CGP-HEBDO.md."
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1NvgtiwfgwKRjsm025O3qlXnUcw6QffZpVLI1_4IAHLc",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "CGP-Perplexity",
          "mode": "name"
        },
        "options": {}
      },
      "id": "d1111111-0000-0000-0000-000000000002",
      "name": "Lire file CGP-Perplexity",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [672, 80],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "EvORDqxqhAIKRlIW",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Ne renvoyer que les lignes PENDING.\nconst pending = [];\nfor (const item of $input.all()) {\n  const r = item.json;\n  if (String(r.statut || '').toUpperCase() !== 'PENDING') continue;\n  pending.push({\n    itemId: r.itemId,\n    runDate: r.runDate,\n    runId: r.runId,\n    title: r.title,\n    url: r.url,\n    source: r.source,\n    date: r.date,\n    summary: r.summary\n  });\n}\nreturn [{ json: { items: pending, count: pending.length } }];"
      },
      "id": "d1111111-0000-0000-0000-000000000003",
      "name": "Filtrer PENDING",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [896, 80]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json.items) }}",
        "options": {}
      },
      "id": "d1111111-0000-0000-0000-000000000004",
      "name": "Répondre (items JSON)",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1120, 80]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "veille-cgp-queue/done",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "e2222222-0000-0000-0000-000000000001",
      "name": "Webhook POST done",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [448, 360],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REMPLACER_PAR_CRED_HEADER",
          "name": "Fidulia webhook token"
        }
      },
      "notes": "POST /webhook/veille-cgp-queue/done. Body : { itemIds:[...], traitePar:'50' }. Marque chaque item DONE individuellement."
    },
    {
      "parameters": {
        "jsCode": "// Transforme { itemIds:[...], traitePar } en une ligne par itemId à mettre à jour.\nconst body = $json.body || $json;\nconst ids = Array.isArray(body.itemIds) ? body.itemIds : [];\nconst traitePar = body.traitePar || '50';\nconst dateTraitement = new Date().toISOString();\nif (ids.length === 0) return [];\nreturn ids.map(id => ({ json: { itemId: String(id), statut: 'DONE', traitePar, dateTraitement } }));"
      },
      "id": "e2222222-0000-0000-0000-000000000002",
      "name": "Préparer maj DONE",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [672, 360]
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "1NvgtiwfgwKRjsm025O3qlXnUcw6QffZpVLI1_4IAHLc",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "CGP-Perplexity",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": ["itemId"],
          "value": {}
        },
        "options": {}
      },
      "id": "e2222222-0000-0000-0000-000000000003",
      "name": "Marquer DONE",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [896, 360],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "EvORDqxqhAIKRlIW",
          "name": "Google Sheets account"
        }
      },
      "notes": "matchingColumns='itemId' (clé unique par ligne) — un run Perplexity produit jusqu'à 8 items partageant le même runId, donc matcher sur runId ne mettrait à jour que la première ligne trouvée. itemId garantit que chaque item est marqué DONE individuellement."
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ ok: true, marques: $items().length }) }}",
        "options": {}
      },
      "id": "e2222222-0000-0000-0000-000000000004",
      "name": "Répondre (ok)",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1120, 360]
    }
  ],
  "connections": {
    "Webhook GET file CGP": {
      "main": [[{ "node": "Lire file CGP-Perplexity", "type": "main", "index": 0 }]]
    },
    "Lire file CGP-Perplexity": {
      "main": [[{ "node": "Filtrer PENDING", "type": "main", "index": 0 }]]
    },
    "Filtrer PENDING": {
      "main": [[{ "node": "Répondre (items JSON)", "type": "main", "index": 0 }]]
    },
    "Webhook POST done": {
      "main": [[{ "node": "Préparer maj DONE", "type": "main", "index": 0 }]]
    },
    "Préparer maj DONE": {
      "main": [[{ "node": "Marquer DONE", "type": "main", "index": 0 }]]
    },
    "Marquer DONE": {
      "main": [[{ "node": "Répondre (ok)", "type": "main", "index": 0 }]]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "pinData": {},
  "meta": {
    "instanceId": "4f1b23afe1c1f425a998bc69ab78d2ebd64d2175c452226921ae034356fabed4"
  }
}
