Skip to content
// lookup_customer({ phone }) → known customer + upcoming bookings
// Demo customer list; the Business version reads a CRM / Google Sheet.
const customers = {
  '+15551230001': { name: 'Maria Santos', last_visit: '2026-08-12', notes: 'Prefers mornings' },
  '+15551230002': { name: 'Sam Lee', last_visit: '2026-09-30', notes: 'Due for a cleaning in March' },
};
const db = $getWorkflowStaticData('global');
db.bookings = db.bookings ?? {};

return $input.all().map(({ json }) => {
  let phone = String(json.args.phone ?? '').replace(/[^\d+]/g, '');
  if (/^\d{10}$/.test(phone)) phone = '+1' + phone;
  const c = customers[phone];
  const today = DateTime.now().toISODate();
  const upcoming = Object.values(db.bookings).filter((b) => b.phone.replace(/^(\d{10})$/, '+1$1') === phone && b.date >= today);
  const result = c
    ? {
        ok: true, known: true, name: c.name, last_visit: c.last_visit, notes: c.notes, upcoming,
        message: `Welcome back, ${c.name.split(' ')[0]}.` + (upcoming.length ? ` You have ${upcoming.length} upcoming appointment${upcoming.length > 1 ? 's' : ''}.` : ''),
      }
    : { ok: true, known: false, upcoming, message: "I don't have that number on file yet, which is no problem. May I have your name?" };
  return { json: { ...json, result } };
});