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 | 3x 25x 24x 4x 4x 4x 2x 2x | import { runQuery } from "@/lib/neo4j";
import type { DocumentPatient, Run } from "./shared";
import {
groupLabReports,
type LabObservationRow,
type LabReportInput,
} from "./lab-report";
import type {
AllergyRow,
ConditionRow,
ImmunizationRow,
MedicationRow,
PatientSummaryInput,
ProcedureRow,
} from "./patient-summary";
/**
* The graph reads behind the two documents (#1171). Primary use only: a
* person's own record, read for that person or for the holder treating them.
* Nothing here runs in a secondary-use session; research reads go through
* the proxy, which drops Art. 71 opt-outs (ADR-062).
*
* The seeds disagree on property names (`id` or `resourceId`, `status` or
* `clinicalStatus`, `date` or `effectiveDate`); each query coalesces them, as
* `loadObservationBundle` does.
*/
const PATIENT_MATCH = `MATCH (p:Patient)
WHERE coalesce(p.id, p.resourceId, elementId(p)) = $patientId`;
export async function loadDocumentPatient(
patientId: string,
run: Run = runQuery,
): Promise<DocumentPatient | null> {
const rows = await run<DocumentPatient>(
`${PATIENT_MATCH}
RETURN coalesce(p.id, p.resourceId, elementId(p)) AS id,
p.name AS name,
p.gender AS gender,
toString(p.birthDate) AS birthDate
LIMIT 1`,
{ patientId },
);
return rows[0] ?? null;
}
/** Every laboratory report of the record, newest first. */
export async function loadLabReports(
patientId: string,
run: Run = runQuery,
): Promise<LabReportInput[]> {
const rows = await run<LabObservationRow>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_OBSERVATION]->(o:Observation)
WHERE o.code IS NOT NULL
AND (
(o.valueQuantity IS NOT NULL AND coalesce(o.category, 'laboratory') = 'laboratory')
OR (o.valueQuantity IS NULL AND o.value IS NOT NULL AND o.category = 'laboratory')
)
OPTIONAL MATCH (d:DiagnosticReport)-[:HAS_RESULT]->(o)
WHERE d.category IN ['LAB', 'laboratory']
WITH o, head(collect(d)) AS d
RETURN coalesce(o.resourceId, o.id, elementId(o)) AS id,
o.code AS code,
coalesce(o.display, o.name, o.code) AS display,
toFloat(coalesce(o.valueQuantity, o.value)) AS value,
coalesce(o.unit, o.valueUnit, '1') AS unit,
toFloat(o.referenceLow) AS low,
toFloat(o.referenceHigh) AS high,
o.referenceText AS rangeText,
coalesce(o.dateTime, toString(o.effectiveDate), o.date, '') AS effective,
o.status AS status,
o.performer AS performer,
o.sourceKind AS sourceKind,
o.sourceLine AS sourceLine,
o.sourceRegion AS sourceRegion,
o.sourceReport AS sourceReport,
coalesce(d.resourceId, d.id) AS reportNodeId,
coalesce(d.name, d.display) AS reportTitle,
d.issued AS reportIssued,
d.conclusion AS reportConclusion
ORDER BY effective, code
LIMIT 2000`,
{ patientId },
);
return groupLabReports(
rows.filter((r) => r.effective && Number.isFinite(r.value)),
patientId,
);
}
export async function loadPatientSummaryInput(
patientId: string,
run: Run = runQuery,
): Promise<PatientSummaryInput> {
const [
conditions,
requests,
statements,
immunizations,
procedures,
allergies,
] = await Promise.all([
run<ConditionRow>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_CONDITION]->(c:Condition)
RETURN coalesce(c.resourceId, c.id, elementId(c)) AS id,
c.code AS code,
coalesce(c.codeSystem, c.system) AS system,
coalesce(c.display, c.name, c.code, 'Condition') AS display,
coalesce(c.clinicalStatus, c.status) AS clinicalStatus,
toString(c.onsetDate) AS onset
ORDER BY onset, id
LIMIT 500`,
{ patientId },
),
run<Omit<MedicationRow, "kind" | "dosage" | "reason">>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_MEDICATION_REQUEST|HAS_MEDICATION]->(m:MedicationRequest)
WITH DISTINCT m
RETURN coalesce(m.resourceId, m.id, elementId(m)) AS id,
coalesce(m.medicationCode, m.code) AS code,
coalesce(m.codeSystem, m.system) AS system,
coalesce(m.display, m.name, 'Medication') AS display,
m.status AS status,
toString(m.date) AS date
ORDER BY date, id
LIMIT 500`,
{ patientId },
),
run<Omit<MedicationRow, "kind">>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_MEDICATION_STATEMENT]->(m:MedicationStatement)
RETURN coalesce(m.resourceId, m.id, elementId(m)) AS id,
m.medicationCode AS code,
m.codeSystem AS system,
coalesce(m.display, m.name, 'Medication') AS display,
m.status AS status,
toString(m.effectiveStart) AS date,
m.dosageText AS dosage,
m.reason AS reason
ORDER BY date, id
LIMIT 500`,
{ patientId },
),
run<ImmunizationRow>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_IMMUNIZATION]->(i:Immunization)
WHERE i.vaccineCode IS NOT NULL
RETURN coalesce(i.resourceId, i.id, elementId(i)) AS id,
i.vaccineCode AS code,
i.codeSystem AS system,
coalesce(i.display, i.name, 'Vaccination') AS display,
i.status AS status,
toString(i.occurrenceDate) AS occurrence,
i.lotNumber AS lot,
i.performer AS performer
ORDER BY occurrence, id
LIMIT 500`,
{ patientId },
),
run<ProcedureRow>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_PROCEDURE]->(x:Procedure)
RETURN coalesce(x.resourceId, x.id, elementId(x)) AS id,
x.code AS code,
coalesce(x.codeSystem, x.system) AS system,
coalesce(x.display, x.name, x.code, 'Procedure') AS display,
x.status AS status,
toString(coalesce(x.performedStart, x.performedDate, x.date)) AS performed
ORDER BY performed, id
LIMIT 500`,
{ patientId },
),
run<AllergyRow>(
`${PATIENT_MATCH}
MATCH (p)-[:HAS_ALLERGY_INTOLERANCE]->(a:AllergyIntolerance)
RETURN coalesce(a.resourceId, a.id, elementId(a)) AS id,
a.code AS code,
coalesce(a.codeSystem, a.system) AS system,
coalesce(a.display, a.name, a.code, 'Allergy') AS display,
a.clinicalStatus AS clinicalStatus,
a.verificationStatus AS verificationStatus,
a.type AS type,
a.category AS category,
a.criticality AS criticality,
toString(a.onsetDate) AS onset
ORDER BY onset, id
LIMIT 500`,
{ patientId },
),
]);
return {
conditions,
allergies,
medications: [
...statements.map((m) => ({ ...m, kind: "statement" as const })),
...requests.map((m) => ({
...m,
kind: "request" as const,
dosage: null,
reason: null,
})),
],
immunizations: immunizations.filter((i) => i.occurrence),
procedures,
};
}
|