{
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "=Below is the customer information extracted from Zendesk. Analyze this data.\n\n{\n  \"companyName\": \"{{ $json.customer_name }}\",\n  \"email\": \"{{ $json.email }}\",\n  \"domain\": \"{{ $json.email ? $json.email.split('@').pop() : null }}\",\n  \"phone\": \"{{ $json.phone }}\",\n  \"address\": \"{{ $json.address }}\",\n  \"locale\": \"{{ $json.locale }}\"\n}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "={{ $('Aggregate Prompts').first().json.data.find(item => item.key === 'system_prompt_base_agent')?.value || \"You are a Helpful Assistant\" }}",
          "maxIterations": 10
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        2000,
        320
      ],
      "id": "0d994704-fbab-48b9-a288-fa40e94700a4",
      "name": "AI Agent",
      "retryOnFail": true
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        1920,
        112
      ],
      "id": "4fb88076-91f4-47b1-8d7e-4ee54d5fe0fa",
      "name": "Google Gemini Chat Model",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n  \"decision\": \"YES or NO\",\n  \"reason\": \"short one-sentence justification\",\n  \"estimated_shipping_volume\": \"low | medium | high\",\n  \"likely_logistics_type\": \"parcels | pallets | both | unknown\",\n  \"score\": 0.0,\n  \"compact_instruction\": \"company summary\"\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        2240,
        112
      ],
      "id": "adf8bf4f-dfd2-4fb3-b12d-4d5ab0cb6cd3",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "toolDescription": "Use this tool to perform real-time Google Search on the internet. The tool returns live web results, which you must analyze.",
        "method": "POST",
        "url": "=https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"system_instruction\": {\n    \"parts\": [\n      {\n        \"text\": \"{{ $('Aggregate Prompts').first().json.data.find(item => item.key === 'system_prompt_web_search_agent')?.value.replace(/\\n/g, '\\\\n') || \"You are a Internet Explorer\" }}\"\n      }\n    ]\n  },\n  \"contents\": [\n    {\n      \"parts\": [\n        {\n          \"text\": {{ JSON.stringify($fromAI(\"input\", \"\", \"string\")) }}\n        }\n      ]\n    }\n  ],\n  \"tools\": [\n    {\n      \"google_search\": {}\n    }\n  ]\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequestTool",
      "typeVersion": 4.3,
      "position": [
        2080,
        112
      ],
      "id": "0ecf856f-1205-4402-8a16-a7a22574725a",
      "name": "Gemini Search",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "6d1ee5eb-c704-48ed-99f5-b977449dc9ee",
              "leftValue": "={{ $json.output.decision.toLowerCase() }}",
              "rightValue": "yes",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "ab900d61-eacc-4494-9ab8-327ac1213599",
              "leftValue": "={{ $json.output.score }}",
              "rightValue": 5,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2320,
        320
      ],
      "id": "c1f79baa-0e41-43c3-85e5-3fc2e32fcdf5",
      "name": "If a Good Lead"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.pipedrive.com/api/v2/organizations",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"name\": \"{{ $json.body.customer_name }}{{ $json.body.email ? \"-\" + $json.body.email.split('@').pop() : '' }}\",\n  \"label_ids\": [10],\n  \"visible_to\": 3,\n  {{ $json.body.address ? `\"address\": {\n    \\\"value\\\": \\\"${$json.body.address}\\\"\n  },` : \"\" }}\n  \"custom_fields\": {\n    \"1a654f5d25fbae0ae82577dfa3448024f852e366\": \"{{ $json.body.zendesk_id }}\",\n    \"e62dba18429c4bb3307916ec46a4793d22f970c2\": {{ $json.body.lead_score }},\n    \"13096dc68ea12064d2c5e0935722ff0839caff2f\": \"{{ $json.body.estimated_shipping_volume }}\",\n    \"5df1b60a6200ac568db991d4e1440459a92ff39b\": \"{{ $json.body.likely_logistics_type }}\",\n    \"a000a1a83ca6689b763f109e03c5f43eb78e71cf\": {{ $json.body.zendesk_id }}\n  }\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        576,
        1328
      ],
      "id": "1a1c3ec1-9fb5-49f2-953f-6c7405316589",
      "name": "Create Organization",
      "retryOnFail": true,
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.pipedrive.com/api/v2/persons",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"name\": \"{{ $('Webhook').item.json.body.customer_name }}\",\n  \"label_ids\": [14],\n  \"org_id\": {{ $json.data.id }},\n  {{ $('Webhook').item.json.body.email ? `\"emails\": [{\n      \\\"value\\\": \\\"${$('Webhook').item.json.body.email}\\\",\n      \\\"primary\\\": true,\n      \\\"label\\\": \\\"Work\\\"\n    }],` : \"\" }}\n  \"phones\": [\n    {\n      \"value\": \"{{ $('Webhook').item.json.body.phone }}\",\n      \"primary\": true,\n      \"label\": \"Phone\"\n    }\n  ],\n  \"visible_to\": 3\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        784,
        1328
      ],
      "id": "5850eff1-3ac8-46d3-a2fe-bf05f7d5a801",
      "name": "Create Person",
      "retryOnFail": true,
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.pipedrive.com/api/v2/deals",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"title\": \"New Business Lead \u2013 {{ $('Webhook').item.json.body.customer_name }}\",\n  \"owner_id\": {{ $json.data.owner_id }},\n  \"person_id\": {{ $json.data.id }},\n  \"org_id\": {{ $json.data.org_id }},\n  \"pipeline_id\": 1,\n  \"stage_id\": 1,\n  \"value\": 0,\n  \"currency\": \"EUR\",\n  \"visible_to\": 3,\n  \"channel\": 37,\n  \"channel_id\": \"{{ $('Webhook').item.json.body.zendesk_id }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        992,
        1328
      ],
      "id": "6e7e5856-23a7-4697-a66a-d9d218c4f99a",
      "name": "Create Deal",
      "retryOnFail": true,
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.pipedrive.com/api/v1/notes",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": {{ JSON.stringify($('Webhook').item.json.body.note) }},\n  \"deal_id\": {{ $json.data.id }},\n  \"pinned_to_deal_flag\": 1\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1200,
        1328
      ],
      "id": "1ecf6a1d-6553-494b-99c1-a5a99a4b85f9",
      "name": "Create Note",
      "retryOnFail": true,
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        688,
        304
      ],
      "id": "bfec5e52-3edd-4dbc-a078-668301a3edfc",
      "name": "Aggregate Prompts"
    },
    {
      "parameters": {
        "content": "## Push Leads to Pipedrive",
        "height": 80,
        "width": 304,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        432,
        1184
      ],
      "typeVersion": 1,
      "id": "da4fef19-177a-41c6-957a-2b14759d9d69",
      "name": "Sticky Note3"
    },
    {
      "parameters": {
        "content": "## \ud83d\udd27 Setup Requirements\n\n**Required Credentials:**\n1. **Pipedrive API** (Query Auth)\n   - Parameter: api_token\n\n2. **Google Gemini API**\n   - PaLM API account\n",
        "height": 236,
        "width": 400,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        976
      ],
      "typeVersion": 1,
      "id": "3cb71af8-06b8-479d-b2b9-a55f952fd075",
      "name": "Setup - Credentials"
    },
    {
      "parameters": {
        "content": "## \ud83d\udcca Data Tables Required\n\n**Table 1: verpakkingenxl-2.0-zendesk**\nMain lead database with fields:\n- Company info (name, CoC, VAT, address)\n- Contact details (name, email, phone)\n- AI results (decision, score, reason)\n- Status tracking (queued/processed)\n- Pipedrive IDs (org, person, deal, note)\n\n**Table 2: verpakkingenxl-2.0-KVS**\nConfiguration storage:\n- system_prompt_base_agent\n- system_prompt_web_search_agent\n- daily_processing_limit",
        "height": 392,
        "width": 400,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        1264
      ],
      "typeVersion": 1,
      "id": "50ae0876-8e5c-4573-bede-48d5af608adc",
      "name": "Setup - Data Tables"
    },
    {
      "parameters": {
        "content": "## \ud83e\udd16 AI Lead Qualification Engine\n\n**Model:** Google Gemini Chat\n**Tools Available:**\n- Gemini Search (web grounding)\n\n**Qualification Process:**\n1. Receives company data from webhook\n2. Performs web research on business\n3. Analyzes shipping potential\n4. Generates structured output:\n   - Decision: YES/NO\n   - Score: 0-10\n   - Reason: Justification\n   - Shipping Volume: low/medium/high\n   - Logistics Type: parcels/pallets/both\n   - Summary: Business overview\n\n**Quality Gate:** Only leads with score > 5",
        "height": 480,
        "width": 440,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1712,
        624
      ],
      "typeVersion": 1,
      "id": "25afaef8-632d-468e-a755-7f1c6db13f62",
      "name": "AI Qualification Engine"
    },
    {
      "parameters": {
        "content": "## \ud83c\udf93 AI Prompt Engineering\n\n**System Prompts Location:** KVS Data Table\n\n**Base Agent Prompt:**\n- Defines AI personality and role\n- Sets qualification criteria\n- Specifies output format\n- Guides decision logic\n\n**Web Search Agent Prompt:**\n- Instructs search strategy\n- Defines relevant sources\n- Guides data extraction\n- Sets research depth\n\n**Best Practices:**\n1. Be specific about output format\n2. Include examples of good decisions\n3. Define edge cases clearly\n4. Test with various company types\n5. Iterate based on results\n\n**Tip:** Update prompts in KVS table without changing workflow structure",
        "height": 564,
        "width": 400,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1200,
        608
      ],
      "typeVersion": 1,
      "id": "65c0bdca-75b9-42b8-8f9b-4e526d8be761",
      "name": "AI Prompt Engineering"
    },
    {
      "parameters": {
        "content": "## \ud83c\udfaf Lead Scoring Logic\n\n**If Score > 5:**\n\u2192 Added to queue\n\u2192 Creates complete lead record\n\u2192 Processed in daily batch\n\n**Decision Criteria:**\n- Business size & potential\n- Shipping volume estimation\n- Industry relevance\n- Company legitimacy\n- Market presence",
        "height": 324,
        "width": 360,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2240,
        624
      ],
      "typeVersion": 1,
      "id": "51df5005-f02b-44af-b76e-4d15422f7cf6",
      "name": "Lead Scoring Logic"
    },
    {
      "parameters": {
        "content": "## Research on Business",
        "height": 80,
        "width": 352,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        352,
        192
      ],
      "typeVersion": 1,
      "id": "827ee4be-b57f-41ee-954a-a0ed64b2f29e",
      "name": "Sticky Note4"
    },
    {
      "parameters": {
        "content": "",
        "height": 2224,
        "width": 2752,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        224,
        32
      ],
      "typeVersion": 1,
      "id": "5c212586-cecd-4d62-a77f-73999f98edb0",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "value": "YKWzyvGF8jUIxKwI",
          "mode": "list",
          "cachedResultName": "verpakkingenxl-2.0-KVS",
          "cachedResultUrl": "/projects/9rlsqBZGPNj4ARdI/datatables/YKWzyvGF8jUIxKwI"
        },
        "returnAll": true
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        512,
        304
      ],
      "id": "24cf5009-60d6-4f0d-93c4-006bf58ace59",
      "name": "Get KVS"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1792,
        304
      ],
      "id": "6dd86a68-8a9a-428d-b3a2-f3241ffff2ab",
      "name": "Loop Over Raw Items"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "adefb025-2a16-4b18-bf4b-dacc972b77e1",
              "name": "done",
              "value": true,
              "type": "boolean"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2768,
        368
      ],
      "id": "e46c9234-0f28-4cb8-8645-3c1ba1645eb7",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "push-to-pipedrive",
        "responseMode": "responseNode",
        "options": {
          "ignoreBots": true
        }
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        384,
        1328
      ],
      "id": "55ddbf92-1e37-4619-8556-96ee895bd830",
      "name": "Webhook"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"org_id\": {{ $json.org_id }},\n  \"person_id\": {{ $json.person_id }},\n  \"deal_id\": {{ $json.deal_id }},\n  \"note_id\": {{ $json.note_id }}\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1584,
        1328
      ],
      "id": "430ff2fc-358c-458f-bb53-0f6cf3117aed",
      "name": "Respond to Webhook"
    },
    {
      "parameters": {
        "content": "## AI-Powered Lead Qualification System\n\n**Purpose:** Automatically research Zendesk leads using AI and push qualified ones to Pipedrive.\n\n**Two Modes:**\n1. **Research Mode** - Daily processing of new leads with AI qualification\n2. **Push Mode** - Daily at 8 PM UTC or via webhook to create Pipedrive records\n\n**Data Tables:**\n- `verpakkingenxl-2.0-zendesk` - Lead database\n- `verpakkingenxl-2.0-KVS` - Configuration store\n\n**AI Processing:**\n- Google Gemini with web search\n- Scores leads 0-10\n- Only leads with score > 5 get pushed to Pipedrive\n\n**Pipedrive Creation:**\nOrganization \u2192 Person \u2192 Deal \u2192 Note (all linked together)",
        "height": 556,
        "width": 400,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        384
      ],
      "typeVersion": 1,
      "id": "cfd24925-f7cd-4304-8b87-264326848dc8",
      "name": "Setup - Credentials1"
    },
    {
      "parameters": {
        "operation": "deleteRows",
        "dataTableId": {
          "__rl": true,
          "value": "wdNYwr5ue45v2HP4",
          "mode": "list",
          "cachedResultName": "verpakkingenxl-2.0-zendesk",
          "cachedResultUrl": "/projects/9rlsqBZGPNj4ARdI/datatables/wdNYwr5ue45v2HP4"
        },
        "filters": {
          "conditions": [
            {
              "keyName": "zendesk_id",
              "keyValue": "={{ $('Loop Over Raw Items').first().json.zendesk_id }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        2560,
        464
      ],
      "id": "dd9f3fb1-e43f-4abb-b746-85224a2165fe",
      "name": "Delete Custom Data"
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "wdNYwr5ue45v2HP4",
          "mode": "list",
          "cachedResultName": "verpakkingenxl-2.0-zendesk",
          "cachedResultUrl": "/projects/9rlsqBZGPNj4ARdI/datatables/wdNYwr5ue45v2HP4"
        },
        "filters": {
          "conditions": [
            {
              "keyName": "zendesk_id",
              "keyValue": "={{ $('Loop Over Raw Items').first().json.zendesk_id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "lead_score": "={{ Number($json.output.score) }}",
            "decision": "={{ $json.output.decision.toLowerCase() }}",
            "estimated_shipping_volume": "={{ $json.output.estimated_shipping_volume.toLowerCase() }}",
            "reason": "={{ $json.output.reason }}",
            "likely_logistics_type": "={{ $json.output.likely_logistics_type.toLowerCase() }}",
            "compact_instruction": "={{ $json.output.compact_instruction }}",
            "note": "=<p><strong>From: {{ $('Loop Over Raw Items').first().json.source.charAt(0).toUpperCase() + $('Loop Over Raw Items').first().json.source.slice(1) }}</strong></p>\n<p><strong>AI Qualification Result:</strong></p>\n<ul>\n  <li><strong>Decision:</strong> {{ $('AI Agent').item.json.output.decision.toLowerCase() }}</li>\n  <li><strong>Lead Score:</strong> {{ $('AI Agent').item.json.output.score }}</li>\n  <li><strong>Reason:</strong> {{ $('AI Agent').item.json.output.reason }}</li>\n  <li><strong>Estimated Shipping Volume:</strong> {{ $('AI Agent').item.json.output.estimated_shipping_volume.toLowerCase() }}</li>\n  <li><strong>Likely Logistics Type:</strong> {{ $('AI Agent').item.json.output.likely_logistics_type.toLowerCase() }}</li>\n  <li><strong>Summary:</strong> {{ $('AI Agent').item.json.output.compact_instruction }}</li>\n</ul>",
            "is_searched": true
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "zendesk_id",
              "displayName": "zendesk_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "customer_name",
              "displayName": "customer_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "email",
              "displayName": "email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "address",
              "displayName": "address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "locale",
              "displayName": "locale",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "source",
              "displayName": "source",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "decision",
              "displayName": "decision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "reason",
              "displayName": "reason",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "estimated_shipping_volume",
              "displayName": "estimated_shipping_volume",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "likely_logistics_type",
              "displayName": "likely_logistics_type",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "compact_instruction",
              "displayName": "compact_instruction",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "note",
              "displayName": "note",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "lead_score",
              "displayName": "lead_score",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "org_id",
              "displayName": "org_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "person_id",
              "displayName": "person_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "deal_id",
              "displayName": "deal_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "note_id",
              "displayName": "note_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "is_searched",
              "displayName": "is_searched",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "boolean",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        2560,
        304
      ],
      "id": "bcf0aae9-4a4e-4128-b339-24504e71e427",
      "name": "Update AI Data"
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "wdNYwr5ue45v2HP4",
          "mode": "list",
          "cachedResultName": "verpakkingenxl-2.0-zendesk",
          "cachedResultUrl": "/projects/9rlsqBZGPNj4ARdI/datatables/wdNYwr5ue45v2HP4"
        },
        "filters": {
          "conditions": [
            {
              "keyName": "zendesk_id",
              "keyValue": "={{ $('Webhook').item.json.body.zendesk_id }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "org_id": "={{ $json.data.org_id }}",
            "deal_id": "={{ $json.data.deal_id }}",
            "person_id": "={{ $json.data.person_id }}",
            "note_id": "={{ $json.data.id }}",
            "status": "processed"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "zendesk_id",
              "displayName": "zendesk_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "customer_name",
              "displayName": "customer_name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "email",
              "displayName": "email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "address",
              "displayName": "address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "locale",
              "displayName": "locale",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "source",
              "displayName": "source",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "decision",
              "displayName": "decision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "reason",
              "displayName": "reason",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "estimated_shipping_volume",
              "displayName": "estimated_shipping_volume",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "likely_logistics_type",
              "displayName": "likely_logistics_type",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "compact_instruction",
              "displayName": "compact_instruction",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "note",
              "displayName": "note",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "lead_score",
              "displayName": "lead_score",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "org_id",
              "displayName": "org_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "person_id",
              "displayName": "person_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "deal_id",
              "displayName": "deal_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "note_id",
              "displayName": "note_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "is_searched",
              "displayName": "is_searched",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "boolean",
              "readOnly": false,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        1392,
        1328
      ],
      "id": "2e342ebe-de23-4cac-b268-2ed0daa50a4f",
      "name": "Update Pipedrive Data"
    },
    {
      "parameters": {
        "content": "## \ud83d\udcbc Pipedrive Record Creation\n\n**Creates 4 Records:**\n\n1. **Organization**\n   - Name format: customer_name-domain\n   - Custom fields:\n     - Zendesk ID\n     - Lead Score\n     - Estimated Shipping Volume\n     - Likely Logistics Type\n   - Label: B2B\n   - Visibility: All\n\n2. **Person (Contact)**\n   - Linked to organization\n   - Email & phone numbers\n   - Label: B2B Contact\n   - Visibility: All\n\n3. **Deal**\n   - Title: \"New Business Lead \u2013 [Company]\"\n   - Pipeline Stage 1\n   - Channel: Zendesk\n   - Owner inherited from person\n\n4. **Note**\n   - AI qualification details\n   - Pinned to deal",
        "height": 676,
        "width": 368
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1088,
        1520
      ],
      "typeVersion": 1,
      "id": "d6f7fd28-8405-4525-9a80-f8faedd739e9",
      "name": "Pipedrive Creation"
    },
    {
      "parameters": {
        "content": "## \ud83d\udcdd Configuration Guide\n\n**Step 1: Pipedrive Custom Fields**\nCreate organization custom fields with these IDs:\n- 1a654f5d... (Zendesk ID)\n- e62dba18... (Lead Score)\n- 13096dc6... (Estimated Shipping Volume)\n- 5df1b60a... (Likely Logistics Type)\n- a000a1a8... (Zendesk ID duplicate)\n\n**Step 2: Update Node Configuration**\nReplace field IDs in:\n- Create Organization node\n\n**Step 3: Pipeline Settings**\n- Pipeline ID: 1\n- Stage ID: 1\n- Channel: 37 (n8n)\n- Visibility: 3 (All)\n\n**Step 4: Labels**\n- Organization Label: 10 (Customer)\n- Person Label: 14 (Customer)",
        "height": 516,
        "width": 400,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1504,
        1520
      ],
      "typeVersion": 1,
      "id": "a80005b4-a0ed-482b-9e2e-85a44d1f9b81",
      "name": "Configuration Guide"
    },
    {
      "parameters": {
        "content": "## Push Lead to Pipedrive Workflow\n\n**Webhook:** POST `/push-to-pipedrive`\n\n**Flow:**\n```\nWebhook \u2192 Create Org \u2192 Create Person \u2192 Create Deal \u2192 Create Note \u2192 Update DB \u2192 Respond\n```\n\n**Input Required:**\n- zendesk_id\n- customer_name\n- email\n- phone\n- lead_score\n- estimated_shipping_volume\n- likely_logistics_type\n- note (AI analysis)\n\n**Process:**\n1. Creates Organization with custom fields\n2. Creates Person linked to org\n3. Creates Deal in Pipeline Stage 1\n4. Adds Note with AI analysis\n5. Updates database status to 'processed'\n6. Returns: org_id, person_id, deal_id, note_id\n\n**Database Update:**\nMarks lead as 'processed' and stores Pipedrive IDs",
        "height": 668,
        "width": 672,
        "color": 2
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        352,
        1520
      ],
      "typeVersion": 1,
      "id": "d6a22256-f37a-403e-a77a-553b6ff64826",
      "name": "Batch Processing Schedule"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        336,
        304
      ],
      "id": "f1ebb2a1-2bfa-4ca1-91ac-d9f7fa06e649",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "value": "wdNYwr5ue45v2HP4",
          "mode": "list",
          "cachedResultName": "verpakkingenxl-2.0-zendesk",
          "cachedResultUrl": "/projects/9rlsqBZGPNj4ARdI/datatables/wdNYwr5ue45v2HP4"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "is_searched",
              "condition": "isFalse"
            },
            {
              "keyName": "source",
              "keyValue": "=zendesk"
            }
          ]
        },
        "limit": "={{ $('Aggregate Prompts').item.json.data.find(item => item.key === 'daily_processing_limit')?.value || 50 }}"
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        1232,
        304
      ],
      "id": "e22c83cd-f781-46c7-8e3f-b93de56076aa",
      "name": "Get Raw Customer Data Zendesk",
      "executeOnce": false,
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "value": "wdNYwr5ue45v2HP4",
          "mode": "list",
          "cachedResultName": "verpakkingenxl-2.0-zendesk",
          "cachedResultUrl": "/projects/9rlsqBZGPNj4ARdI/datatables/wdNYwr5ue45v2HP4"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "is_searched",
              "condition": "isFalse"
            },
            {
              "keyName": "source",
              "keyValue": "=erp"
            }
          ]
        },
        "limit": "={{ $json[\"data\"].find(item => item.key === 'daily_processing_limit')?.value || 50 }}"
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        880,
        304
      ],
      "id": "79625eb7-9e87-40ea-80ab-73aedf024d5e",
      "name": "Get Raw Customer Data Erp",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1056,
        304
      ],
      "id": "4b3320c0-ed0f-45fa-a78a-c46daf4889a4",
      "name": "Aggregate Erp",
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1408,
        304
      ],
      "id": "6d707343-0aa4-40e4-93c5-cfc13dbdb6ab",
      "name": "Aggregate Zendesk"
    },
    {
      "parameters": {
        "jsCode": "// Cleaned inputs\nconst zendeskData = ($('Aggregate Zendesk').first().json.data || [])\n  .filter(i => i && Object.keys(i).length);\n\nconst erpData = ($('Aggregate Erp').first().json.data || [])\n  .filter(i => i && Object.keys(i).length);\n\n// Limit\nconst limit = $('Aggregate Prompts')\n  .first()\n  .json\n  .data\n  ?.find(i => i.key === 'daily_processing_limit')\n  ?.value || 0;\n\nfunction pickBalanced(z, e, limit) {\n  const half = Math.floor(limit / 2);\n\n  let takeZ = Math.min(half, z.length);\n  let takeE = Math.min(half, e.length);\n\n  if (takeZ < half) takeE = Math.min(e.length, limit - takeZ);\n  if (takeE < half) takeZ = Math.min(z.length, limit - takeE);\n\n  return [\n    ...z.slice(0, takeZ),\n    ...e.slice(0, takeE)\n  ];\n}\n\nreturn pickBalanced(zendeskData, erpData, limit);\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1584,
        304
      ],
      "id": "9a4fdfdc-40f5-4047-a718-d5bde5a1b00f",
      "name": "Combine Data"
    },
    {
      "parameters": {
        "content": "## Lead Qualification Process\n\n**Workflow Overview:**\n```\nSchedule (hourly) \u2192 Get KVS \u2192 Aggregate Prompts \u2192 Get ERP Data \u2192 Aggregate ERP \u2192 Get Zendesk Data \u2192 Aggregate Zendesk \u2192 Balance & Merge \u2192 Loop Over Items \u2192 AI Agent \u2192 Decision \u2192 Update/Delete \u2192 Loop Back\n```\n\n**Before Loop:**\n- **Get KVS:** Fetches AI prompts and `daily_processing_limit` from config table\n- **Aggregate Prompts:** Makes config accessible to downstream nodes\n- **Get Raw Data (ERP & Zendesk):** Fetches unprocessed leads where `is_searched = false` from respective sources\n- **Balance & Merge:** JavaScript code balances ERP/Zendesk leads (50/50 split) up to processing limit\n\n**Configuration:** Edit system prompts and limits in KVS table without workflow changes",
        "height": 404,
        "width": 692,
        "color": 6
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        320,
        480
      ],
      "typeVersion": 1,
      "id": "e759bcfd-3b7a-43eb-adb6-1a31fe6dd339",
      "name": "Lead Qualification Overview"
    }
  ],
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "If a Good Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Gemini Search": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "If a Good Lead": {
      "main": [
        [
          {
            "node": "Update AI Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delete Custom Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Organization": {
      "main": [
        [
          {
            "node": "Create Person",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Person": {
      "main": [
        [
          {
            "node": "Create Deal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Deal": {
      "main": [
        [
          {
            "node": "Create Note",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Note": {
      "main": [
        [
          {
            "node": "Update Pipedrive Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Prompts": {
      "main": [
        [
          {
            "node": "Get Raw Customer Data Erp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get KVS": {
      "main": [
        [
          {
            "node": "Aggregate Prompts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Raw Items": {
      "main": [
        [],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Loop Over Raw Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Create Organization",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Custom Data": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update AI Data": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Pipedrive Data": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get KVS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Raw Customer Data Zendesk": {
      "main": [
        [
          {
            "node": "Aggregate Zendesk",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Raw Customer Data Erp": {
      "main": [
        [
          {
            "node": "Aggregate Erp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Erp": {
      "main": [
        [
          {
            "node": "Get Raw Customer Data Zendesk",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Zendesk": {
      "main": [
        [
          {
            "node": "Combine Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Data": {
      "main": [
        [
          {
            "node": "Loop Over Raw Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}