import type { Organization } from "fhir/r4"; /** * How a practice handles laboratory work, and whether it has imaging on site. The server writes this * URL, so the literal must match it. An absent field means "not asked", which is not "no". * `laboratory` means the practice performs tests itself; collecting specimens is a separate answer. */ export const DIAGNOSTICS_SETTINGS_EXTENSION_URL = "https://akashichealthrecords.com/fhir/extensions/diagnostics-settings"; export const DIAGNOSTICS_FIELDS = [ "ordersExternal", "collectsSpecimens", "laboratory", "acceptsOwnOrders", "acceptsOutsideOrders", "imaging", ] as const; export type DiagnosticsField = (typeof DIAGNOSTICS_FIELDS)[number]; export type DiagnosticsDeclaration = Partial>; export const readDiagnosticsDeclaration = ( organization?: Organization | null, ): DiagnosticsDeclaration => { const found = (organization?.extension ?? []) .find((e) => e.url === DIAGNOSTICS_SETTINGS_EXTENSION_URL); if (!found) return {}; const declaration: DiagnosticsDeclaration = {}; for (const field of DIAGNOSTICS_FIELDS) { const value = (found.extension ?? []).find((e) => e.url === field)?.valueBoolean; if (value !== undefined) declaration[field] = value; } return declaration; }; /** The declaration as the Organization extension the server writes, for the copy held in context. */ export const diagnosticsExtension = (declaration: DiagnosticsDeclaration) => ({ url: DIAGNOSTICS_SETTINGS_EXTENSION_URL, extension: DIAGNOSTICS_FIELDS .filter((field) => declaration[field] !== undefined) .map((field) => ({ url: field, valueBoolean: declaration[field] })), }); /** Whether the practice has answered at all, which is a different question from what it answered. */ export const hasDeclaredDiagnostics = (declaration: DiagnosticsDeclaration): boolean => DIAGNOSTICS_FIELDS.some((field) => declaration[field] !== undefined); /** * Whether the Lab and imaging setup door is offered. The declaration is answered on that page, so a * practice that has not answered is still offered it; one that answered no to all of it is not. */ export const offersDiagnosticsSetup = (declaration: DiagnosticsDeclaration): boolean => !hasDeclaredDiagnostics(declaration) || DIAGNOSTICS_FIELDS.some((field) => declaration[field] === true); /** What the catalogue may offer to add. Neither declared means nothing to add, not everything. */ export const declaredKinds = ( declaration: DiagnosticsDeclaration, ): ("laboratory" | "imaging")[] => [ ...(declaration.laboratory === true ? (["laboratory"] as const) : []), ...(declaration.imaging === true ? (["imaging"] as const) : []), ];