Skip to content
// Cancel a booking (or a pending offer): frees the slot and the holds. A Google event, if any, is removed by hand (Exercise 3).
const b = $('Cancel API').first().json.body ?? {};
const db = $getWorkflowStaticData('global');
const lead = (db.leads ?? {})[String(b.lead_id ?? '')];
if (!lead) return [{ json: { found: false, lead_id: b.lead_id ?? null } }];
const freed = [];
for (const [k, v] of Object.entries(db.bookings ?? {})) if (v === lead.id) { delete db.bookings[k]; freed.push(k); }
for (const [k, h] of Object.entries(db.holds ?? {})) if (h.lead_id === lead.id) delete db.holds[k];
const was = lead.status;
lead.status = 'cancelled'; lead.offered = []; lead.cancelled_at = new Date().toISOString(); lead.cancel_reason = String(b.reason ?? '');
return [{ json: { found: true, lead_id: lead.id, previous_status: was, status: 'cancelled', freed_slots: freed, event_id: lead.booking?.event_id ?? null } }];