diff --git a/src/messaging/flow/tool-registry.ts b/src/messaging/flow/tool-registry.ts index 491322d..e4c5e6d 100644 --- a/src/messaging/flow/tool-registry.ts +++ b/src/messaging/flow/tool-registry.ts @@ -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( `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( `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) { diff --git a/src/messaging/messaging.service.ts b/src/messaging/messaging.service.ts index 42d9cb7..130cc55 100644 --- a/src/messaging/messaging.service.ts +++ b/src/messaging/messaging.service.ts @@ -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( `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) {