253 lines
9.2 KiB
TypeScript
253 lines
9.2 KiB
TypeScript
import { HttpErrorResponse } from '@angular/common/http';
|
|
import { Component, input } from '@angular/core';
|
|
import type { Parameters, ParametersParameter } from 'fhir/r4';
|
|
|
|
/** The clinical scenarios the backend publishes, as the screens name them (the API's codes stay). */
|
|
export const SCENARIO_TEXT: Record<string, string> = {
|
|
surveillance: 'Vigilancia',
|
|
pre_colposcopy: 'Precolposcopia',
|
|
post_colposcopy: 'Postcolposcopia',
|
|
post_colposcopy_second_visit: 'Segunda visita postcolposcopia',
|
|
post_treatment: 'Postratamiento',
|
|
survival: 'Supervivencia',
|
|
};
|
|
|
|
/** The exclusion criteria the backend publishes, as the screens say them. */
|
|
export const CRITERION_TEXT: Record<string, string> = {
|
|
hysterectomy: 'Histerectomía previa al índice',
|
|
under_18: 'Edad menor de 18 años al índice',
|
|
prior_outcome: 'Resultado CIN2+ previo al índice',
|
|
no_index_result: 'Sin resultado de citología o VPH en la visita índice',
|
|
unreported_biopsy: 'Biopsia solicitada sin resultado',
|
|
outcome_by_index: 'Resultado CIN2+ hasta la visita índice',
|
|
cancer_history: 'Antecedente de cáncer cervicouterino',
|
|
prior_treatment: 'Tratamiento previo a la visita índice',
|
|
no_referral: 'Sin resultado que sustente la referencia a colposcopia',
|
|
no_cotest: 'Sin cotest de citología y VPH en la visita índice',
|
|
no_pretreatment_biopsy: 'Sin biopsia previa al tratamiento en el periodo requerido',
|
|
outcome_before_index: 'Resultado CIN2+ entre tratamiento e índice',
|
|
no_birth_date: 'Fecha de nacimiento desconocida',
|
|
died_by_index: 'Defunción en o antes de la visita índice',
|
|
};
|
|
|
|
export interface Reason {
|
|
/** the API's code, untouched */
|
|
code: string;
|
|
text: string;
|
|
}
|
|
export interface Visit {
|
|
indexDate: string;
|
|
/** the sample the visit is evaluated at, when the backend names one */
|
|
sampleId?: string;
|
|
/** the backend's decision; it is never derived from the reasons */
|
|
eligible: boolean;
|
|
reasons: Reason[];
|
|
}
|
|
export interface ScenarioGroup {
|
|
code: string;
|
|
text: string;
|
|
visits: Visit[];
|
|
}
|
|
|
|
/** What the section can be in; none of them says "eligible" unless the backend did. */
|
|
export type EligibilityState =
|
|
| { kind: 'loading' }
|
|
| { kind: 'error' }
|
|
| { kind: 'unavailable' }
|
|
| { kind: 'gold'; text?: string }
|
|
| { kind: 'none'; cutoff?: string }
|
|
| { kind: 'evaluated'; cutoff?: string; groups: ScenarioGroup[] };
|
|
|
|
type Part = ParametersParameter;
|
|
const value = (p?: Part): unknown =>
|
|
p?.valueCode ?? p?.valueString ?? p?.valueDate ?? p?.valueBoolean ?? undefined;
|
|
const named = (parts: Part[] | undefined, name: string) =>
|
|
(parts ?? []).filter((p) => p.name === name);
|
|
const coding = (p?: Part) => p?.valueCoding;
|
|
|
|
/** The answer of `$eligibility`, grouped by scenario and keeping the visits apart: each visit has
|
|
* its own decision and its own reasons. A code the screens do not know shows the display the API
|
|
* gave, else the code itself. An answer that is not what the contract says is "unavailable". */
|
|
export function eligibilityView(answer: Parameters | null | undefined): EligibilityState {
|
|
const top = answer?.parameter;
|
|
if (answer?.resourceType !== 'Parameters' || !Array.isArray(top)) return { kind: 'unavailable' };
|
|
const status = value(named(top, 'status')[0]);
|
|
const cutoff = value(named(top, 'cutoff')[0]) as string | undefined;
|
|
const candidates = named(top, 'candidate');
|
|
if (status === 'no_candidate' && !candidates.length) return { kind: 'none', cutoff };
|
|
if (status !== 'evaluated' || !candidates.length) return { kind: 'unavailable' };
|
|
const groups = new Map<string, ScenarioGroup>();
|
|
for (const c of candidates) {
|
|
const scenario = coding(named(c.part, 'scenario')[0]);
|
|
const indexDate = value(named(c.part, 'index-date')[0]) as string | undefined;
|
|
const eligible = value(named(c.part, 'eligible')[0]);
|
|
if (!scenario?.code || !indexDate || typeof eligible !== 'boolean')
|
|
return { kind: 'unavailable' };
|
|
const group = groups.get(scenario.code) ?? {
|
|
code: scenario.code,
|
|
text: SCENARIO_TEXT[scenario.code] ?? scenario.display ?? scenario.code,
|
|
visits: [],
|
|
};
|
|
groups.set(scenario.code, group);
|
|
group.visits.push({
|
|
indexDate,
|
|
sampleId: value(named(c.part, 'index-sample-id')[0]) as string | undefined,
|
|
eligible,
|
|
reasons: named(c.part, 'criterion').flatMap((r) => {
|
|
const k = coding(r);
|
|
return k?.code
|
|
? [{ code: k.code, text: CRITERION_TEXT[k.code] ?? k.display ?? k.code }]
|
|
: [];
|
|
}),
|
|
});
|
|
}
|
|
for (const g of groups.values()) g.visits.sort((a, b) => b.indexDate.localeCompare(a.indexDate));
|
|
return { kind: 'evaluated', cutoff, groups: [...groups.values()] };
|
|
}
|
|
|
|
/** What the answer's failure says: the gold must be updated (409), the patient has no such
|
|
* operation (404, 422), or the call itself failed (anything else, including no connection). */
|
|
export function eligibilityFailure(error: unknown): EligibilityState {
|
|
if (error instanceof HttpErrorResponse) {
|
|
if (error.status === 409) {
|
|
const issue = (error.error as { issue?: { diagnostics?: string }[] } | null)?.issue?.[0];
|
|
return { kind: 'gold', text: issue?.diagnostics };
|
|
}
|
|
if (error.status === 404 || error.status === 422) return { kind: 'unavailable' };
|
|
}
|
|
return { kind: 'error' };
|
|
}
|
|
|
|
const plural = (n: number, one: string, many: string) => `${n} ${n === 1 ? one : many}`;
|
|
|
|
/** "Elegibilidad por escenario clínico": the decisions the backend publishes for each indexed visit,
|
|
* one scenario at a time. It shows them; it decides nothing. */
|
|
@Component({
|
|
selector: 'app-eligibility',
|
|
template: `
|
|
@let s = state();
|
|
@switch (s.kind) {
|
|
@case ('loading') {
|
|
<p class="state" role="status" data-state="loading">Consultando la elegibilidad…</p>
|
|
}
|
|
@case ('error') {
|
|
<p class="state" role="alert" data-state="error">
|
|
No se pudo consultar la elegibilidad. Intente de nuevo más tarde.
|
|
</p>
|
|
}
|
|
@case ('unavailable') {
|
|
<p class="state" role="status" data-state="unavailable">
|
|
Información de elegibilidad no disponible para esta paciente.
|
|
</p>
|
|
}
|
|
@case ('gold') {
|
|
<p class="state" role="status" data-state="gold">
|
|
La elegibilidad requiere actualizar la capa gold del servidor; todavía no está disponible.
|
|
</p>
|
|
@if (s.text) {
|
|
<p class="muted detail">{{ s.text }}</p>
|
|
}
|
|
}
|
|
@case ('none') {
|
|
<p class="state" role="status" data-state="none">
|
|
Sin candidatos: la paciente no tiene visitas indexadas en estos escenarios
|
|
@if (s.cutoff) {
|
|
a la fecha de corte {{ s.cutoff }}
|
|
}
|
|
. Esto no significa que sea elegible.
|
|
</p>
|
|
}
|
|
@case ('evaluated') {
|
|
@for (g of s.groups; track g.code) {
|
|
<details class="scenario" [attr.data-scenario]="g.code" [open]="g.visits.length <= 2">
|
|
<summary>
|
|
<strong>{{ g.text }}</strong>
|
|
<span class="muted">
|
|
· {{ plural(g.visits.length, 'visita', 'visitas') }} ·
|
|
{{ plural(count(g, true), 'elegible', 'elegibles') }},
|
|
{{ plural(count(g, false), 'no elegible', 'no elegibles') }}
|
|
</span>
|
|
</summary>
|
|
<ul class="visits">
|
|
@for (v of g.visits; track $index) {
|
|
<li class="visit" [attr.data-eligible]="v.eligible">
|
|
<p class="head">
|
|
<span class="badge" [class.ok]="v.eligible">{{
|
|
v.eligible ? 'Elegible' : 'No elegible'
|
|
}}</span>
|
|
Fecha índice {{ v.indexDate }}
|
|
<span class="muted"
|
|
>· {{ v.sampleId ? 'Muestra ' + v.sampleId : 'Sin muestra asociada' }}</span
|
|
>
|
|
</p>
|
|
@if (v.reasons.length) {
|
|
<ul class="why" aria-label="Motivos de exclusión">
|
|
@for (r of v.reasons; track r.code) {
|
|
<li [attr.data-code]="r.code">{{ r.text }}</li>
|
|
}
|
|
</ul>
|
|
}
|
|
</li>
|
|
}
|
|
</ul>
|
|
</details>
|
|
}
|
|
@if (s.cutoff) {
|
|
<p class="muted note">Visitas hasta la fecha de corte {{ s.cutoff }}.</p>
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
styles: `
|
|
.note,
|
|
.detail {
|
|
font-size: 12px;
|
|
}
|
|
.state {
|
|
margin: 8px 0;
|
|
}
|
|
.scenario {
|
|
border-top: 1px solid #dce3e7;
|
|
padding: 6px 0;
|
|
}
|
|
.scenario summary {
|
|
cursor: pointer;
|
|
}
|
|
.visits {
|
|
list-style: none;
|
|
margin: 6px 0 0;
|
|
padding: 0;
|
|
}
|
|
.visit {
|
|
padding: 4px 0 4px 12px;
|
|
}
|
|
.head {
|
|
margin: 0;
|
|
}
|
|
.badge {
|
|
background: #f3d6dc;
|
|
border-radius: 10px;
|
|
color: #7a1f36;
|
|
font-size: 12px;
|
|
font-weight: 500;
|
|
margin-right: 6px;
|
|
padding: 1px 8px;
|
|
}
|
|
.badge.ok {
|
|
background: #dcefe4;
|
|
color: #1d5a3b;
|
|
}
|
|
.why {
|
|
margin: 2px 0 0;
|
|
padding-left: 22px;
|
|
}
|
|
`,
|
|
})
|
|
export class Eligibility {
|
|
readonly state = input.required<EligibilityState>();
|
|
protected plural = plural;
|
|
protected count(g: ScenarioGroup, eligible: boolean) {
|
|
return g.visits.filter((v) => v.eligible === eligible).length;
|
|
}
|
|
}
|