Skip to content
// cancel_appointment({ booking_id } or { phone, date }) → cancellation
const db = $getWorkflowStaticData('global');
db.bookings = db.bookings ?? {};

return $input.all().map(({ json }) => {
  const a = json.args;
  const id = String(a.booking_id ?? '').replace(/\s/g, '').toUpperCase();
  const phone = String(a.phone ?? '').replace(/[^\d+]/g, '');
  const date = String(a.date ?? '').trim();

  const match = db.bookings[id] ?? Object.values(db.bookings).find((b) => phone && b.phone === phone && b.date === date);
  let result;
  if (!match) {
    result = { ok: false, message: "I couldn't find that booking. Could you give me your booking number, or the phone number and date you booked?" };
  } else {
    delete db.bookings[match.id];
    result = { ok: true, booking_id: match.id, message: `Done. Your appointment on ${DateTime.fromISO(match.date).toFormat('cccc, LLLL d')} is cancelled.` };
  }
  return { json: { ...json, result } };
});