import { Organization } from 'fhir/r4'; // --------------------------------------------------------------------------- // - WHERE THIS PRACTICE'S INCOMING REFERRALS LAND. // // front-desk the receptionists handle referrals // referral-workspace staff holding the referral coordinator role work them // // The same field is read by SENDING organizations: a practice that has designated a destination is one // another practice can deliver to in-system, with no fax and no phone call. One field does both jobs. // // Mirrors the server's `utils/referralRoutingPolicy.ts` - this file is the client's READER, and the two // must agree on the extension url byte for byte. // --------------------------------------------------------------------------- // There is no GET /R4/referral-routing-settings. The org's saved routing travels only on the Organization // resource, as this extension. The server WRITES this exact url (setReferralRouting in its own copy of // this file); the client READS it back here, so the literal MUST match character for character. // A drifted character reads as "never saved" forever: the practice chooses a destination, the server // stores it, and every reader still sees no designation. export const ORG_REFERRAL_ROUTING_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-referral-routing'; const DESTINATION_SUB_URL = 'destination'; export const REFERRAL_DESTINATIONS = { FRONT_DESK: 'front-desk', REFERRAL_WORKSPACE: 'referral-workspace', } as const; export type ReferralDestination = (typeof REFERRAL_DESTINATIONS)[keyof typeof REFERRAL_DESTINATIONS]; const VALID_DESTINATIONS: readonly string[] = Object.values(REFERRAL_DESTINATIONS); export const isReferralDestination = (value: unknown): value is ReferralDestination => typeof value === 'string' && VALID_DESTINATIONS.includes(value); export interface ReferralRoutingPolicy { /** * OPTIONAL, AND THERE IS DELIBERATELY NO DEFAULT VALUE - the same rule as the server's copy, and it * is the load-bearing one. * * *"A routing setting nobody filled in is the same as not being on the system, because the sending * side falls back to fax anyway."* Undefined means the practice has never chosen, and a sending org * must read that as *"we cannot deliver in-system here"*. * * Defaulting to `front-desk` would make every practice advertise, from the day this ships, that it * accepts in-system referrals - delivering them into queues nobody has been told to watch. A referral * that silently lands somewhere unwatched is strictly worse than a fax, because the sender believes it * arrived. */ destination?: ReferralDestination; } /** A practice that has never opened the setting has no destination - not a default one. */ export const DEFAULT_REFERRAL_ROUTING_POLICY: ReferralRoutingPolicy = {}; /** * Pure reader - mirrors the server's `resolveEffectiveReferralRouting` exactly (same extension url, same * child url, same treatment of an unrecognised code), so every surface seeds from whatever the practice * last saved rather than from a second opinion assembled locally. */ export function resolveOrgReferralRouting(organization?: Organization | null): ReferralRoutingPolicy { const ext = organization?.extension?.find((e) => e.url === ORG_REFERRAL_ROUTING_EXTENSION_URL); if (!ext) return { ...DEFAULT_REFERRAL_ROUTING_POLICY }; const destination = (ext.extension ?? []).find((e) => e.url === DESTINATION_SUB_URL)?.valueCode; // An unrecognised stored value reads as "never chosen" rather than being passed through - a code // this build does not know cannot be routed to, and showing it as a designation would tell an admin // their referrals are going somewhere that does not exist. return isReferralDestination(destination) ? { destination } : { ...DEFAULT_REFERRAL_ROUTING_POLICY }; } /** Whether another practice could deliver a referral here in-system, rather than faxing it. */ export const acceptsInSystemReferrals = (organization?: Organization | null): boolean => resolveOrgReferralRouting(organization).destination !== undefined;