import type { DiagnosticOrderQueueRow } from "../../../hooks/useDiagnosticOrderQueue.ts"; import { DIAGNOSTIC_ORDER_STATE_LABELS, type DiagnosticOrderState } from "../../../utils/diagnosticOrderQueue.ts"; /** * A state nobody stamped is its own thing, not a blank. An order this system does not recognise is * one somebody has to look at, and an empty cell reads as a row that merely has less in it. */ export const stateLabel = (state?: DiagnosticOrderState) => (state ? DIAGNOSTIC_ORDER_STATE_LABELS[state] : "Not tracked"); /** * An order still waiting on an answer, the only kind a result can be filed against. An order with no * state is excluded: this system did not stamp it, so a clinical result must not be written against it. */ export const isAwaitingResult = (state?: DiagnosticOrderState) => Boolean(state) && state !== "resulted" && state !== "reviewed"; /** * A test performed here whose specimen has not been received: nothing can be resulted, or reported, yet. * A test sent out is resulted from the other laboratory's report, and one routed to another Akashic * practice comes back from it. The server refuses the same (`Receive the specimen before entering its * result.`), at the result and before a report is uploaded; this is the screens saying so first. */ export const awaitsSpecimen = (row: Pick) => row.kind === "laboratory" && !row.accession && !row.sentOut && !row.akashicRoute; export const AWAITS_SPECIMEN_MESSAGE = "Receive the specimen before entering its result."; /** Urgent work belongs at the top; within a priority, oldest first, because it has waited longest. */ export const sortQueue = (rows: DiagnosticOrderQueueRow[]): DiagnosticOrderQueueRow[] => { const urgency = (row: DiagnosticOrderQueueRow) => (row.priority === "stat" ? 0 : row.priority === "urgent" ? 1 : 2); return [...rows].sort((a, b) => { const byUrgency = urgency(a) - urgency(b); if (byUrgency !== 0) return byUrgency; return (a.authoredOn ?? "").localeCompare(b.authoredOn ?? ""); }); }; /** What is known of a send-out. Sent is not delivered, and an unanswered attempt is unknown, not failed. */ export const deliveryLine = (row: DiagnosticOrderQueueRow): string | undefined => { // An Akashic laboratory has the order in its intake once sent; acceptance and return are its own facts. if (row.akashicRoute) { const laboratory = row.akashicRoute.laboratory ?? row.sentOut?.laboratory ?? "the laboratory"; if (row.akashicRoute.returnedAt) return `Released result returned by ${laboratory} against this order`; // The refusal and its reason stand on the row below. if (row.akashicRoute.refusedAt) return `Refused by ${laboratory}`; if (row.akashicRoute.acceptedAt) return `Accepted by ${laboratory} · specimen receipt not confirmed`; // Saved sent, but the laboratory has not been given it yet; it is delivered from the order. if (row.deliveryOwed) return `Not yet delivered to ${laboratory} · deliver it from the order`; return `To ${laboratory} · Awaiting ${laboratory} acceptance`; } if (!row.sentOut) return undefined; const laboratory = row.sentOut.laboratory ?? "the laboratory"; // Once their result is filed, that is where the order stands; the send is history. if (row.resultOnFile) return `Performed by ${laboratory} · result filed here`; // Saved, but nothing confirms the electronic send was handed on; it is retried from the order. if (row.transmissionOwed) return `Electronic send to ${laboratory} not confirmed · retry it from the order`; if (row.transmission?.outcome === "accepted") return `Delivered to ${laboratory} · acceptance not confirmed`; if (row.transmission?.outcome === "failed") { return `Sent to ${laboratory} · delivery status unavailable. Check this attempt before sending again.`; } // A refusal is its own marker on the row. if (row.transmission?.outcome === "refused") return undefined; return `Sent out to ${laboratory} · delivery not confirmed`; }; /** What is known of returning a released outside result, for the report version now on file. */ export const returnLine = (row: DiagnosticOrderQueueRow): string | undefined => { if (!row.returned) return undefined; const to = row.fromOutside ?? "the sender"; const updated = (row.returned.version ?? 0) > 0; switch (row.returned.outcome) { case "delivered": return `Returned to ${to} · this does not confirm the clinician reviewed it`; case "unconfirmed": return `Faxed to ${to} · ${updated ? "updated report " : ""}delivery not confirmed`; case "failed": return `Delivery to ${to} failed. Check the attempt before sending again.`; case "unavailable": return "Delivery status unavailable. Check before sending again."; default: return updated ? "Updated report delivery not confirmed" : `Return needed · delivery to ${to} not confirmed`; } }; /** Where another test of the same draw stands, as the release dialog says it about its siblings. */ export const panelStanding = (row: DiagnosticOrderQueueRow): string => { const test = row.test ?? "Another test"; if (row.state === "resulted" || row.state === "reviewed") return `${test} is already released`; if (row.awaitingRelease) return `${test} is awaiting release`; if (row.accession) return `${test} is still on the bench`; return `${test} has no result yet`; };