{
  "name": "DPL · 5 · Atribuição & Venda",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "venda",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "n1",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "webhookId": "venda"
    },
    {
      "parameters": {
        "jsCode": "// Registra a conversão. Payload: { lead_id, valor }\nconst b = $json.body ?? $json;\nif (!b.lead_id || b.valor == null) throw new Error('Payload precisa de lead_id e valor');\nreturn [{ json: { id: b.lead_id, valor: Number(b.valor) } }];"
      },
      "id": "n2",
      "name": "Validar Venda",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=UPDATE leads_funil SET status = 'convertido', valor_venda = {{ $json.valor }}, convertido_em = now(), atualizado_em = now()\nWHERE id = '{{ $json.id }}'",
        "options": {}
      },
      "id": "n3",
      "name": "Registrar Conversão",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=SELECT origem_tipo, origem_ref,\n        COUNT(*) AS vendas,\n        SUM(valor_venda) AS receita\nFROM leads_funil\nWHERE status = 'convertido'\nGROUP BY origem_tipo, origem_ref\nORDER BY receita DESC",
        "options": {}
      },
      "id": "n4",
      "name": "Receita por Origem",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ ok: true, ranking: $items().map(i => i.json) }) }}",
        "options": {}
      },
      "id": "n5",
      "name": "Responder",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "content": "## WF5 · Atribuição & Venda\nMarca a conversão com o valor e devolve o **ranking de receita por post/parceiro** — a resposta à pergunta de ouro: quais parcerias vendem. Dispare via webhook quando fechar a venda (ou por um botão no CRM).\n\n**Configurar:** Postgres (Supabase). Webhook URL → seu ponto de registro de venda.",
        "width": 560,
        "height": 260
      },
      "id": "n6",
      "name": "Nota",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        -60
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Validar Venda",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validar Venda": {
      "main": [
        [
          {
            "node": "Registrar Conversão",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Registrar Conversão": {
      "main": [
        [
          {
            "node": "Receita por Origem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receita por Origem": {
      "main": [
        [
          {
            "node": "Responder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}