import { Appointment, Location } from "fhir/r4"; import { isVirtualLocation } from "./roomLocation"; // FHIR R4 has no `Appointment.location` element - the site rides in `participant[].actor` as a // `Location/` reference, next to the Practitioner, PractitionerRole and Patient actors. The // patient-facing appointment cards used to ignore it entirely and print the logged-in // organization's name in the location slot, so an appointment booked at "West Burlington Hospital" // displayed as "Great River Health Center" (the org). The data was always correct; nothing read it. // // This matters beyond tidiness now that a telehealth visit IS a Location: showing the // org there would tell a patient booked into a Video Visit to attend a physical building. /** The `Location/...` participant on an appointment, if it has one. */ const findLocationParticipant = (appointment: Appointment) => appointment.participant?.find((participant) => // Anchored on the prefix on purpose. The sibling practitioner lookup in these cards matches with // `reference.includes("Practitioner")`; copying that here would let any reference that merely // CONTAINS "Location" win. participant.actor?.reference?.startsWith("Location/"), ); /** * The name to show in an appointment's location slot. * * Falls back to `organizationName` when the appointment names no location - appointments booked * before locations were captured have no Location participant, and the owning organization is the * best "where" we have for them. A Location participant with no usable display falls back the same * way: a blank where the site should be reads as deliberate, which is worse than naming the org. */ export const getAppointmentLocationName = ( appointment: Appointment, organizationName: string | undefined, locations: readonly Location[] = [], ): string => { const participant = findLocationParticipant(appointment); // A virtual Location's name is the admin's label for it; the visit itself is a video visit. const id = participant?.actor?.reference?.split("/")[1]; const location = id ? locations.find((candidate) => candidate.id === id) : undefined; if (location && isVirtualLocation(location)) return "Video visit"; const locationDisplay = participant?.actor?.display?.trim(); return locationDisplay || organizationName?.trim() || ""; };