feat(messaging): conflict check before booking appointment

Check for duplicate patient+doctor+date and max 3 slots per time before
creating the appointment. Returns actionable message if conflict found.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-20 15:46:27 +05:30
parent 6847f5de95
commit 6a3834a7eb

View File

@@ -344,6 +344,32 @@ ${callerContext ? `\n${callerContext}` : ''}`;
const cleanPhone = phoneNumber.replace(/[^0-9]/g, '').slice(-10);
const resolved = await callerService.resolve(cleanPhone, auth).catch(() => null);
// Conflict check: same doctor + same date
const bookingDate = scheduledAt.split('T')[0];
const existingAppts = await platform.query<any>(
`{ appointments(first: 50, filter: { doctorName: { eq: "${doctorName}" } }, orderBy: [{ scheduledAt: AscNullsLast }]) { edges { node { id scheduledAt status patientName } } } }`,
).catch(() => ({ appointments: { edges: [] } }));
const conflicts = existingAppts.appointments.edges
.map((e: any) => e.node)
.filter((a: any) => a.status === 'SCHEDULED' && a.scheduledAt?.startsWith(bookingDate));
// Check if this patient already has a booking with this doctor on the same date
const patientConflict = conflicts.find((a: any) =>
a.patientName?.toLowerCase().includes(patientName.split(' ')[0].toLowerCase()),
);
if (patientConflict) {
logger.log(`[WA-BOOK] Conflict: patient already booked with ${doctorName} on ${bookingDate}`);
return { booked: false, message: `You already have an appointment with ${doctorName} on ${bookingDate}. Would you like to choose a different date?` };
}
// Check if the doctor has too many appointments at this exact time
const slotConflicts = conflicts.filter((a: any) => a.scheduledAt === scheduledAt);
if (slotConflicts.length >= 3) {
logger.log(`[WA-BOOK] Conflict: ${doctorName} fully booked at ${scheduledAt} (${slotConflicts.length} existing)`);
return { booked: false, message: `${doctorName} is fully booked at this time. Please choose a different slot.` };
}
if (resolved?.isNew) {
const firstName = patientName.split(' ')[0];
const lastName = patientName.split(' ').slice(1).join(' ') || '';