CaMa indicators and follow-up list, model governance page

This commit is contained in:
Roberto Treviño
2026-11-25 23:20:52 -06:00
parent 9b02641804
commit 53a6393cb2
19 changed files with 974 additions and 19 deletions
+75 -3
View File
@@ -24,14 +24,18 @@ const AGE_GROUPS: [number, number][] = [
[55, 64],
];
export function ageGroup(birth: string | undefined, at: string): string {
export function ageGroup(
birth: string | undefined,
at: string,
groups: [number, number][] = AGE_GROUPS,
): string {
if (!birth) return 'Sin dato';
const [by, bm, bd] = birth.split('-').map(Number);
const [y, m, d] = at.split('-').map(Number);
const age = y - by - (m < bm || (m === bm && d < bd) ? 1 : 0);
const found = AGE_GROUPS.find(([lo, hi]) => lo <= age && age <= hi);
const found = groups.find(([lo, hi]) => lo <= age && age <= hi);
if (found) return `${found[0]}-${found[1]}`;
return age < AGE_GROUPS[0][0] ? `<${AGE_GROUPS[0][0]}` : `${AGE_GROUPS.at(-1)![1] + 1}+`;
return age < groups[0][0] ? `<${groups[0][0]}` : `${groups.at(-1)![1] + 1}+`;
}
/** Her current risk of an outcome at a horizon: from the latest index date any of the
@@ -132,3 +136,71 @@ export function windowStart(cutoff: string, months: number): string {
const day = new Date(Date.UTC(year, month, Math.min(d, last) + 1));
return day.toISOString().slice(0, 10);
}
/** One row of CaMa's follow-up list: her latest mammogram reported by the cut-off, where the
* biopsy it led to stands, and how soon she should be seen. */
export interface CamaRow {
patient: Patient;
name?: string;
curp?: string;
status: string;
priority: string;
ageGroup: string;
mammogram: { date: string; birads?: string };
biopsy?: { date: string; histology?: string };
}
const CAMA_AGE_GROUPS: [number, number][] = [
[40, 49],
[50, 59],
[60, 69],
];
export function camaRows(page: Bundle, cutoff: string): CamaRow[] {
const reports = resources<DiagnosticReport>(page, 'DiagnosticReport');
const observations = new Map(resources<Observation>(page, 'Observation').map((o) => [o.id, o]));
const results = (r: DiagnosticReport) =>
new Map(
(r.result ?? [])
.map((x) => observations.get(x.reference?.split('/')[1]))
.filter((o) => !!o)
.map((o) => [code(o!.code), o!]),
);
const reported = (r: DiagnosticReport) => ext(r, 'report-date')?.valueDate;
return resources<Patient>(page, 'Patient', 'match').map((patient) => {
const mine = reports.filter((r) => r.subject?.reference === `Patient/${patient.id}`);
const mammogram = mine
.filter((r) => code(r.code) === 'mammography' && (reported(r) ?? '9') <= cutoff)
.sort((a, b) => a.effectiveDateTime!.localeCompare(b.effectiveDateTime!))
.at(-1);
const exam = mammogram?.effectiveDateTime ?? '';
const biopsy = mine
.filter(
(r) =>
code(r.code) === 'breast_biopsy' &&
results(r).get('breast_biopsy.mammogram_exam_date')?.valueDateTime === exam &&
exam <= (reported(r) ?? '') &&
reported(r)! <= cutoff,
)
.sort((a, b) => reported(a)!.localeCompare(reported(b)!))
.at(-1);
return {
patient,
name: patient.name?.[0]?.text,
curp: patient.identifier?.find((i) => i.system === CURP)?.value,
status: ext(patient, 'follow-up-status')?.valueCoding?.code ?? '',
priority: ext(patient, 'follow-up-priority')?.valueCoding?.code ?? '',
ageGroup: ageGroup(patient.birthDate, cutoff, CAMA_AGE_GROUPS),
mammogram: {
date: exam,
birads: mammogram
? code(results(mammogram).get('mammography.birads')?.valueCodeableConcept)
: undefined,
},
biopsy: biopsy && {
date: reported(biopsy)!,
histology: code(results(biopsy).get('breast_biopsy.histology')?.valueCodeableConcept),
},
};
});
}