Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 3x 3x 3x 3x 136x 50x 100x 3x 23x 8x 8x 2x 25x 6x 6x 6x 25x 6x 6x 25x 6x 6x 8x 8x 8x 25x 12x 12x 12x 2x 10x 10x 9x 10x 9x 9x 9x 12x 8x 8x 8x 1x 7x 7x 24x 7x 7x 8x 1x 1x | import { runQuery } from "@/lib/neo4j";
import { SOURCE_KIND_EXTENSION } from "@/lib/overview/observations";
import {
EXT_SOURCE_LINE,
EXT_SOURCE_REGION,
} from "@/lib/fhir-documents/shared";
/**
* Values the Klarbefund app sends into the patient's own record (#473
* phase 3, "insert my values").
*
* The app sends the FHIR R4 document it already writes for a report: Patient,
* DocumentReference, Observations, DiagnosticReport, Provenance. Only the
* Observations are taken, and only ones that say what they are:
*
* - `status: preliminary`. A value read from a photograph or typed in is
* never `final`; that is the laboratory's word, and only its own document
* carries it (ADR-033).
* - A LOINC code. A value LOINC does not code (a microbiome organism, the
* night-time dip of blood pressure) is reported back as skipped, not stored
* under a code it does not have.
* - A number with a UCUM unit, and a date.
*
* Its provenance (`ocr-transcribed`, `self-tracked`, `lab-issued-digital`)
* travels with it and is shown with it.
*/
const LOINC = "http://loinc.org";
const UCUM = "http://unitsofmeasure.org";
/** One report's worth, generously: a microbiome panel is about 80. */
export const MAX_OBSERVATIONS = 500;
export interface IncomingRow {
resourceId: string;
code: string;
display: string;
value: number;
unit: string;
low: number | null;
high: number | null;
rangeText: string | null;
effective: string;
category: string;
sourceKind: string | null;
/** The line on the paper the value was read from, as the app printed it. */
sourceLine: string | null;
/** `page@x,y,w,h` of that line, so a document can point at it (#1171). */
sourceRegion: string | null;
performer: string | null;
}
export interface Parsed {
rows: IncomingRow[];
/** Why each refused Observation was refused, by its id in the bundle. */
skipped: { id: string; reason: string }[];
}
type Json = Record<string, unknown>;
const obj = (v: unknown): Json =>
v && typeof v === "object" && !Array.isArray(v) ? (v as Json) : {};
const arr = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
const str = (v: unknown): string | null => (typeof v === "string" ? v : null);
const num = (v: unknown): number | null =>
typeof v === "number" && Number.isFinite(v) ? v : null;
/**
* The Observations of one report's bundle, as rows to store. Throws on a
* body that is not a FHIR Bundle at all.
*/
export function parseReportBundle(
body: unknown,
ids: { patientId: string; reportId: string },
): Parsed {
const bundle = obj(body);
if (bundle.resourceType !== "Bundle") {
throw new Error("the body is not a FHIR Bundle");
}
const resources = arr(bundle.entry).map((e) => obj(obj(e).resource));
// The laboratory that printed the report. The app names it as an
// Organization in the bundle and references it (#1172), keeping the name
// as the reference's display; an app built before that sends the display
// alone. The Organization's own name wins when the reference resolves.
const organizationName = (reference: Json): string | null => {
const id = str(reference.reference)?.match(/^Organization\/(.+)$/)?.[1];
const organization = resources.find(
(r) => r.resourceType === "Organization" && r.id === id,
);
return id && organization ? str(organization.name) : null;
};
const performer =
resources
.filter((r) => r.resourceType === "DiagnosticReport")
.map((r) => obj(arr(r.performer)[0]))
.map((p) => organizationName(p) ?? str(p.display))
.find(Boolean) ?? null;
const rows: IncomingRow[] = [];
const skipped: Parsed["skipped"] = [];
resources
.filter((r) => r.resourceType === "Observation")
.forEach((r, index) => {
const id = str(r.id) ?? `#${index + 1}`;
const refuse = (reason: string) => skipped.push({ id, reason });
if (r.status !== "preliminary") {
return refuse("only preliminary values are accepted from the app");
}
const code = obj(r.code);
const loinc = arr(code.coding)
.map(obj)
.find((c) => c.system === LOINC && str(c.code));
if (!loinc) return refuse("no LOINC code");
const quantity = obj(r.valueQuantity);
const value = num(quantity.value);
const unit = str(quantity.code) ?? str(quantity.unit);
if (value === null || !unit) return refuse("no number with a unit");
Iif (quantity.system && quantity.system !== UCUM) {
return refuse("the unit is not UCUM");
}
const effective = str(r.effectiveDateTime);
if (!effective || Number.isNaN(Date.parse(effective))) {
return refuse("no date");
}
const range = obj(arr(r.referenceRange)[0]);
const extensions = arr(r.extension).map(obj);
const extension = (url: string) => extensions.find((e) => e.url === url);
const source = extension(SOURCE_KIND_EXTENSION);
rows.push({
resourceId: `${ids.patientId}-${ids.reportId}-${index + 1}`,
code: str(loinc.code)!,
display:
str(code.text) ?? str(loinc.display) ?? str(loinc.code) ?? "Unknown",
value,
unit,
low: num(obj(range.low).value),
high: num(obj(range.high).value),
rangeText: str(range.text),
effective,
category:
str(obj(arr(obj(arr(r.category)[0]).coding)[0]).code) ?? "laboratory",
sourceKind: str(source?.valueCode) ?? str(source?.valueString),
sourceLine: str(extension(EXT_SOURCE_LINE)?.valueString),
sourceRegion: str(extension(EXT_SOURCE_REGION)?.valueString),
performer,
});
});
return { rows, skipped };
}
/**
* Replaces one report's values in the sandbox record with these. Sending a
* report again therefore updates it rather than doubling it. Returns how
* many are stored. Never touches a record that is not a sandbox.
*/
export async function storeReportRows(input: {
patientId: string;
reportId: string;
rows: IncomingRow[];
}): Promise<number> {
const result = await runQuery<{ stored: number }>(
`MATCH (p:Patient {id: $patientId, sandbox: true})
OPTIONAL MATCH (p)-[:HAS_OBSERVATION]->(old:Observation {sourceReport: $reportId})
WITH p, collect(old) AS previous
FOREACH (n IN previous | DETACH DELETE n)
WITH p
UNWIND $rows AS r
MERGE (o:Observation {resourceId: r.resourceId})
ON CREATE SET o.createdAt = datetime()
SET o.code = r.code, o.display = r.display, o.valueQuantity = r.value,
o.unit = r.unit, o.referenceLow = r.low, o.referenceHigh = r.high,
o.referenceText = r.rangeText, o.effectiveDate = r.effective,
o.category = r.category, o.status = 'preliminary',
o.sourceKind = r.sourceKind, o.sourceLine = r.sourceLine,
o.sourceRegion = r.sourceRegion, o.performer = r.performer,
o.sourceReport = $reportId, o.contributedBy = 'klarbefund-app'
MERGE (p)-[:HAS_OBSERVATION]->(o)
RETURN count(o) AS stored`,
input,
);
return result[0]?.stored ?? 0;
}
|