import type { Appointment, CodeableConcept, HealthcareService } from 'fhir/r4'; import { selectedServiceIdOf } from './booking/selectedServiceRef'; /** * The visit's catalogue service: the one it recorded choosing, else the only one whose type shares a code, or ''. * Never guessed: a code two services share leaves an older visit with its own label. */ export const visitServiceIdFor = (appointment: Appointment | undefined, services: HealthcareService[]): string => { const recorded = selectedServiceIdOf(appointment); if (recorded) return services.some((service) => service.id === recorded) ? recorded : ''; const codes = new Set( (appointment?.serviceType ?? []) .flatMap((concept) => (concept.coding ?? []).map((coding) => coding.code)) .filter((code): code is string => Boolean(code)), ); if (codes.size === 0) return ''; const matches = services.filter((service) => (service.type ?? []).some((t) => (t.coding ?? []).some((c) => c.code && codes.has(c.code))), ); return matches.length === 1 ? matches[0].id ?? '' : ''; }; /** What the visit says it was booked for, read from the visit itself. */ export const bookedServiceLabel = (appointment: Appointment | undefined): string | undefined => { const [concept] = appointment?.serviceType ?? []; return concept?.text || concept?.coding?.find((c) => c.display)?.display || undefined; }; /** * The service fields an edit writes. A service nobody changed stays exactly as booked, whether or not the * form matched it to the catalogue: an edit to the time must not erase it, nor restate it in today's coding. */ export const serviceFieldsToSave = (args: { selectedService: HealthcareService | undefined; appointment: Appointment | undefined; isEditMode: boolean; serviceTouched: boolean; }): { serviceType: CodeableConcept[] | undefined; serviceCategory: CodeableConcept[] | undefined } => { if (args.isEditMode && !args.serviceTouched) { return { serviceType: args.appointment?.serviceType, serviceCategory: args.appointment?.serviceCategory }; } if (args.selectedService) return { serviceType: args.selectedService.type, serviceCategory: args.selectedService.category }; return { serviceType: undefined, serviceCategory: undefined }; }; /** The services the desk may choose now: the practice's active ones, and an edited visit's own current service. */ export const deskSelectableServices = (services: HealthcareService[], currentServiceId: string | undefined): HealthcareService[] => services.filter((service) => service.active !== false || (Boolean(currentServiceId) && service.id === currentServiceId)); /** Why the desk may not send this service, or undefined: a switched-off service stays only as the visit's own. */ export const inactiveSelectionRefusal = (args: { selectedService: HealthcareService | undefined; currentServiceId: string | undefined; isEditMode: boolean; }): string | undefined => { const { selectedService, currentServiceId, isEditMode } = args; if (!selectedService || selectedService.active !== false) return undefined; if (isEditMode && currentServiceId && selectedService.id === currentServiceId) return undefined; return `${selectedService.name ?? 'This service'} is no longer offered by the practice. Choose another service.`; };