Files
WebApp/src/app/features/indicadores/paciente-detalle/paciente-detalle.state.ts
T
Angel Capetillo Zavala 3b246b4f1f Identify follow-up patients by pac_id
Rows name their patient with pac_id (a decimal string) or the legacy
id_seudonimo. pac_id wins; an invalid pac_id is never replaced by the
legacy field, and a page that repeats a patient or mixes both fields is
not drawn or exported. The table, patient record, PDF and spreadsheet
follow the announced identifier column and label. CaCu requests risk
only with a pac_id.
2026-11-13 18:19:27 -06:00

75 lines
3.3 KiB
TypeScript

import { computed, DestroyRef, Injectable, inject, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subject, catchError, defer, map, of, switchMap } from 'rxjs';
import { SeguimientoPaciente } from '../cacu-seguimiento.models';
import { identidadDe } from '../paciente-identidad';
import { PacienteDetalleEstado, PacienteResumen } from './paciente-detalle.models';
import { PACIENTE_DETALLE_PROVIDER } from './paciente-detalle.provider';
import { AltaRegistroTemporal } from './paciente-registro.models';
import { PacienteRegistrosSesion } from './paciente-registros-sesion';
/** Selección por instancia de Seguimiento; las altas temporales viven aparte en la sesión Angular. */
@Injectable()
export class PacienteDetalleState {
private readonly provider = inject(PACIENTE_DETALLE_PROVIDER);
private readonly sesion = inject(PacienteRegistrosSesion);
private readonly seleccion = new Subject<PacienteResumen | null>();
private readonly actual = signal<PacienteDetalleEstado>({ tipo: 'cerrado' });
readonly estado = this.actual.asReadonly();
readonly registrosTemporales = computed(() => {
const estado = this.actual();
return estado.tipo === 'cerrado' ? [] : this.sesion.paraPaciente(estado.resumen.identidad.id);
});
constructor() {
this.seleccion.pipe(
switchMap(resumen => resumen ? defer(() => this.provider.obtener(resumen.identidad.id)).pipe(
map(detalle => {
if (detalle && (detalle.id_seudonimo !== resumen.identidad.id || detalle.origen !== 'sintetico')) {
throw new Error('Detalle incompatible con la selección');
}
return detalle
? { tipo: 'disponible', resumen, detalle } as const
: { tipo: 'no-disponible', resumen } as const;
}),
catchError(() => of({ tipo: 'error', resumen } as const)),
) : of({ tipo: 'cerrado' } as const)),
takeUntilDestroyed(inject(DestroyRef)),
).subscribe(estado => this.actual.set(estado));
}
abrir(paciente: SeguimientoPaciente, corte: string) {
const identidad = identidadDe(paciente);
if (!identidad) return;
const resumen: PacienteResumen = {
identidad,
grupoEdad: paciente.grupo_edad === 'sin_dato' ? null : paciente.grupo_edad,
estatus: paciente.estatus.etiqueta,
corte,
origen: 'seguimiento-demostracion',
citologia: paciente.ultima_citologia ? {
fecha: paciente.ultima_citologia.fecha,
resultado: paciente.ultima_citologia.bethesda,
calidad: paciente.ultima_citologia.calidad,
} : null,
vph: paciente.ultimo_vph ? {
fecha: paciente.ultimo_vph.fecha,
resultado: paciente.ultimo_vph.resultado,
genotipo16y18: paciente.ultimo_vph.genotipo_16_18,
} : null,
diasTomaDiagnostico: paciente.dias_toma_diagnostico,
prioridad: paciente.prioridad?.etiqueta ?? null,
};
this.actual.set({ tipo: 'cargando', resumen });
this.seleccion.next(resumen);
}
cerrar() { this.seleccion.next(null); }
agregarRegistro(alta: AltaRegistroTemporal) {
const estado = this.actual();
if (estado.tipo === 'cerrado' || estado.resumen.identidad.id !== alta.paciente.id) return false;
return this.sesion.agregar(alta.paciente.id, alta.registro);
}
}