fix: link patientId to appointment in WhatsApp booking

Appointments created via WhatsApp had null patientId — lookup_appointments
couldn't find them. Now resolves patient before booking and includes
patientId in the createAppointment mutation. Fixed in both flow tool
registry and legacy messaging service.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-20 18:56:39 +05:30
parent 903e82b536
commit 963cf28d23
2 changed files with 8 additions and 5 deletions

View File

@@ -172,8 +172,10 @@ export class ToolRegistry {
return { booked: false, message: `${doctorName} is fully booked at this time.` };
}
// Create lead/patient if new
// Resolve caller — creates lead/patient if new
const resolved = await this.caller.resolve(cleanPhone, ctx.auth).catch(() => null);
let patientId = resolved?.patientId;
if (resolved?.isNew && patientName) {
const firstName = patientName.split(' ')[0];
const lastName = patientName.split(' ').slice(1).join(' ') || '';
@@ -182,7 +184,7 @@ export class ToolRegistry {
`mutation($data: PatientCreateInput!) { createPatient(data: $data) { id } }`,
{ data: { fullName: { firstName, lastName }, phones: { primaryPhoneNumber: `+91${cleanPhone}` }, patientType: 'NEW' } },
);
const patientId = p?.createPatient?.id;
patientId = p?.createPatient?.id;
await this.platform.query<any>(
`mutation($data: LeadCreateInput!) { createLead(data: $data) { id } }`,
{ data: { name: `WhatsApp — ${patientName}`, contactName: { firstName, lastName }, contactPhone: { primaryPhoneNumber: `+91${cleanPhone}` }, source: 'WHATSAPP', status: 'NEW', interestedService: department, ...(patientId ? { patientId } : {}) } },
@@ -190,10 +192,10 @@ export class ToolRegistry {
} catch {}
}
// Book
// Book — include patientId so appointment is linked to patient record
const result = await this.platform.query<any>(
`mutation($data: AppointmentCreateInput!) { createAppointment(data: $data) { id } }`,
{ data: { name: `WhatsApp Booking — ${patientName} (${department})`, scheduledAt, status: 'SCHEDULED', doctorName, department, reasonForVisit: reason ?? 'General Consultation' } },
{ data: { name: `WhatsApp Booking — ${patientName} (${department})`, scheduledAt, status: 'SCHEDULED', doctorName, department, reasonForVisit: reason ?? 'General Consultation', ...(patientId ? { patientId } : {}) } },
);
const id = result?.createAppointment?.id;
if (id) {

View File

@@ -380,6 +380,7 @@ ${callerContext ? `\n${callerContext}` : ''}`;
return { booked: false, message: `${doctorName} is fully booked at this time. Please choose a different slot.` };
}
let patientId = resolved?.patientId;
if (resolved?.isNew) {
const firstName = patientName.split(' ')[0];
const lastName = patientName.split(' ').slice(1).join(' ') || '';
@@ -400,7 +401,7 @@ ${callerContext ? `\n${callerContext}` : ''}`;
const result = await platform.query<any>(
`mutation($data: AppointmentCreateInput!) { createAppointment(data: $data) { id } }`,
{ data: { name: `WhatsApp Booking — ${patientName} (${department})`, scheduledAt, status: 'SCHEDULED', doctorName, department, reasonForVisit: reason } },
{ data: { name: `WhatsApp Booking — ${patientName} (${department})`, scheduledAt, status: 'SCHEDULED', doctorName, department, reasonForVisit: reason, ...(patientId ? { patientId } : {}) } },
);
const id = result?.createAppointment?.id;
if (id) {