{
  "name": "Bella - Lead Enricher",
  "nodes": [
    {
      "id": "3e347782-c891-4197-bef9-a3ec09f21fd9",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "bella-enrich",
        "responseMode": "responseNode"
      }
    },
    {
      "id": "cb6328fd-8d4a-44e1-a95e-afe609917b83",
      "name": "Code: Extract Leads",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        280,
        0
      ],
      "parameters": {
        "jsCode": "const body = $input.first().json.body || $input.first().json;\nconst leads = body.leads || [];\nreturn leads.map(lead => ({ json: lead }));"
      }
    },
    {
      "id": "56aeb139-fe98-4ebd-be2c-b16b2ce61c95",
      "name": "Note: Apollo Key",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        840,
        -120
      ],
      "parameters": {
        "content": "\u26a0\ufe0f Replace APOLLO_KEY_PLACEHOLDER with real Apollo API key in the HTTP Request node below.",
        "height": 100,
        "width": 360,
        "color": 4
      }
    },
    {
      "id": "dffbb6cb-0d0b-402a-99e2-7cc0e525e369",
      "name": "HTTP Request: Apollo Lookup",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        840,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.apollo.io/v1/people/search",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "q_organization_name": "={{ $json.school_name }}",
          "person_titles": [
            "Director",
            "Administrator",
            "Owner",
            "Principal"
          ],
          "organization_locations": [
            "California"
          ],
          "api_key": "APOLLO_KEY_PLACEHOLDER"
        },
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "05fc0daf-60d0-47c4-82cd-c584075d1ba7",
      "name": "Code: Parse Apollo",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        0
      ],
      "parameters": {
        "jsCode": "const data=$json;\nconst people=data.people||[];\nconst best=people.find(p=>p.email)||people[0]||{};\nlet lead={}; try{ lead=$('Code: Extract Leads').item.json; }catch(e){ lead={}; }\nreturn { json:{\n  school_name:lead.school_name||'',\n  citation_number:lead.citation_number||'',\n  citation_type:lead.citation_type||'',\n  state:lead.state||'CA',\n  contact_name:((best.first_name||'')+' '+(best.last_name||'')).trim(),\n  contact_email:best.email||'',\n  contact_title:best.title||'',\n  contact_linkedin:best.linkedin_url||'',\n  organization:best.organization_name||lead.school_name||''\n}};",
        "mode": "runOnceForEachItem"
      }
    },
    {
      "id": "cc47c38f-ec62-4df2-9414-0c2f0f5d529d",
      "name": "AI: Categorize Lead",
      "type": "@n8n/n8n-nodes-langchain.anthropic",
      "typeVersion": 1,
      "position": [
        1400,
        0
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "claude-sonnet-4-5-20250929",
          "mode": "list",
          "cachedResultName": "claude-sonnet-4-5-20250929"
        },
        "messages": {
          "values": [
            {
              "content": "=Return ONLY valid JSON (no markdown, no code fences) with keys: use_case (one of compliance|enrollment|operations), cta_variant (integer 1-4), custom_hook (one sentence, specific to the citation), email_subject, email_preview.\n\nSchool: {{ $json.school_name }}\nContact: {{ $json.contact_name }} ({{ $json.contact_title }})\nState: {{ $json.state }}\nCitation type: {{ $json.citation_type }}\nCitation #: {{ $json.citation_number }}\n\nPick use_case: compliance for record-keeping/reporting/audit violations; enrollment for admissions/enrollment-agreement/refund issues; operations otherwise."
            }
          ]
        },
        "options": {
          "system": "You are Bella AI's outbound sales strategist. Bella is an AI-first Student Information System for NACCAS-accredited beauty/barber/cosmetology schools. Be concise and specific."
        }
      },
      "retryOnFail": true,
      "maxTries": 4,
      "waitBetweenTries": 3000,
      "onError": "continueRegularOutput"
    },
    {
      "id": "8e8ccf6d-5066-4daa-86e8-e7e0d43513fe",
      "name": "Code: Build Enriched Lead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1680,
        0
      ],
      "parameters": {
        "jsCode": "const out=$json;\nlet raw='';\nif(typeof out.text==='string') raw=out.text;\nelse if(Array.isArray(out.content)) raw=out.content.map(c=>c.text||'').join('');\nelse if(out.message&&Array.isArray(out.message.content)) raw=out.message.content.map(c=>c.text||'').join('');\nelse if(typeof out.content==='string') raw=out.content;\nraw=raw.replace(/```json/gi,'').replace(/```/g,'').trim();\nlet ai={}; try{ ai=JSON.parse(raw); }catch(e){ ai={}; }\nconst lead=$('Code: Parse Apollo').item.json;\nconst ct=(lead.citation_type||'').toLowerCase();\nfunction huc(){ if(/record|report|audit|attendance|clock|file|document/.test(ct)) return 'compliance'; if(/enroll|admission|refund|agreement|disclosure|tuition/.test(ct)) return 'enrollment'; return 'operations'; }\nconst useCase=['compliance','enrollment','operations'].includes(ai.use_case)?ai.use_case:huc();\nlet cta=parseInt(ai.cta_variant,10); if(!(cta>=1&&cta<=4)) cta=(useCase==='compliance'?2:useCase==='enrollment'?1:4);\nconst slug=(lead.school_name||'school').toLowerCase().replace(/[^a-z0-9\\s-]/g,'').trim().replace(/\\s+/g,'-').replace(/-+/g,'-').substring(0,60);\nreturn { json:{ ...lead, use_case:useCase, cta_variant:cta,\n  custom_hook: ai.custom_hook || ('Your BPPE citation for '+(lead.citation_type||'compliance')+' is exactly what Bella was built to eliminate.'),\n  email_subject: ai.email_subject || ('Bella can resolve your BPPE citation, '+(lead.school_name||'')),\n  email_preview: ai.email_preview||'',\n  ai_categorized: !!ai.use_case, slug, enriched_at:new Date().toISOString() }};",
        "mode": "runOnceForEachItem"
      }
    },
    {
      "id": "ccec209f-da1c-4fb6-9aed-757b50b70f9a",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1960,
        0
      ],
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "enriched_leads"
      }
    },
    {
      "id": "23a68d5b-cca1-4086-aec2-40f0e3cdbaf4",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        2240,
        0
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { success: true, enriched_leads: $json.enriched_leads } }}"
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Code: Extract Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Extract Leads": {
      "main": [
        [
          {
            "node": "HTTP Request: Apollo Lookup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request: Apollo Lookup": {
      "main": [
        [
          {
            "node": "Code: Parse Apollo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Parse Apollo": {
      "main": [
        [
          {
            "node": "AI: Categorize Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI: Categorize Lead": {
      "main": [
        [
          {
            "node": "Code: Build Enriched Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Build Enriched Lead": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}