diff --git a/src/pages/call-desk.tsx b/src/pages/call-desk.tsx index 618b7cc..2157c2e 100644 --- a/src/pages/call-desk.tsx +++ b/src/pages/call-desk.tsx @@ -108,14 +108,22 @@ export const CallDeskPage = () => { } }, [isInCall]); - // Build activeLead from resolved caller or fallback to client-side match + // Build activeLead from resolved caller or fallback to client-side match. + // The resolver is the authoritative source for patientId (it just joined + // lead↔patient by phone), so overlay it on top of any worklist row that + // pre-dates the linkage. Without this, the Book Appt pills can't find + // a returning caller's prior appointments because the frontend loses + // sight of which patient they are. + const workLead = resolvedCaller ? marketingLeads.find((l) => l.id === resolvedCaller.leadId) : null; const callerLead = resolvedCaller - ? marketingLeads.find((l) => l.id === resolvedCaller.leadId) ?? { - id: resolvedCaller.leadId, - contactName: { firstName: resolvedCaller.firstName, lastName: resolvedCaller.lastName }, - contactPhone: [{ number: resolvedCaller.phone, callingCode: '+91' }], - patientId: resolvedCaller.patientId, - } + ? workLead + ? { ...workLead, patientId: (workLead as any).patientId ?? resolvedCaller.patientId } + : { + id: resolvedCaller.leadId, + contactName: { firstName: resolvedCaller.firstName, lastName: resolvedCaller.lastName }, + contactPhone: [{ number: resolvedCaller.phone, callingCode: '+91' }], + patientId: resolvedCaller.patientId, + } : callerNumber ? marketingLeads.find((l) => l.contactPhone?.[0]?.number?.endsWith(callerNumber) || callerNumber.endsWith(l.contactPhone?.[0]?.number ?? '---')) : null;