{
  "name": "Sales CRM - Email Events + GHL",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "sales-dashboard/email-event",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-event-1",
      "name": "Webhook - Email Event",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "lookup-email",
              "name": "lookup_email",
              "value": "={{ $json.body.event_type === 'email.received' ? $json.body.sender_email : $json.body.recipient_email }}",
              "type": "string"
            },
            {
              "id": "is-reply",
              "name": "is_reply",
              "value": "={{ $json.body.event_type === 'email.received' }}",
              "type": "boolean"
            },
            {
              "id": "event-type",
              "name": "event_type",
              "value": "={{ $json.body.event_type }}",
              "type": "string"
            },
            {
              "id": "pipeline-status",
              "name": "pipeline_status",
              "value": "={{ $json.body.pipeline_status || '' }}",
              "type": "string"
            },
            {
              "id": "delivery-status",
              "name": "delivery_status",
              "value": "={{ $json.body.delivery_status || '' }}",
              "type": "string"
            },
            {
              "id": "email-opened-at",
              "name": "email_opened_at",
              "value": "={{ $json.body.email_opened_at || '' }}",
              "type": "string"
            },
            {
              "id": "reply-received-at",
              "name": "reply_received_at",
              "value": "={{ $json.body.reply_received_at || '' }}",
              "type": "string"
            },
            {
              "id": "bounced-at",
              "name": "bounced_at",
              "value": "={{ $json.body.bounced_at || '' }}",
              "type": "string"
            },
            {
              "id": "bounce-reason",
              "name": "bounce_reason",
              "value": "={{ $json.body.bounce_reason || '' }}",
              "type": "string"
            },
            {
              "id": "reply-subject",
              "name": "reply_subject",
              "value": "={{ $json.body.subject || '' }}",
              "type": "string"
            },
            {
              "id": "reply-body",
              "name": "reply_body",
              "value": "={{ $json.body.body_text || '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "set-fields-1",
      "name": "Set Fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "operation": "search",
        "documentId": {
          "__rl": true,
          "value": "1DEmE-3aiDhQlMNcFct4KbUQDOnNBJRUo-vPYhw6VpUY",
          "mode": "list",
          "cachedResultName": "Lead Gen Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=924549057",
          "mode": "list",
          "cachedResultName": "enriched_leads"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "email",
              "lookupValue": "={{ $json.lookup_email }}"
            }
          ]
        },
        "options": {}
      },
      "id": "sheets-find-1",
      "name": "Find Lead by Email",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        640,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "has-results",
              "leftValue": "={{ $json.lead_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "isNotEmpty"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-found-1",
      "name": "Lead Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        840,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "1DEmE-3aiDhQlMNcFct4KbUQDOnNBJRUo-vPYhw6VpUY",
          "mode": "list",
          "cachedResultName": "Lead Gen Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=924549057",
          "mode": "list",
          "cachedResultName": "enriched_leads"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "lead_id": "={{ $('Find Lead by Email').item.json.lead_id }}",
            "pipeline_status": "={{ $('Set Fields').item.json.pipeline_status || $('Find Lead by Email').item.json.pipeline_status }}",
            "delivery_status": "={{ $('Set Fields').item.json.delivery_status || $('Find Lead by Email').item.json.delivery_status }}",
            "email_opened_at": "={{ $('Set Fields').item.json.email_opened_at || $('Find Lead by Email').item.json.email_opened_at }}",
            "reply_received_at": "={{ $('Set Fields').item.json.reply_received_at || $('Find Lead by Email').item.json.reply_received_at }}",
            "bounced_at": "={{ $('Set Fields').item.json.bounced_at || $('Find Lead by Email').item.json.bounced_at }}",
            "bounce_reason": "={{ $('Set Fields').item.json.bounce_reason || $('Find Lead by Email').item.json.bounce_reason }}"
          },
          "matchingColumns": [
            "lead_id"
          ],
          "schema": [
            {
              "id": "lead_id",
              "displayName": "lead_id",
              "required": false,
              "defaultMatch": true,
              "canBeUsedToMatch": true,
              "type": "string"
            },
            {
              "id": "pipeline_status",
              "displayName": "pipeline_status",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "type": "string"
            },
            {
              "id": "delivery_status",
              "displayName": "delivery_status",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "type": "string"
            },
            {
              "id": "email_opened_at",
              "displayName": "email_opened_at",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "type": "string"
            },
            {
              "id": "reply_received_at",
              "displayName": "reply_received_at",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "type": "string"
            },
            {
              "id": "bounced_at",
              "displayName": "bounced_at",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "type": "string"
            },
            {
              "id": "bounce_reason",
              "displayName": "bounce_reason",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "sheets-update-1",
      "name": "Update Lead",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1040,
        200
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-reply-check",
              "leftValue": "={{ $('Set Fields').item.json.is_reply }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-reply-1",
      "name": "Is Reply?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1260,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://rest.gohighlevel.com/v1/contacts/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"email\": \"{{ $('Find Lead by Email').item.json.email }}\",\n  \"name\": \"{{ $('Find Lead by Email').item.json.name }}\",\n  \"phone\": \"{{ $('Find Lead by Email').item.json.phone || '' }}\",\n  \"companyName\": \"{{ $('Find Lead by Email').item.json.business_name || '' }}\",\n  \"website\": \"{{ $('Find Lead by Email').item.json.website || '' }}\",\n  \"source\": \"Cold Email Reply\",\n  \"tags\": [\"cold-email-reply\", \"sales-crm\"],\n  \"customField\": {\n    \"reply_subject\": \"{{ $('Set Fields').item.json.reply_subject }}\",\n    \"reply_body\": \"{{ $('Set Fields').item.json.reply_body }}\",\n    \"original_lead_id\": \"{{ $('Find Lead by Email').item.json.lead_id }}\",\n    \"icp_score\": \"{{ $('Find Lead by Email').item.json.icp_score || '' }}\",\n    \"intent_score\": \"{{ $('Find Lead by Email').item.json.intent_score || '' }}\",\n    \"total_score\": \"{{ $('Find Lead by Email').item.json.total_score || '' }}\"\n  }\n}",
        "options": {}
      },
      "id": "ghl-create-1",
      "name": "Push to GHL",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1480,
        100
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, message: 'Event processed', event_type: $('Set Fields').item.json.event_type, pushed_to_ghl: $('Set Fields').item.json.is_reply }) }}"
      },
      "id": "respond-success-1",
      "name": "Respond Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1700,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: false, message: 'Lead not found', lookup_email: $('Set Fields').item.json.lookup_email }) }}"
      },
      "id": "respond-not-found-1",
      "name": "Respond Not Found",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1040,
        400
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, message: 'Event processed - no GHL push', event_type: $('Set Fields').item.json.event_type }) }}"
      },
      "id": "respond-no-ghl-1",
      "name": "Respond No GHL",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1480,
        300
      ]
    }
  ],
  "connections": {
    "Webhook - Email Event": {
      "main": [
        [
          {
            "node": "Set Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Fields": {
      "main": [
        [
          {
            "node": "Find Lead by Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Lead by Email": {
      "main": [
        [
          {
            "node": "Lead Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lead Found?": {
      "main": [
        [
          {
            "node": "Update Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Not Found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead": {
      "main": [
        [
          {
            "node": "Is Reply?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Reply?": {
      "main": [
        [
          {
            "node": "Push to GHL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond No GHL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Push to GHL": {
      "main": [
        [
          {
            "node": "Respond Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [],
  "meta": {
    "templateCredsSetupCompleted": false
  }
}