Real Estate Lead Nurture
Module 19 · business · intermediate
stop.js
// Stop the drip when a lead replies, books, or opts out.
const b = $('Stop Lead API').first().json.body ?? {};
const db = $getWorkflowStaticData('global');
db.leads = db.leads ?? {};
let phone = String(b.phone ?? '').replace(/[^\d+]/g, '');
if (/^\d{10}$/.test(phone)) phone = '+1' + phone;
const key = String(b.email ?? '').trim().toLowerCase() || phone;
const lead = db.leads[key];
if (!lead) return [{ json: { found: false, key } }];
lead.status = 'stopped';
lead.stop_reason = String(b.reason ?? 'replied');
lead.stopped_at = DateTime.now().toISO();
return [{ json: { found: true, key, status: lead.status, stop_reason: lead.stop_reason, pending_steps_cancelled: lead.drip.filter((d) => !d.sent_at).map((d) => d.step) } }];