import type { MedicationRequest, MedicationStatement } from "fhir/r4"; // What a medication record says, from its own status, intent and negation only, for every list and summary. A prescription // is a recorded order, not proof it is taken; a statement is reported use; anything else stays visible in its own words. export type MedicationMeaning = | { group: "hidden" } | { group: "current"; kind: "prescription" | "reported-use" } | { group: "other"; label: string } | { group: "past"; label: string }; /** The intents that make an order a prescription. A proposal, a plan or an option is not one. */ const FORMAL_ORDER = new Set(["order", "original-order", "reflex-order", "filler-order", "instance-order"]); const REQUEST_STATUS: Record = { "on-hold": "On hold", unknown: "Status unknown", stopped: "Stopped", completed: "Completed", cancelled: "Cancelled", }; const REQUEST_INTENT: Record = { proposal: "Proposed", plan: "Planned", option: "An option" }; const STATEMENT_STATUS: Record = { intended: "Intended", "on-hold": "On hold", unknown: "Status unknown", stopped: "Stopped", completed: "Completed", "not-taken": "Recorded as not taken", }; const said = (table: Record, value: string | undefined, absent: string): string => value ? table[value] ?? `Recorded as ${value}` : absent; export const requestMeaning = (request: Pick): MedicationMeaning => { const { status, intent } = request; if (status === "entered-in-error" || status === "draft") return { group: "hidden" }; if (status === "stopped" || status === "completed" || status === "cancelled") return { group: "past", label: said(REQUEST_STATUS, status, "") }; // An order not to give a medicine is never a claim that it is prescribed. if (request.doNotPerform === true) return { group: "other", label: "Recorded instruction not to take this" }; const formal = Boolean(intent && FORMAL_ORDER.has(intent)); if (status === "active" && formal) return { group: "current", kind: "prescription" }; const label = [ formal ? undefined : said(REQUEST_INTENT, intent, "Intent not recorded"), status === "active" ? undefined : said(REQUEST_STATUS, status, "Status not recorded"), ].filter(Boolean).join(" ยท "); return { group: "other", label }; }; export const statementMeaning = (statement: Pick): MedicationMeaning => { const status = statement.status as string | undefined; if (status === "entered-in-error" || status === "draft") return { group: "hidden" }; if (status === "stopped" || status === "completed" || status === "not-taken") return { group: "past", label: said(STATEMENT_STATUS, status, "") }; if (status === "active") return { group: "current", kind: "reported-use" }; return { group: "other", label: said(STATEMENT_STATUS, status, "Status not recorded") }; }; /** * Who reported a statement, from its own `informationSource` only. The subject is themselves, a RelatedPerson is the * person caring for them, a clinician or practice is named as recorded; nothing else is guessed, so an absent or * unfamiliar source is said to be unrecorded rather than credited to the patient or the care team. */ export type StatementSource = | { who: "patient" } | { who: "helper"; name?: string } | { who: "clinician"; name?: string } | { who: "unknown" }; export const statementSource = (statement: Pick): StatementSource => { const reference = statement.informationSource?.reference ?? ""; const name = statement.informationSource?.display?.trim() || undefined; if (reference.startsWith("Patient/") && reference === statement.subject?.reference) return { who: "patient" }; if (reference.startsWith("RelatedPerson/")) return { who: "helper", name }; if (/^(Practitioner|PractitionerRole|Organization)\//.test(reference)) return { who: "clinician", name }; return { who: "unknown" }; };