{
  "name": "WF-FOLLOWUP-EMAIL: 24hr + Day 5 Follow-Up",
  "nodes": [
    {
      "id": "schedule",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 2
            }
          ]
        }
      }
    },
    {
      "id": "queryDemoSent",
      "name": "Query Leads Needing Follow-Up",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "=https://api.close.com/api/v1/lead/?query=status_id:\"stat_JkhhQYXcbMQXXixqSBlmq35po9YWwgD0cn2XnNXP8z2\" OR status_id:\"stat_YRcuGl0533SzjVUwsZOjlOpjDoR25szAYhqEgj8V5nE\"&_fields=id,display_name,contacts,custom,status_id&_limit=50",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "filterLeads",
      "name": "Filter and Classify Leads",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "jsCode": "var leads=$json.data||[]; var now=new Date(); var results=[]; for(var i=0;i<leads.length;i++){ var lead=leads[i]; var custom=lead.custom||{}; var followupField=custom['cf_wijygdI8p4OqsMRsMtIZWCXrgI9NMWeMwrfy4scYNax']||''; var contact=(lead.contacts&&lead.contacts[0])||{}; var emails=contact.emails||[]; var email=''; for(var e=0;e<emails.length;e++){ if(emails[e].email){ email=emails[e].email; break; } } if(!email) continue; var firstName=(contact.first_name||lead.display_name||'').split(' ')[0]||'there'; if(followupField===''){ results.push({ json:{ leadId:lead.id, contactId:contact.id||'', firstName:firstName, email:email, followupType:'stamp_first_seen' } }); } else if(followupField.indexOf('first_seen:')===0){ var firstSeenTs=parseInt(followupField.split(':')[1]); var hrs=(now.getTime()-firstSeenTs)/(1000*60*60); if(hrs>=24 && hrs<120){ results.push({ json:{ leadId:lead.id, contactId:contact.id||'', firstName:firstName, email:email, followupType:'24hr' } }); } } else if(followupField.indexOf('24hr_sent:')===0){ var sentTs=parseInt(followupField.split(':')[1]); var days=(now.getTime()-sentTs)/(1000*60*60*24); if(days>=4){ results.push({ json:{ leadId:lead.id, contactId:contact.id||'', firstName:firstName, email:email, followupType:'day5' } }); } } } if(results.length===0){ results.push({ json:{ noLeads:true } }); } return results;"
      }
    },
    {
      "id": "hasLeads",
      "name": "Has Leads?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        660,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "leftValue": "={{ $json.noLeads }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "notEquals"
              },
              "id": "condition-1773266875174-b1rhay0e6"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "routeType",
      "name": "Route Followup Type",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        880,
        200
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.followupType }}",
                    "rightValue": "24hr",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "24hr"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.followupType }}",
                    "rightValue": "day5",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Day 5"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.followupType }}",
                    "rightValue": "stamp_first_seen",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Stamp"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "send24hr",
      "name": "Send 24hr Follow-Up Email",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        100
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/email/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $json.leadId }}\",\n  \"to\": [\"{{ $json.email }}\"],\n  \"sender\": \"user@example.com\",\n  \"subject\": \"Did you get a chance to watch?\",\n  \"body_text\": \"Hey {{ $json.firstName }}, following up on the walkthrough: https://www.voicesdr.ai/see-how-it-works . Ready? https://www.voicesdr.ai/pricing . Talk soon, Laura, VoiceSDR\",\n  \"status\": \"outbox\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "mark24hr",
      "name": "Mark 24hr Sent",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        100
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $json.lead_id }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"custom.cf_wijygdI8p4OqsMRsMtIZWCXrgI9NMWeMwrfy4scYNax\": \"24hr_sent:{{ Date.now() }}\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "sendDay5",
      "name": "Send Day 5 Final Email",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/email/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $json.leadId }}\",\n  \"to\": [\"{{ $json.email }}\"],\n  \"sender\": \"user@example.com\",\n  \"subject\": \"Last note from me, {{ $json.firstName }}\",\n  \"body_text\": \"Hey {{ $json.firstName }}, I will keep this short. VoiceSDR answers every call. Video: https://www.voicesdr.ai/see-how-it-works . I will not bug you again. Best, Laura, VoiceSDR\",\n  \"status\": \"outbox\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "moveNurture",
      "name": "Move to Nurture",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        300
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $json.lead_id }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"status_id\": \"stat_0vucAu8aZ2V7ZPjef1fPdEToaJQXABBju5uqcZ3e5VK\",\n  \"custom.cf_wijygdI8p4OqsMRsMtIZWCXrgI9NMWeMwrfy4scYNax\": \"day5_final_sent\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "stampFirstSeen",
      "name": "Stamp First Seen",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        500
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $json.leadId }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"custom.cf_wijygdI8p4OqsMRsMtIZWCXrgI9NMWeMwrfy4scYNax\": \"first_seen:{{ Date.now() }}\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Query Leads Needing Follow-Up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Query Leads Needing Follow-Up": {
      "main": [
        [
          {
            "node": "Filter and Classify Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter and Classify Leads": {
      "main": [
        [
          {
            "node": "Has Leads?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Leads?": {
      "main": [
        [
          {
            "node": "Route Followup Type",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Route Followup Type": {
      "main": [
        [
          {
            "node": "Send 24hr Follow-Up Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Day 5 Final Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stamp First Seen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send 24hr Follow-Up Email": {
      "main": [
        [
          {
            "node": "Mark 24hr Sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Day 5 Final Email": {
      "main": [
        [
          {
            "node": "Move to Nurture",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false
}