{
  "name": "DPL · 2 · Consentimento & Handoff",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "consentimento",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "n1",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "webhookId": "consentimento"
    },
    {
      "parameters": {
        "jsCode": "// Detecta consentimento (SIM) ou opt-out (SAIR) na resposta da DM.\nconst b = $json.body ?? $json;\nconst resp = (b.text || b.reply || b.message || '').trim().toLowerCase();\nlet consent = 'pendente';\nif (/\\b(sim|aceito|pode|ok|confirmo|quero)\\b/.test(resp)) consent = 'sim';\nif (/\\b(sair|nao|não|stop|parar|remover)\\b/.test(resp)) consent = 'sair';\nconst contato = b.ig_id || b.contact_id || b.subscriber_id || '';\nconst msg_wpp = 'Oi! Aqui é da [Farmácia] 💚 Recebemos seu interesse pelo Instagram. Você já tem prescrição, ou é uma recomendação que viu com um profissional?';\nreturn [{ json: { contato, consent, msg_wpp } }];"
      },
      "id": "n2",
      "name": "Ler Resposta",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.consent }}",
              "rightValue": "sim",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "n3",
      "name": "Consentiu?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=UPDATE leads_funil SET consentimento_lgpd = true, consentimento_em = now(), status = 'consentido', atualizado_em = now()\nWHERE contato = '{{ $('Ler Resposta').item.json.contato }}'",
        "options": {}
      },
      "id": "n4",
      "name": "Registrar Consentimento",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.EVOLUTION_URL }}/message/sendText/{{ $env.EVOLUTION_INSTANCE }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.EVOLUTION_APIKEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ number: $('Ler Resposta').item.json.contato, text: $('Ler Resposta').item.json.msg_wpp }) }}",
        "options": {}
      },
      "id": "n5",
      "name": "WhatsApp: Boas-vindas",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=UPDATE leads_funil SET status = '{{ $('Ler Resposta').item.json.consent === 'sair' ? 'opt_out' : 'aguardando_consentimento' }}', atualizado_em = now()\nWHERE contato = '{{ $('Ler Resposta').item.json.contato }}'",
        "options": {}
      },
      "id": "n6",
      "name": "Registrar Opt-out / Aguardar",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        420
      ]
    },
    {
      "parameters": {
        "content": "## WF2 · Consentimento & Handoff\nSó passa para o WhatsApp **depois** do SIM (consentimento LGPD registrado). `SAIR` marca opt-out. O handoff usa a Evolution API.\n\n**Configurar:** Postgres (Supabase) + variáveis `EVOLUTION_URL`, `EVOLUTION_INSTANCE`, `EVOLUTION_APIKEY`. Webhook URL → ManyChat (no passo de resposta da DM).",
        "width": 560,
        "height": 280
      },
      "id": "n7",
      "name": "Nota",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        -60
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Ler Resposta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ler Resposta": {
      "main": [
        [
          {
            "node": "Consentiu?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Consentiu?": {
      "main": [
        [
          {
            "node": "Registrar Consentimento",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Registrar Opt-out / Aguardar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Registrar Consentimento": {
      "main": [
        [
          {
            "node": "WhatsApp: Boas-vindas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}