diff --git a/src/components/call-desk/appointment-form.tsx b/src/components/call-desk/appointment-form.tsx index acb5bc8..53bdc77 100644 --- a/src/components/call-desk/appointment-form.tsx +++ b/src/components/call-desk/appointment-form.tsx @@ -286,13 +286,17 @@ export const AppointmentForm = ({ const trimmedName = patientName.trim(); const nameChanged = isNameEditable && trimmedName.length > 0 && trimmedName !== initialLeadName; - // DO NOT update the shared Patient entity when name changes - // during appointment creation. The Patient record is shared - // across all appointments — modifying it here would + // Update patient name only when it was empty (new caller with no name). + // Don't overwrite an existing patient name — that would // retroactively change the name on all past appointments. - // The patient name for THIS appointment is stored on the - // Appointment entity itself (via doctorName/department). - // Bug #527: removed updatePatient() call. + // Bug #527: only set name on patients with no existing name. + if (nameChanged && patientId && initialLeadName.length === 0) { + const nameParts = { firstName: trimmedName.split(' ')[0], lastName: trimmedName.split(' ').slice(1).join(' ') || '' }; + apiClient.graphql( + `mutation($id: UUID!, $data: PatientUpdateInput!) { updatePatient(id: $id, data: $data) { id } }`, + { id: patientId, data: { fullName: nameParts } }, + ).catch((err: unknown) => console.warn('Failed to update patient name:', err)); + } // Update lead status/lastContacted on every appointment book // (those are genuinely about this appointment), but only