import type { Appointment } from 'fhir/r4'; import { ORGANIZATION_TAG_SYSTEM } from './carePlanAuthorship'; // The statuses the server cancels when a patient withdraws a grant (cancelAppointment.ts). const CANCELLED_ON_WITHDRAWAL: ReadonlySet = new Set([ 'proposed', 'pending', 'booked', 'arrived', 'checked-in', 'waitlist', ]); /** * The visits a withdrawal from this practice will cancel, by the server's own rule: tagged with this * practice, still standing, and starting after the instant. A visit with no start is left alone there, * so it is not counted here. */ export const visitsAWithdrawalCancels = ( appointments: readonly Appointment[], organizationId: string, now: string, ): Appointment[] => appointments.filter((appointment) => appointment.meta?.tag?.some((tag) => tag.system === ORGANIZATION_TAG_SYSTEM && tag.code === organizationId) && CANCELLED_ON_WITHDRAWAL.has(appointment.status ?? '') && Boolean(appointment.start) && Date.parse(appointment.start as string) > Date.parse(now)) .sort((a, b) => Date.parse(a.start as string) - Date.parse(b.start as string)); /** What the visit is, falling back to the plain word so a listed visit is never nameless. */ export const withdrawnVisitName = (appointment: Appointment): string => appointment.serviceType?.[0]?.text?.trim() || appointment.serviceType?.[0]?.coding?.find((coding) => coding.display?.trim())?.display?.trim() || appointment.appointmentType?.text?.trim() || appointment.description?.trim() || 'Visit';