import type { ServiceRequest } from "fhir/r4"; const ORDER_REJECTION_URL = "https://akashichealthrecords.com/fhir/StructureDefinition/order-rejection"; /** A laboratory's refusal of this order that nobody has resolved yet, with the reason it gave. */ export const openOrderRefusal = (order: ServiceRequest | undefined): { reason?: string } | undefined => { const found = order?.extension?.find((extension) => extension.url === ORDER_REJECTION_URL); if (!found || found.extension?.some((part) => part.url === "resolved")) return undefined; const reason = found.extension?.find((part) => part.url === "reason")?.valueCodeableConcept; return { reason: reason?.text ?? reason?.coding?.[0]?.display }; }; const SPECIMEN_REJECTION_URL = "https://akashichealthrecords.com/fhir/StructureDefinition/specimen-rejection"; /** The laboratory refusing the TUBE rather than the order: why, and whether a new sample was asked for. */ export const specimenRefusal = (order: ServiceRequest | undefined): { reason?: string; redraw: boolean } | undefined => { const found = order?.extension?.find((extension) => extension.url === SPECIMEN_REJECTION_URL); if (!found) return undefined; const part = (url: string) => found.extension?.find((each) => each.url === url); const reason = part("reason")?.valueCodeableConcept; return { reason: reason?.text ?? reason?.coding?.[0]?.display, redraw: part("disposition")?.valueCode === "redraw" }; };