import type { Appointment } from 'fhir/r4'; import { visitTimePhase } from './visitInvitationWords'; // A scheduled collaboration visit is an ordinary appointment whose invitation names the team it invites from. That one // value is the only link from a visit to its team; nothing else is taken to mean "this team's". const VISIT_INVITATION_EXTENSION = 'https://akashichealthrecords.com/fhir/StructureDefinition/appointment-visit-invitation'; export const appointmentTeamId = (appointment: Pick): string | undefined => appointment.extension?.find((ext) => ext.url === VISIT_INVITATION_EXTENSION) ?.extension?.find((sub) => sub.url === 'teamId')?.valueString || undefined; export type TeamVisitTab = 'upcoming' | 'past' | 'cancelled'; const CANCELLED = new Set(['cancelled', 'noshow', 'entered-in-error']); /** * Which list a team visit belongs on. Cancelled (or no-show, or entered in error) by its own status; past once its * scheduled time has passed or it was held; otherwise upcoming, including a visit under way and one with no readable * time. The clock never makes a visit cancelled or held. */ export const teamVisitTab = (visit: { start?: string; end?: string; status?: string }, now: number): TeamVisitTab => { if (CANCELLED.has(visit.status ?? '')) return 'cancelled'; if (visit.status === 'fulfilled' || visitTimePhase(visit, now) === 'ended') return 'past'; return 'upcoming'; }; /** How a scheduled collaboration visit is attended: the patient is always in it, on video. */ export const SCHEDULED_ATTENDANCE = 'Video visit with the patient';