{
  "name": "Hunter_to_CleverReach_Workflow_Gastronomie",
  "nodes": [
    {
      "parameters": {},
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://maps.googleapis.com/maps/api/place/textsearch/json",
        "method": "GET",
        "queryParametersUi": {
          "parameter": [
            {
              "name": "query",
              "value": "Gastronomie OR Catering M\u00fcnchen"
            },
            {
              "name": "key",
              "value": "={{$env.GOOGLE_API_KEY}}"
            }
          ]
        },
        "responseFormat": "json"
      },
      "name": "Google Places Text Search",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = items[0].json.results || [];\nreturn results.map(r => ({ json: {name: r.name, place_id: r.place_id, address: r.formatted_address}}));"
      },
      "name": "Map Places -> Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://maps.googleapis.com/maps/api/place/details/json",
        "method": "GET",
        "queryParametersUi": {
          "parameter": [
            {
              "name": "place_id",
              "value": "={{$json.place_id}}"
            },
            {
              "name": "fields",
              "value": "name,website,formatted_address"
            },
            {
              "name": "key",
              "value": "={{$env.GOOGLE_API_KEY}}"
            }
          ]
        },
        "responseFormat": "json"
      },
      "name": "Place Details",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.map(it => { const website = it.json.result?.website || it.json.website || ''; let domain = null; try { domain = website ? (new URL(website)).hostname.replace(/^www\\./,'') : null; } catch (e) { domain = null; } return { json: { name: it.json.result?.name || it.json.name, place_id: it.json.place_id, address: it.json.result?.formatted_address || it.json.address, website, domain }}; });"
      },
      "name": "Extract Domain",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.hunter.io/v2/domain-search?domain={{$json.domain}}&api_key=DEIN_HUNTER_KEY",
        "method": "GET",
        "responseFormat": "json"
      },
      "name": "Hunter Domain Search",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const out = [];\nfor (const item of items) {\n  const emails = item.json.data?.emails || [];\n  for (const e of emails) {\n    out.push({ json: { business_name: item.json.name, place_id: item.json.place_id, domain: item.json.domain, email: e.value, position: e.position || '', type: e.type || '' }});\n  }\n}\nreturn out;"
      },
      "name": "Flatten Hunter Emails",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1400,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const allowedRoles = [\"ceo\",\"owner\",\"gesch\u00e4ftsf\u00fchrer\",\"founder\",\"marketing\",\"manager\",\"leitung\",\"inhaber\"];\nconst disallowed = [\"info\",\"kontakt\",\"buchhaltung\",\"rechnung\",\"jobs\",\"hr\",\"support\",\"noreply\",\"no-reply\"];\n\nreturn items.filter(it => {\n  const email = (it.json.email||'').toLowerCase();\n  const pos = (it.json.position||'').toLowerCase();\n\n  if (allowedRoles.some(k => pos.includes(k))) return true;\n  if (disallowed.some(k => email.includes(k))) return false;\n\n  const local = email.split('@')[0];\n  if (local.includes('.') && !local.match(/^[0-9\\-_.]+$/)) return true;\n\n  return false;\n});"
      },
      "name": "Filter Relevant Roles",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1600,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://rest.cleverreach.com/v3/receivers/{LIST_ID}",
        "method": "POST",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "{ \"email\": \"{{$json.email}}\", \"source\": \"Hunter.io Import\", \"attributes\": { \"company\": \"{{$json.business_name}}\", \"city\": \"{{$json.city || ''}}\" } }"
      },
      "name": "CleverReach Add Receiver",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1800,
        300
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Google Places Text Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Places Text Search": {
      "main": [
        [
          {
            "node": "Map Places -> Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Places -> Items": {
      "main": [
        [
          {
            "node": "Place Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Place Details": {
      "main": [
        [
          {
            "node": "Extract Domain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Domain": {
      "main": [
        [
          {
            "node": "Hunter Domain Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hunter Domain Search": {
      "main": [
        [
          {
            "node": "Flatten Hunter Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten Hunter Emails": {
      "main": [
        [
          {
            "node": "Filter Relevant Roles",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Relevant Roles": {
      "main": [
        [
          {
            "node": "CleverReach Add Receiver",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}