{
  "name": "Logixb2b — Pipedrive Lead pipeline",
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Webhook intención de Lead → crea persona + Lead en Pipedrive (etapa configurada) → Slack con enlace."
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Europe/Madrid"
  },
  "active": false,
  "pinData": {},
  "tags": [
    {
      "name": "Logixb2b"
    },
    {
      "name": "recursos"
    },
    {
      "name": "crm"
    },
    {
      "name": "pipedrive"
    }
  ],
  "nodes": [
    {
      "id": "sticky-instrucciones-logix",
      "name": "INSTRUCCIONES (conectar + VPS)",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -480,
        40
      ],
      "parameters": {
        "content": "## INSTRUCCIONES — Pipedrive Lead pipeline\n\n**Recurso:** `/recursos/pipedrive-pipeline-lead`\n**Guía:** /pipedrive-n8n-automatizacion-pipeline-ventas\n**Zona:** Europe/Madrid · `active: false` al importar (tú lo activas).\n\n### 1) Importar\n1. n8n → ⋯ → **Import from File** → este JSON.\n2. El flujo llega **inactive**. No pegues secretos en el lienzo ni en el archivo.\n\n### 2) Credenciales y configuración por nodo\n- **Webhook Lead Pipedrive** (`webhook`): sin credencial. Tras Active, copia **URL de producción** (HTTPS del VPS/Cloud). El emisor (formulario, CRM, bot) debe POST a esa URL.\n- **Config Pipedrive** (`Set` Config): **no lleva credencial**. Aquí van URLs/placeholders (`REEMPLAZA_*`, `example.com`, `TU-CRM`). Sustituye todos antes de Active.\n- **Normalizar persona + Lead** (`Set`): sin credencial. Revisa expresiones `={{ }}` y campos de salida.\n- **Pipedrive — crear person** (`HTTP Request` Pipedrive): Credentials → API token (query `api_token`) o Header. Sustituye company domain.\n- **Guardar personId** (`Set`): sin credencial. Revisa expresiones `={{ }}` y campos de salida.\n- **¿Person OK?** (`IF`): sin credencial. Comprueba la condición (email, umbral, over_limit, etc.) con datos reales de prueba.\n- **Pipedrive — crear Lead** (`HTTP Request` Pipedrive): Credentials → API token (query `api_token`) o Header. Sustituye company domain.\n- **Resultado Lead** (`Set`): sin credencial. Revisa expresiones `={{ }}` y campos de salida.\n- **¿Lead OK?** (`IF`): sin credencial. Comprueba la condición (email, umbral, over_limit, etc.) con datos reales de prueba.\n- **Slack — aviso Lead Pipedrive** (`HTTP Request` → Slack/Teams): crea Incoming Webhook en Slack (o Adaptive Card URL en Teams). Pega la URL en **Config** o en este nodo. Auth: none (la URL ya es secreta).\n\n### 3) Orden recomendado de puesta en marcha\n1. Rellena el nodo **Config** (URLs reales, umbrales, webhooks de Slack).\n2. Crea Credentials en n8n (candado de cada nodo HTTP / Sheets / OpenAI) y **enlázalas**.\n3. Ejecuta **una vez** (Manual / Test URL del Webhook / \"Execute workflow\").\n4. Verifica CRM + Slack/Sheets (y que la IA no escriba en producción sin HITL).\n5. Solo entonces: interruptor **Active**.\n\n### 4) Producción 24/7 en VPS (UE)\nImportar en localhost no basta: webhooks y cron necesitan n8n encendido.\n\n1. VPS en UE (Hetzner/OVH/IONOS…) + Docker Compose + **Postgres** + HTTPS (Caddy/Nginx).\n2. Variables mínimas: `GENERIC_TIMEZONE=Europe/Madrid`, `WEBHOOK_URL=https://n8n.tudominio.com/`, `N8N_ENCRYPTION_KEY`, `N8N_HOST` / `N8N_PROTOCOL=https`.\n3. Firewall: solo 80/443 (+ SSH). Backups del volumen Postgres + export de workflows.\n4. Importa este JSON en la instancia de producción, vuelve a crear Credentials (no se copian solas), actualiza URLs del Config al dominio real.\n5. Pon **Active**. Prueba el Webhook de producción o espera el próximo cron.\n6. Alternativa: **n8n Cloud** (mismo JSON; Credentials en la UI Cloud).\n\n### 5) RGPD + EU AI Act (resumen)\nSi hay nodos IA sois **deployers** (Reglamento UE 2024/1689). Art. 4 alfabetización. **HITL** antes de outreach, scores masivos o pausas de cuenta. Minimiza PII en Slack/logs. Informativo — no es asesoramiento legal. /uso-responsable-de-ia-y-transparencia\n",
        "height": 1500,
        "width": 580,
        "color": 4
      }
    },
    {
      "id": "pd-webhook",
      "name": "Webhook Lead Pipedrive",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        560
      ],
      "webhookId": "pd-deal",
      "parameters": {
        "httpMethod": "POST",
        "path": "pd-deal",
        "responseMode": "onReceived",
        "options": {}
      }
    },
    {
      "id": "pd-config",
      "name": "Config Pipedrive",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        300,
        560
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "pd-c1",
              "name": "apiToken",
              "value": "sustituye-con-tu-clave-de-pipedrive",
              "type": "string"
            },
            {
              "id": "pd-c2",
              "name": "companyDomain",
              "value": "tuempresa",
              "type": "string"
            },
            {
              "id": "pd-c3",
              "name": "pipelineId",
              "value": "1",
              "type": "string"
            },
            {
              "id": "pd-c4",
              "name": "stageId",
              "value": "1",
              "type": "string"
            },
            {
              "id": "pd-c5",
              "name": "slackWebhookUrl",
              "value": "https://example.com/aviso-equipo-slack",
              "type": "string"
            },
            {
              "id": "pd-c6",
              "name": "receivedAt",
              "value": "={{ $now.setZone('Europe/Madrid').toFormat('yyyy-MM-dd HH:mm') }}",
              "type": "string"
            },
            {
              "id": "pd-c7",
              "name": "rawBody",
              "value": "={{ $json.body || $json }}",
              "type": "object"
            }
          ]
        }
      }
    },
    {
      "id": "pd-normalize",
      "name": "Normalizar persona + Lead",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        600,
        560
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "pd-n1",
              "name": "personName",
              "value": "={{ String($json.rawBody.name || $json.rawBody.personName || 'Lead sin nombre').trim() }}",
              "type": "string"
            },
            {
              "id": "pd-n2",
              "name": "email",
              "value": "={{ String($json.rawBody.email || '').trim().toLowerCase() }}",
              "type": "string"
            },
            {
              "id": "pd-n3",
              "name": "orgName",
              "value": "={{ String($json.rawBody.company || $json.rawBody.orgName || $json.rawBody.empresa || 'Sin empresa').trim() }}",
              "type": "string"
            },
            {
              "id": "pd-n4",
              "name": "dealTitle",
              "value": "={{ String($json.rawBody.dealTitle || (($json.rawBody.company || $json.rawBody.empresa || 'Lead') + ' — ' + ($json.rawBody.name || 'Lead'))).trim() }}",
              "type": "string"
            },
            {
              "id": "pd-n5",
              "name": "dealValue",
              "value": "={{ Number($json.rawBody.value || $json.rawBody.dealValue || 0) }}",
              "type": "number"
            },
            {
              "id": "pd-n6",
              "name": "apiToken",
              "value": "={{ $json.apiToken }}",
              "type": "string"
            },
            {
              "id": "pd-n7",
              "name": "companyDomain",
              "value": "={{ $json.companyDomain }}",
              "type": "string"
            },
            {
              "id": "pd-n8",
              "name": "pipelineId",
              "value": "={{ $json.pipelineId }}",
              "type": "string"
            },
            {
              "id": "pd-n9",
              "name": "stageId",
              "value": "={{ $json.stageId }}",
              "type": "string"
            },
            {
              "id": "pd-n10",
              "name": "slackWebhookUrl",
              "value": "={{ $json.slackWebhookUrl }}",
              "type": "string"
            },
            {
              "id": "pd-n11",
              "name": "receivedAt",
              "value": "={{ $json.receivedAt }}",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "pd-person",
      "name": "Pipedrive — crear person",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        560
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ 'https://' + $json.companyDomain + '.pipedrive.com/api/v1/persons?api_token=' + $json.apiToken }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ name: $json.personName, email: [{ value: $json.email, primary: true, label: 'work' }], org_id: undefined, visible_to: '3' }) }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true
            }
          }
        }
      }
    },
    {
      "id": "pd-after-person",
      "name": "Guardar personId",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1200,
        560
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "pd-p1",
              "name": "personOk",
              "value": "={{ Number($json.statusCode) >= 200 && Number($json.statusCode) < 300 && !!$json.body?.data?.id }}",
              "type": "boolean"
            },
            {
              "id": "pd-p2",
              "name": "personId",
              "value": "={{ $json.body?.data?.id || '' }}",
              "type": "string"
            },
            {
              "id": "pd-p3",
              "name": "personStatus",
              "value": "={{ $json.statusCode }}",
              "type": "number"
            },
            {
              "id": "pd-p4",
              "name": "dealTitle",
              "value": "={{ $('Normalizar persona + Lead').item.json.dealTitle }}",
              "type": "string"
            },
            {
              "id": "pd-p5",
              "name": "dealValue",
              "value": "={{ $('Normalizar persona + Lead').item.json.dealValue }}",
              "type": "number"
            },
            {
              "id": "pd-p6",
              "name": "email",
              "value": "={{ $('Normalizar persona + Lead').item.json.email }}",
              "type": "string"
            },
            {
              "id": "pd-p7",
              "name": "orgName",
              "value": "={{ $('Normalizar persona + Lead').item.json.orgName }}",
              "type": "string"
            },
            {
              "id": "pd-p8",
              "name": "personName",
              "value": "={{ $('Normalizar persona + Lead').item.json.personName }}",
              "type": "string"
            },
            {
              "id": "pd-p9",
              "name": "apiToken",
              "value": "={{ $('Normalizar persona + Lead').item.json.apiToken }}",
              "type": "string"
            },
            {
              "id": "pd-p10",
              "name": "companyDomain",
              "value": "={{ $('Normalizar persona + Lead').item.json.companyDomain }}",
              "type": "string"
            },
            {
              "id": "pd-p11",
              "name": "pipelineId",
              "value": "={{ $('Normalizar persona + Lead').item.json.pipelineId }}",
              "type": "string"
            },
            {
              "id": "pd-p12",
              "name": "stageId",
              "value": "={{ $('Normalizar persona + Lead').item.json.stageId }}",
              "type": "string"
            },
            {
              "id": "pd-p13",
              "name": "slackWebhookUrl",
              "value": "={{ $('Normalizar persona + Lead').item.json.slackWebhookUrl }}",
              "type": "string"
            },
            {
              "id": "pd-p14",
              "name": "receivedAt",
              "value": "={{ $('Normalizar persona + Lead').item.json.receivedAt }}",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "pd-if-person",
      "name": "¿Person OK?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1500,
        560
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "pd-cond-person",
              "leftValue": "={{ $json.personOk }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "pd-deal",
      "name": "Pipedrive — crear Lead",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1800,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ 'https://' + $json.companyDomain + '.pipedrive.com/api/v1/deals?api_token=' + $json.apiToken }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ title: $json.dealTitle, value: $json.dealValue, currency: 'EUR', person_id: Number($json.personId), pipeline_id: Number($json.pipelineId), stage_id: Number($json.stageId) }) }}",
        "options": {
          "timeout": 20000,
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true
            }
          }
        }
      }
    },
    {
      "id": "pd-after-deal",
      "name": "Resultado Lead",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2100,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "pd-d1",
              "name": "dealOk",
              "value": "={{ Number($json.statusCode) >= 200 && Number($json.statusCode) < 300 && !!$json.body?.data?.id }}",
              "type": "boolean"
            },
            {
              "id": "pd-d2",
              "name": "dealId",
              "value": "={{ $json.body?.data?.id || '' }}",
              "type": "string"
            },
            {
              "id": "pd-d3",
              "name": "dealUrl",
              "value": "={{ 'https://' + $('Guardar personId').item.json.companyDomain + '.pipedrive.com/deal/' + ($json.body?.data?.id || '') }}",
              "type": "string"
            },
            {
              "id": "pd-d4",
              "name": "stageId",
              "value": "={{ $('Guardar personId').item.json.stageId }}",
              "type": "string"
            },
            {
              "id": "pd-d5",
              "name": "orgName",
              "value": "={{ $('Guardar personId').item.json.orgName }}",
              "type": "string"
            },
            {
              "id": "pd-d6",
              "name": "personName",
              "value": "={{ $('Guardar personId').item.json.personName }}",
              "type": "string"
            },
            {
              "id": "pd-d7",
              "name": "email",
              "value": "={{ $('Guardar personId').item.json.email }}",
              "type": "string"
            },
            {
              "id": "pd-d8",
              "name": "slackWebhookUrl",
              "value": "={{ $('Guardar personId').item.json.slackWebhookUrl }}",
              "type": "string"
            },
            {
              "id": "pd-d9",
              "name": "receivedAt",
              "value": "={{ $('Guardar personId').item.json.receivedAt }}",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "pd-if-deal",
      "name": "¿Lead OK?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2400,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "pd-cond-deal",
              "leftValue": "={{ $json.dealOk }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "pd-slack",
      "name": "Slack — aviso Lead Pipedrive",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2700,
        40
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $json.slackWebhookUrl }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $json.slackText || 'Actualización Logixb2b' }) }}",
        "options": {
          "timeout": 10000
        }
      }
    },
    {
      "id": "pd-fail",
      "name": "Fallo API — revisar",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1800,
        820
      ],
      "parameters": {}
    },
    {
      "id": "pd-fail-deal",
      "name": "Lead falló — revisar",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        2700,
        560
      ],
      "parameters": {}
    }
  ],
  "connections": {
    "Webhook Lead Pipedrive": {
      "main": [
        [
          {
            "node": "Config Pipedrive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config Pipedrive": {
      "main": [
        [
          {
            "node": "Normalizar persona + Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalizar persona + Lead": {
      "main": [
        [
          {
            "node": "Pipedrive — crear person",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pipedrive — crear person": {
      "main": [
        [
          {
            "node": "Guardar personId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Guardar personId": {
      "main": [
        [
          {
            "node": "¿Person OK?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "¿Person OK?": {
      "main": [
        [
          {
            "node": "Pipedrive — crear Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fallo API — revisar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pipedrive — crear Lead": {
      "main": [
        [
          {
            "node": "Resultado Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resultado Lead": {
      "main": [
        [
          {
            "node": "¿Lead OK?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "¿Lead OK?": {
      "main": [
        [
          {
            "node": "Slack — aviso Lead Pipedrive",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Lead falló — revisar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
