{
  "name": "Territory & Company Size Lead Routing — HubSpot → Slack",
  "nodes": [
    {
      "parameters": { "eventsUi": { "eventValues": [{ "name": "contact.creation" }] } },
      "id": "hubspot-trigger", "name": "HubSpot Trigger",
      "type": "n8n-nodes-base.hubspotTrigger", "typeVersion": 1, "position": [0, 0],
      "webhookId": "hubspot-trigger-id",
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts/{{ $json.objectId }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "properties", "value": "firstname,lastname,email,jobtitle,company,state,country,hubspot_owner_id" }, { "name": "associations", "value": "companies" }] },
        "options": {}
      },
      "id": "fetch-contact", "name": "Fetch Contact",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [220, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.hubapi.com/crm/v3/objects/companies/{{ $json.associations.companies.results[0].id }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "properties", "value": "name,numberofemployees,state,country,hubspot_owner_id" }] },
        "options": {}
      },
      "id": "fetch-company", "name": "Fetch Company",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [440, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "conditions": { "conditions": [{ "leftValue": "={{ $json.properties.hubspot_owner_id }}", "rightValue": "", "operator": { "type": "string", "operation": "notEquals" } }], "combinator": "and" }
      },
      "id": "has-owner", "name": "Company Has Owner?",
      "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [660, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const contact = $('Fetch Contact').first().json;\nconst company = $('Fetch Company').first().json;\n\nconst state = (company?.properties?.state || contact.properties.state || '').toUpperCase();\nconst country = (company?.properties?.country || contact.properties.country || '').toUpperCase();\nconst employees = parseInt(company?.properties?.numberofemployees || '0');\n\nconst TERRITORY_MAP = {\n  'NY': { ownerId: '111111', slackId: 'U01AAAA', rep: 'Alice' },\n  'MA': { ownerId: '111111', slackId: 'U01AAAA', rep: 'Alice' },\n  'CT': { ownerId: '111111', slackId: 'U01AAAA', rep: 'Alice' },\n  'NJ': { ownerId: '111111', slackId: 'U01AAAA', rep: 'Alice' },\n  'CA': { ownerId: '222222', slackId: 'U02BBBB', rep: 'Bob' },\n  'WA': { ownerId: '222222', slackId: 'U02BBBB', rep: 'Bob' },\n  'OR': { ownerId: '222222', slackId: 'U02BBBB', rep: 'Bob' },\n  'FL': { ownerId: '333333', slackId: 'U03CCCC', rep: 'Carol' },\n  'GA': { ownerId: '333333', slackId: 'U03CCCC', rep: 'Carol' },\n  'TX': { ownerId: '333333', slackId: 'U03CCCC', rep: 'Carol' },\n};\n\nconst ENTERPRISE_REP = { ownerId: '444444', slackId: 'U04DDDD', rep: 'Dave (Enterprise)' };\nconst ENTERPRISE_THRESHOLD = 1000;\nconst DEFAULT_REP = { ownerId: '555555', slackId: 'U05EEEE', rep: 'Eve (Catch-all)' };\n\nlet assignedRep;\nlet reason;\n\nif (employees >= ENTERPRISE_THRESHOLD) {\n  assignedRep = ENTERPRISE_REP;\n  reason = `Enterprise (${employees} employees)`;\n} else if (TERRITORY_MAP[state]) {\n  assignedRep = TERRITORY_MAP[state];\n  reason = `Territory match: ${state}`;\n} else {\n  assignedRep = DEFAULT_REP;\n  reason = `No territory match (state: ${state || 'unknown'})`;\n}\n\nreturn [{\n  json: {\n    contactId: contact.id,\n    contactName: `${contact.properties.firstname || ''} ${contact.properties.lastname || ''}`.trim(),\n    email: contact.properties.email,\n    company: company?.properties?.name || contact.properties.company,\n    employees,\n    state,\n    assignedRep,\n    reason,\n  }\n}];"
      },
      "id": "territory-rules", "name": "Apply Territory Rules",
      "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [880, 0]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts/{{ $json.contactId }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "hubspotApi",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "={ \"properties\": { \"hubspot_owner_id\": \"{{ $json.assignedRep.ownerId }}\" } }",
        "options": {}
      },
      "id": "update-owner", "name": "Update Owner",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [1100, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": { "__rl": true, "value": "={{ $json.assignedRep.slackId }}", "mode": "id" },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \"🆕 *New Lead Routed to You*\\n*\" + $json.contactName + \"* at \" + $json.company + \" (\" + $json.employees + \" employees)\\n📍 \" + ($json.state || 'Unknown location') + \"\\nRouting reason: \" + $json.reason\n      }\n    },\n    {\n      \"type\": \"actions\",\n      \"elements\": [{ \"type\": \"button\", \"text\": { \"type\": \"plain_text\", \"text\": \"View in HubSpot\" }, \"url\": \"https://app.hubspot.com/contacts/YOUR_PORTAL_ID/contact/\" + $json.contactId }]\n    }\n  ]\n}",
        "otherOptions": { "unfurl_links": false }
      },
      "id": "slack-notify", "name": "Notify Rep",
      "type": "n8n-nodes-base.slack", "typeVersion": 2.3, "position": [1320, 0],
      "credentials": { "slackApi": { "id": "credential-id", "name": "Slack API" } }
    }
  ],
  "connections": {
    "HubSpot Trigger": { "main": [[{ "node": "Fetch Contact", "type": "main", "index": 0 }]] },
    "Fetch Contact": { "main": [[{ "node": "Fetch Company", "type": "main", "index": 0 }]] },
    "Fetch Company": { "main": [[{ "node": "Company Has Owner?", "type": "main", "index": 0 }]] },
    "Company Has Owner?": { "main": [[], [{ "node": "Apply Territory Rules", "type": "main", "index": 0 }]] },
    "Apply Territory Rules": { "main": [[{ "node": "Update Owner", "type": "main", "index": 0 }]] },
    "Update Owner": { "main": [[{ "node": "Notify Rep", "type": "main", "index": 0 }]] }
  },
  "pinData": {},
  "settings": { "executionOrder": "v1" },
  "staticData": null, "tags": [], "triggerCount": 0, "active": false,
  "meta": { "instanceId": "", "templateId": "revi-territory-routing" }
}
