{
  "name": "Trevilo: enquiry to the right department",
  "nodes": [
    {
      "parameters": {
        "content": "## Trevilo: enquiry to the right department\n\n1. Open **Trevilo enquiry**, copy the **Production URL**.\n2. In **Trevilo enquiry**, create a **Header Auth** credential: name `X-Webhook-Key`, value a long random key.\n3. In Trevilo, add one rule per department, each sending to an n8n connection with the Production URL plus `?department=sales` (or accounts, support, your own names). Set each connection's login to **API key in a header**, header name `X-Webhook-Key`, and the same key.\n4. In **Pick the owner**, edit **Department owners**: one line per department name with that department's HubSpot owner ID, and **Needs sorting owner** for anything else.\n5. In both HubSpot steps, create a **HubSpot App Token** credential from a HubSpot private app.\n6. Publish, then send a test enquiry for each department.",
        "height": 420,
        "width": 460
      },
      "id": "63bee3e3-efd6-5ed8-8cf6-8ff8fdfee35f",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -560,
        -280
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "trevilo-department",
        "authentication": "headerAuth",
        "options": {}
      },
      "id": "93c78916-5270-514c-aa66-288abbcd28d1",
      "name": "Trevilo enquiry",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.2,
      "position": [
        0,
        0
      ],
      "webhookId": "a566e90b-124c-5f54-858b-35307637b269"
    },
    {
      "parameters": {
        "operation": "removeItemsSeenInPreviousExecutions",
        "dedupeValue": "={{ $('Trevilo enquiry').item.json.headers['idempotency-key'] }}",
        "options": {}
      },
      "id": "1f857dfc-d28c-5a4c-a014-94ae7e2e55b9",
      "name": "Skip repeat deliveries",
      "type": "n8n-nodes-base.removeDuplicates",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "17a6ec67-5f7e-5d0e-882e-10da8c853cca",
              "name": "departmentOwners",
              "type": "object",
              "value": "={\"sales\": \"REPLACE_WITH_OWNER_ID\", \"accounts\": \"REPLACE_WITH_OWNER_ID\", \"support\": \"REPLACE_WITH_OWNER_ID\"}"
            },
            {
              "id": "b76c3857-4909-5d3c-90a6-cb9acd1b1c4a",
              "name": "needsSortingOwner",
              "type": "string",
              "value": "REPLACE_WITH_OWNER_ID"
            },
            {
              "id": "c58d8521-f028-521a-b55d-0aa9199d126a",
              "name": "department",
              "type": "string",
              "value": "={{ String($('Trevilo enquiry').item.json.query?.department || '').trim().toLowerCase() }}"
            }
          ]
        },
        "options": {}
      },
      "id": "298c23ed-821d-52d3-8170-cba0020da020",
      "name": "Pick the owner",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "e4c84699-3354-5bf4-8bc7-c97521fbd5f5",
              "leftValue": "={{ Object.keys($json.departmentOwners || {}).includes($json.department) }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "5c6d9ca0-2722-5651-92a9-6d3e90f74d20",
      "name": "Known department?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c412c24a-6f2c-5fd9-a30d-262c07b1baa2",
              "name": "ownerId",
              "type": "string",
              "value": "={{ $json.departmentOwners[$json.department] }}"
            },
            {
              "id": "7c83bfca-1003-5f11-8077-af732a1e6666",
              "name": "label",
              "type": "string",
              "value": "={{ $json.department.charAt(0).toUpperCase() + $json.department.slice(1) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "1832a5cd-4118-5c4c-8aa9-47ec2117c22e",
      "name": "Use the department owner",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        880,
        -120
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e3f9e1db-a360-5aed-8c1d-a3b3d84550e8",
              "name": "ownerId",
              "type": "string",
              "value": "={{ $json.needsSortingOwner }}"
            },
            {
              "id": "dac16c1c-ff56-504e-afd8-4376918b1c46",
              "name": "label",
              "type": "string",
              "value": "Needs sorting"
            }
          ]
        },
        "options": {}
      },
      "id": "beae77e7-7aa6-576e-b624-1801b399cdcc",
      "name": "Use the needs sorting owner",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        880,
        120
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "c95424f4-1915-5738-a581-ddf66f2ec834",
              "leftValue": "={{ Boolean($('Trevilo enquiry').item.json.body.contact?.email) }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "69bc8661-7b5d-5933-99ff-8a0c666acdd4",
      "name": "Has an email?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1100,
        0
      ]
    },
    {
      "parameters": {
        "authentication": "appToken",
        "resource": "contact",
        "operation": "upsert",
        "email": "={{ $('Trevilo enquiry').item.json.body.contact.email }}",
        "additionalFields": {
          "firstName": "={{ ($('Trevilo enquiry').item.json.body.enquiry?.name || '').split(' ')[0] }}",
          "lastName": "={{ ($('Trevilo enquiry').item.json.body.enquiry?.name || '').split(' ').slice(1).join(' ') }}",
          "phoneNumber": "={{ $('Trevilo enquiry').item.json.body.contact?.phone || '' }}",
          "message": "={{ $('Trevilo enquiry').item.json.body.enquiry?.message || '' }}",
          "contactOwner": "={{ $('Has an email?').item.json.ownerId }}"
        },
        "options": {}
      },
      "id": "74bf5843-15ef-5e38-83a0-4ec2f25bb2af",
      "name": "Create or update contact",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2.2,
      "position": [
        1320,
        -100
      ]
    },
    {
      "parameters": {
        "authentication": "appToken",
        "resource": "engagement",
        "type": "task",
        "dueDate": "={{ $now.plus({ days: 1 }).toISO() }}",
        "metadata": {
          "subject": "={{ $('Has an email?').item.json.label }}: {{ $('Trevilo enquiry').item.json.body.enquiry?.name || $('Trevilo enquiry').item.json.body.contact.email }}",
          "body": "={{ $('Trevilo enquiry').item.json.body.enquiry?.message || '' }}\n\nPage: {{ $('Trevilo enquiry').item.json.body.source?.url || '' }}",
          "status": "NOT_STARTED",
          "forObjectType": "CONTACT"
        },
        "additionalFields": {
          "associations": {
            "contactIds": "={{ $json.vid }}",
            "ownerId": "={{ Number($('Has an email?').item.json.ownerId) }}"
          }
        }
      },
      "id": "2d00e9f3-a8f4-5e6d-bccd-acba75c14765",
      "name": "Create task",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2.2,
      "position": [
        1540,
        -100
      ]
    }
  ],
  "connections": {
    "Trevilo enquiry": {
      "main": [
        [
          {
            "node": "Skip repeat deliveries",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Skip repeat deliveries": {
      "main": [
        [
          {
            "node": "Pick the owner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pick the owner": {
      "main": [
        [
          {
            "node": "Known department?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Known department?": {
      "main": [
        [
          {
            "node": "Use the department owner",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Use the needs sorting owner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Use the department owner": {
      "main": [
        [
          {
            "node": "Has an email?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Use the needs sorting owner": {
      "main": [
        [
          {
            "node": "Has an email?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has an email?": {
      "main": [
        [
          {
            "node": "Create or update contact",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Create or update contact": {
      "main": [
        [
          {
            "node": "Create task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
