import type { Appointment } from "fhir/r4"; import type { PatientReadinessSummary } from "../services/PatientReadinessService.tsx"; // The server's label and statuses for the "first appointment" area (patientReadiness.ts). export const FIRST_APPOINTMENT_LABEL = "First appointment"; const SCHEDULED_STATUSES = new Set(["booked", "arrived", "checked-in", "fulfilled"]); /** * The server reads appointments through HealthLake search, which lags a write by seconds, so a * readiness read just after the desk books can miss the visit the desk is looking at. A scheduled * visit already on screen settles that one area; nothing here ever marks an area incomplete. */ export const readinessCountsTheVisitOnScreen = ( summary: PatientReadinessSummary, appointments: Appointment[], ): PatientReadinessSummary => { if (!summary.incompleteLabels.includes(FIRST_APPOINTMENT_LABEL)) return summary; if (!appointments.some((appointment) => SCHEDULED_STATUSES.has(appointment.status))) return summary; const incompleteLabels = summary.incompleteLabels.filter((label) => label !== FIRST_APPOINTMENT_LABEL); return { ...summary, incompleteLabels, completedCount: Math.min(summary.totalCount, summary.completedCount + 1), completed: incompleteLabels.length === 0, }; };