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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 7x 2x 2x 3x 2x 2x 2x 1x 1x 1x | import { runQuery } from "@/lib/neo4j";
import type { FhirRecordResource } from "@/lib/overview/observations";
/**
* The resources a record holds besides laboratory Observations (ADR-059),
* as FHIR R4: DiagnosticReports with their results (a resting ECG, an imaging
* report), Immunizations (the vaccination record) and MedicationStatements
* (the medication plan). `/api/patient/observations?include=all` appends them
* to the Bundle, so an ePA or EHR integrator receives the whole record in
* one answer; without `include` the Bundle stays Observations only, which is
* what the Klarbefund app parses.
*/
interface ReportRow {
id: string;
code: string;
display: string;
category: string;
categoryDisplay: string | null;
status: string | null;
effective: string;
issued: string | null;
performer: string | null;
conclusion: string | null;
results: string[];
}
interface ImmunizationRow {
id: string;
code: string;
system: string | null;
display: string;
status: string | null;
occurrence: string;
lot: string | null;
performer: string | null;
}
interface MedicationStatementRow {
id: string;
code: string;
system: string | null;
display: string;
status: string | null;
dosage: string | null;
dose: number | null;
doseUnit: string | null;
reason: string | null;
since: string;
planId: string | null;
planPrinted: string | null;
source: string | null;
}
function coding(code: string, system: string | null, display: string) {
return {
coding: [{ system: system ?? undefined, code, display }],
text: display,
};
}
export function reportToFhir(r: ReportRow): FhirRecordResource {
return {
resourceType: "DiagnosticReport",
id: r.id,
status: r.status ?? "final",
category: [
{
coding: [
{
system: "http://terminology.hl7.org/CodeSystem/v2-0074",
code: r.category,
display: r.categoryDisplay ?? undefined,
},
],
},
],
code: coding(r.code, "http://loinc.org", r.display),
effectiveDateTime: r.effective,
issued: r.issued ?? undefined,
performer: r.performer ? [{ display: r.performer }] : undefined,
result: r.results.map((id) => ({ reference: `urn:uuid:${id}` })),
conclusion: r.conclusion ?? undefined,
};
}
export function immunizationToFhir(r: ImmunizationRow): FhirRecordResource {
return {
resourceType: "Immunization",
id: r.id,
status: r.status ?? "completed",
vaccineCode: coding(r.code, r.system, r.display),
occurrenceDateTime: r.occurrence,
lotNumber: r.lot ?? undefined,
performer: r.performer ? [{ actor: { display: r.performer } }] : undefined,
};
}
export function medicationStatementToFhir(
r: MedicationStatementRow,
): FhirRecordResource {
return {
resourceType: "MedicationStatement",
id: r.id,
status: r.status ?? "active",
medicationCodeableConcept: coding(r.code, r.system, r.display),
effectivePeriod: { start: r.since },
reasonCode: r.reason ? [{ text: r.reason }] : undefined,
dosage: r.dosage
? [
{
text: r.dosage,
doseAndRate:
r.dose !== null
? [
{
doseQuantity: {
value: r.dose,
unit: r.doseUnit ?? undefined,
},
},
]
: undefined,
},
]
: undefined,
identifier: r.planId
? [{ system: "urn:klarbefund:medication-plan", value: r.planId }]
: undefined,
note: r.source
? [
{
text: r.planPrinted
? `${r.source}, printed ${r.planPrinted}`
: r.source,
},
]
: undefined,
};
}
/** Every non-Observation resource of one record, oldest first per kind. */
export async function loadRecordExtras(
patientId: string,
): Promise<FhirRecordResource[]> {
const [reports, immunizations, statements] = await Promise.all([
runQuery<ReportRow>(
`MATCH (p:Patient)-[:HAS_DIAGNOSTIC_REPORT]->(d:DiagnosticReport)
WHERE coalesce(p.id, p.resourceId, elementId(p)) = $patientId
OPTIONAL MATCH (d)-[:HAS_RESULT]->(o:Observation)
WITH d, collect(coalesce(o.resourceId, elementId(o))) AS results
RETURN coalesce(d.resourceId, elementId(d)) AS id,
coalesce(d.code, '') AS code,
coalesce(d.display, d.name, d.code, 'Report') AS display,
coalesce(d.category, 'OTH') AS category,
d.categoryDisplay AS categoryDisplay,
d.status AS status,
coalesce(toString(d.effectiveDate), d.issued, '') AS effective,
d.issued AS issued,
d.performer AS performer,
d.conclusion AS conclusion,
results
ORDER BY effective
LIMIT 200`,
{ patientId },
),
runQuery<ImmunizationRow>(
`MATCH (p:Patient)-[:HAS_IMMUNIZATION]->(i:Immunization)
WHERE coalesce(p.id, p.resourceId, elementId(p)) = $patientId
RETURN coalesce(i.resourceId, elementId(i)) AS id,
coalesce(i.vaccineCode, '') AS code,
i.codeSystem AS system,
coalesce(i.display, i.name, 'Vaccination') AS display,
i.status AS status,
coalesce(toString(i.occurrenceDate), '') AS occurrence,
i.lotNumber AS lot,
i.performer AS performer
ORDER BY occurrence
LIMIT 200`,
{ patientId },
),
runQuery<MedicationStatementRow>(
`MATCH (p:Patient)-[:HAS_MEDICATION_STATEMENT]->(m:MedicationStatement)
WHERE coalesce(p.id, p.resourceId, elementId(p)) = $patientId
RETURN coalesce(m.resourceId, elementId(m)) AS id,
coalesce(m.medicationCode, '') AS code,
m.codeSystem AS system,
coalesce(m.display, m.name, 'Medication') AS display,
m.status AS status,
m.dosageText AS dosage,
m.dose AS dose,
m.doseUnit AS doseUnit,
m.reason AS reason,
coalesce(toString(m.effectiveStart), '') AS since,
m.planId AS planId,
toString(m.planPrinted) AS planPrinted,
m.source AS source
ORDER BY since
LIMIT 200`,
{ patientId },
),
]);
return [
...reports.filter((r) => r.code).map(reportToFhir),
...immunizations.filter((r) => r.code).map(immunizationToFhir),
...statements.filter((r) => r.code).map(medicationStatementToFhir),
];
}
|