diff --git a/src/components/call-desk/appointment-form.tsx b/src/components/call-desk/appointment-form.tsx index 485b055..b405dd7 100644 --- a/src/components/call-desk/appointment-form.tsx +++ b/src/components/call-desk/appointment-form.tsx @@ -18,6 +18,7 @@ type ExistingAppointment = { doctorName: string; doctorId?: string; department: string; + clinicId?: string; reasonForVisit?: string; status: string; }; @@ -82,7 +83,11 @@ export const AppointmentForm = ({ const [patientPhone, setPatientPhone] = useState(callerNumber ?? ''); const [age, setAge] = useState(''); const [gender, setGender] = useState(null); - const [clinic, setClinic] = useState(null); + // Preload clinic from the existing appointment when editing — so the + // select lands on the right branch instead of being empty and forcing + // the agent to re-pick. Only historical rows that predate clinicId + // persistence will fall through to the auto-select-from-slot logic. + const [clinic, setClinic] = useState(existingAppointment?.clinicId ?? null); const [clinicItems, setClinicItems] = useState>([]); const [department, setDepartment] = useState(existingAppointment?.department ?? null); const [doctor, setDoctor] = useState(existingAppointment?.doctorId ?? null); @@ -113,14 +118,29 @@ export const AppointmentForm = ({ ).then(slots => { // Filter by selected clinic — doctor may visit multiple branches const filtered = clinic ? slots.filter(s => s.clinicId === clinic) : slots; - setTimeSlotItems(filtered.map(s => ({ id: s.time, label: s.label }))); + let items = filtered.map(s => ({ id: s.time, label: s.label })); + + // In edit mode, the saved timeSlot may have been filtered out + // (past-slot filter, schedule change, clinic mismatch). Inject + // it as a synthetic option so the dropdown still shows the + // existing value — otherwise the agent sees a cleared field + // and assumes the save-time was lost. + if (timeSlot && !items.some(i => i.id === timeSlot)) { + items = [{ id: timeSlot, label: `${timeSlot} (current)` }, ...items]; + } + + setTimeSlotItems(items); // Auto-select clinic from the slot's clinic only if no clinic chosen if (filtered.length === 0 && slots.length > 0 && !clinic) { setClinic(slots[0].clinicId); - setTimeSlotItems(slots.filter(s => s.clinicId === slots[0].clinicId).map(s => ({ id: s.time, label: s.label }))); + const autoItems = slots.filter(s => s.clinicId === slots[0].clinicId).map(s => ({ id: s.time, label: s.label })); + if (timeSlot && !autoItems.some(i => i.id === timeSlot)) { + autoItems.unshift({ id: timeSlot, label: `${timeSlot} (current)` }); + } + setTimeSlotItems(autoItems); } }).catch(() => setTimeSlotItems([])); - }, [doctor, date, clinic]); + }, [doctor, date, clinic, timeSlot]); // Availability state const [bookedSlots, setBookedSlots] = useState([]); diff --git a/src/components/call-desk/context-panel.tsx b/src/components/call-desk/context-panel.tsx index b82fd13..097b053 100644 --- a/src/components/call-desk/context-panel.tsx +++ b/src/components/call-desk/context-panel.tsx @@ -150,6 +150,7 @@ export const ContextPanel = ({ selectedLead, activities, calls, followUps, appoi doctorName: editingAppointment.doctorName ?? '', doctorId: editingAppointment.doctorId ?? undefined, department: editingAppointment.department ?? '', + clinicId: editingAppointment.clinicId ?? undefined, reasonForVisit: editingAppointment.reasonForVisit ?? undefined, status: editingAppointment.appointmentStatus ?? 'SCHEDULED', }} diff --git a/src/lib/queries.ts b/src/lib/queries.ts index 90ff5c5..e7cf847 100644 --- a/src/lib/queries.ts +++ b/src/lib/queries.ts @@ -73,7 +73,8 @@ export const APPOINTMENTS_QUERY = `{ appointments(first: 100, orderBy: [{ schedu scheduledAt durationMin appointmentType status doctorName department reasonForVisit patient { id fullName { firstName lastName } phones { primaryPhoneNumber } } - doctor { id } + doctor { id fullName { firstName lastName } } + clinicId clinic { id clinicName } } } } }`; export const PATIENTS_QUERY = `{ patients(first: 50) { edges { node { diff --git a/src/lib/transforms.ts b/src/lib/transforms.ts index 41e0035..13a9296 100644 --- a/src/lib/transforms.ts +++ b/src/lib/transforms.ts @@ -158,22 +158,31 @@ export function transformCalls(data: any): Call[] { } export function transformAppointments(data: any): Appointment[] { - return extractEdges(data, 'appointments').map((n) => ({ - id: n.id, - createdAt: n.createdAt, - scheduledAt: n.scheduledAt, - durationMinutes: n.durationMin ?? 30, - appointmentType: n.appointmentType, - appointmentStatus: n.status, - doctorName: n.doctorName, - doctorId: n.doctor?.id ?? null, - department: n.department, - reasonForVisit: n.reasonForVisit, - patientId: n.patient?.id ?? null, - patientName: n.patient?.fullName ? `${n.patient.fullName.firstName} ${n.patient.fullName.lastName}`.trim() : null, - patientPhone: n.patient?.phones?.primaryPhoneNumber ?? null, - clinicName: n.department ?? null, - })); + return extractEdges(data, 'appointments').map((n) => { + // Doctor name: prefer the relation's fullName (authoritative — pulled + // from the Doctor entity). Fall back to the denormalized doctorName + // field for legacy rows that predate the doctor relation being fetched. + const doctorFullName = n.doctor?.fullName + ? `${n.doctor.fullName.firstName ?? ''} ${n.doctor.fullName.lastName ?? ''}`.trim() + : ''; + return { + id: n.id, + createdAt: n.createdAt, + scheduledAt: n.scheduledAt, + durationMinutes: n.durationMin ?? 30, + appointmentType: n.appointmentType, + appointmentStatus: n.status, + doctorName: doctorFullName || n.doctorName || null, + doctorId: n.doctor?.id ?? null, + department: n.department, + reasonForVisit: n.reasonForVisit, + patientId: n.patient?.id ?? null, + patientName: n.patient?.fullName ? `${n.patient.fullName.firstName} ${n.patient.fullName.lastName}`.trim() : null, + patientPhone: n.patient?.phones?.primaryPhoneNumber ?? null, + clinicId: n.clinicId ?? n.clinic?.id ?? null, + clinicName: n.clinic?.clinicName ?? null, + }; + }); } export function transformPatients(data: any): Patient[] { diff --git a/src/types/entities.ts b/src/types/entities.ts index 474648d..300ccc8 100644 --- a/src/types/entities.ts +++ b/src/types/entities.ts @@ -323,6 +323,7 @@ export type Appointment = { patientId: string | null; patientName: string | null; patientPhone: string | null; + clinicId: string | null; clinicName: string | null; };