import type { Location, Organization } from "fhir/r4"; import { isRoomLocation } from "./roomLocation"; /** * Whether a practice rooms patients in physical exam rooms. Unset is not "no": a practice that has not * answered follows its rooms, on with at least one active room and off with none. An answer always wins. * Rooming is never required for care; this only decides whether room actions and wording are shown. */ export const PRACTICE_WORKFLOW_EXTENSION_URL = "https://akashichealthrecords.com/fhir/extensions/practice-workflow"; const PHYSICAL_ROOMING = "physicalRooming"; export const readPhysicalRooming = (organization?: Organization | null): boolean | undefined => (organization?.extension ?? []) .find((e) => e.url === PRACTICE_WORKFLOW_EXTENSION_URL) ?.extension?.find((e) => e.url === PHYSICAL_ROOMING)?.valueBoolean; export const usesPhysicalRooming = (organization: Organization | null | undefined, locations: Location[]): boolean => readPhysicalRooming(organization) ?? locations.some((location) => isRoomLocation(location) && location.status !== "inactive"); /** The organization with this answer recorded, every other extension and workflow field kept. */ export const withPhysicalRooming = (organization: Organization, rooming: boolean): Organization => { const others = (organization.extension ?? []).filter((e) => e.url !== PRACTICE_WORKFLOW_EXTENSION_URL); const workflow = (organization.extension ?? []).find((e) => e.url === PRACTICE_WORKFLOW_EXTENSION_URL); const fields = (workflow?.extension ?? []).filter((e) => e.url !== PHYSICAL_ROOMING); return { ...organization, extension: [ ...others, { url: PRACTICE_WORKFLOW_EXTENSION_URL, extension: [...fields, { url: PHYSICAL_ROOMING, valueBoolean: rooming }] }, ], }; };