diff --git a/src/components/call-desk/appointment-form.tsx b/src/components/call-desk/appointment-form.tsx index 53bdc77..7eead57 100644 --- a/src/components/call-desk/appointment-form.tsx +++ b/src/components/call-desk/appointment-form.tsx @@ -259,6 +259,25 @@ export const AppointmentForm = ({ ); notify.success('Appointment Updated'); } else { + // If no patient record exists yet (new caller), create one now + let resolvedPatientId = patientId; + if (!resolvedPatientId && callerNumber) { + const trimmedName = patientName.trim(); + const nameParts = { + firstName: trimmedName.split(' ')[0] || 'Unknown', + lastName: trimmedName.split(' ').slice(1).join(' ') || '', + }; + try { + const created = await apiClient.graphql<{ createPatient: { id: string } }>( + `mutation($data: PatientCreateInput!) { createPatient(data: $data) { id } }`, + { data: { fullName: nameParts, phones: { primaryPhoneNumber: callerNumber }, patientType: 'NEW' } }, + ); + resolvedPatientId = created.createPatient.id; + } catch (err) { + console.warn('Failed to create patient:', err); + } + } + // Create appointment await apiClient.graphql( `mutation CreateAppointment($data: AppointmentCreateInput!) { @@ -274,7 +293,7 @@ export const AppointmentForm = ({ department: selectedDoctor?.department ?? '', doctorId: doctor, reasonForVisit: chiefComplaint || null, - ...(patientId ? { patientId } : {}), + ...(resolvedPatientId ? { patientId: resolvedPatientId } : {}), }, }, ); diff --git a/src/pages/call-desk.tsx b/src/pages/call-desk.tsx index 64476b8..4db54f9 100644 --- a/src/pages/call-desk.tsx +++ b/src/pages/call-desk.tsx @@ -91,7 +91,7 @@ export const CallDeskPage = () => { .then((result) => { setResolvedCaller(result); if (result.isNew) { - notify.info('New Caller', 'Lead and patient records created'); + notify.info('New Caller', 'No existing records found for this number'); } }) .catch((err) => {