diff --git a/src/messaging/messaging.service.ts b/src/messaging/messaging.service.ts index 49bdfcf..3041239 100644 --- a/src/messaging/messaging.service.ts +++ b/src/messaging/messaging.service.ts @@ -251,7 +251,10 @@ ${callerContext ? `\n${callerContext}` : ''}`; date: z.string().optional().describe('Date in YYYY-MM-DD. Defaults to tomorrow.'), }), execute: async ({ doctorId, doctorName, date }) => { + // Default to tomorrow, use IST for day-of-week matching const targetDate = date ?? new Date(Date.now() + 86400000).toISOString().split('T')[0]; + const dayNames = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY']; + const targetDay = dayNames[new Date(targetDate + 'T00:00:00+05:30').getDay()]; const data = await platform.query( `{ doctors(first: 50) { edges { node { @@ -259,25 +262,46 @@ ${callerContext ? `\n${callerContext}` : ''}`; ${DOCTOR_VISIT_SLOTS_FRAGMENT} } } } }`, ); - const allDocs = normalizeDoctors(data.doctors.edges.map((e: any) => e.node)); - const doctor = allDocs.find((d: any) => d.id === doctorId); - const slots = doctor?.availableSlots ?? []; + const rawDocs = data.doctors.edges.map((e: any) => e.node); + const doctor = rawDocs.find((d: any) => d.id === doctorId); + if (!doctor) { + return { sent: false, message: `Doctor not found.` }; + } - if (!slots.length) { + // Find visit slots for the target day-of-week + const rawSlots = doctor.visitSlots?.edges?.map((e: any) => e.node) ?? []; + const daySlots = rawSlots.filter((s: any) => s.dayOfWeek === targetDay); + + if (!daySlots.length) { + return { sent: false, message: `${doctorName} is not available on ${targetDay.charAt(0) + targetDay.slice(1).toLowerCase()} (${targetDate}). Please choose a different date.` }; + } + + // Generate hourly time slots from startTime-endTime + const timeSlots: { time: string; clinic: string }[] = []; + for (const ds of daySlots) { + const startHour = parseInt(ds.startTime?.split(':')[0] ?? '9', 10); + const endHour = parseInt(ds.endTime?.split(':')[0] ?? '17', 10); + const clinicName = ds.clinic?.clinicName ?? ''; + for (let h = startHour; h < endHour && timeSlots.length < 10; h++) { + timeSlots.push({ time: `${String(h).padStart(2, '0')}:00`, clinic: clinicName }); + } + } + + if (!timeSlots.length) { return { sent: false, message: `No slots available for ${doctorName} on ${targetDate}.` }; } const sections: ListSection[] = [{ title: `${doctorName} — ${targetDate}`, - rows: slots.slice(0, 10).map((s: any) => ({ + rows: timeSlots.map((s) => ({ id: `slot:${doctorId}:${targetDate}T${s.time}:00`, title: s.time, - description: s.clinic ?? '', + description: s.clinic, })), }]; await provider.sendList(phone, `Available slots for ${doctorName}:`, 'View Slots', sections); - logger.log(`[WA-TOOL] send_slot_list: ${slots.length} slots for ${doctorName}`); - return { sent: true, slots: slots.length }; + logger.log(`[WA-TOOL] send_slot_list: ${timeSlots.length} slots for ${doctorName} on ${targetDate} (${targetDay})`); + return { sent: true, slots: timeSlots.length }; }, }),