{
  "name": "04d — Outreach Touch 4 (Day +14, soft close)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "15 9 * * 1-5"
            }
          ]
        }
      },
      "id": "wf04d-0001",
      "name": "Weekdays 09:15",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT l.*, lc.id as contact_id, lc.full_name, lc.role_title, lc.email,\n       es1.sent_at as first_sent_at\nFROM leads.leads l\nJOIN leads.lead_contacts lc ON lc.lead_id = l.id AND lc.is_primary = true\nJOIN leads.email_sends es1 ON es1.lead_id = l.id AND es1.touch_number = 1\nWHERE l.state = 'contacted'\n  AND es1.sent_at <= NOW() - INTERVAL '14 days'\n  AND NOT EXISTS (\n    SELECT 1 FROM leads.email_sends es4 WHERE es4.lead_id = l.id AND es4.touch_number = 4\n  )\nORDER BY es1.sent_at ASC LIMIT 10",
        "additionalFields": {}
      },
      "id": "wf04d-0002",
      "name": "Fetch Touch 4 Ready Leads",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "wf04d-0003",
      "name": "Split Leads",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const l = $json;\nconst ra = l.research_artifact || {};\nconst calUrl = $env.CAL_DISCOVERY_URL || 'https://calendly.com/YOUR-LINK';\nconst senderName = $env.SENDER_NAME || 'Artenis';\n\nconst workflow = ra.the_specific_workflow || 'this kind of workflow';\nconst companyName = l.company_name || 'your company';\n\nconst subject = 'One last note, ' + (l.full_name ? l.full_name.split(' ')[0] : companyName);\n\nconst body = 'Last note from my end. If automating ' + workflow + ' is ever worth a 20-minute conversation, happy to walk through what it typically looks like for a business your size.\\n\\n' + calUrl + '\\n\\nEither way, good luck with everything.';\n\nreturn [{ json: { ...l, generated_subject: subject, generated_body: body } }];"
      },
      "id": "wf04d-0004",
      "name": "Build Soft Close (Fixed Template)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.email }}",
        "subject": "={{ $json.generated_subject }}",
        "emailType": "text",
        "message": "={{ $json.final_body }}",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "wf04d-0005",
      "name": "Send via Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO leads.email_sends (lead_id, contact_id, touch_number, subject, body, sent_at) VALUES ($1, $2, 4, $3, $4, NOW())",
        "additionalFields": {
          "queryParams": "={{ JSON.stringify([$json.id, $json.contact_id, $json.generated_subject, $json.final_body]) }}"
        }
      },
      "id": "wf04d-0006",
      "name": "Insert email_sends",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE leads.leads SET state = 'closed_no_response', updated_at = NOW() WHERE id = $1",
        "additionalFields": {
          "queryParams": "={{ JSON.stringify([$json.id]) }}"
        }
      },
      "id": "wf04d-0007",
      "name": "Update Lead: Closed No Response",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1560,
        300
      ]
    }
  ],
  "connections": {
    "Weekdays 09:15": {
      "main": [
        [
          {
            "node": "Fetch Touch 4 Ready Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Touch 4 Ready Leads": {
      "main": [
        [
          {
            "node": "Split Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Leads": {
      "main": [
        [],
        [
          {
            "node": "Build Soft Close (Fixed Template)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Soft Close (Fixed Template)": {
      "main": [
        [
          {
            "node": "Send via Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send via Gmail": {
      "main": [
        [
          {
            "node": "Insert email_sends",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert email_sends": {
      "main": [
        [
          {
            "node": "Update Lead: Closed No Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead: Closed No Response": {
      "main": [
        [
          {
            "node": "Split Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
