72 lines
3.2 KiB
TypeScript
72 lines
3.2 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 { 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.id_seudonimo);
|
|
});
|
|
|
|
constructor() {
|
|
this.seleccion.pipe(
|
|
switchMap(resumen => resumen ? defer(() => this.provider.obtener(resumen.id_seudonimo)).pipe(
|
|
map(detalle => {
|
|
if (detalle && (detalle.id_seudonimo !== resumen.id_seudonimo || 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 resumen: PacienteResumen = {
|
|
id_seudonimo: paciente.id_seudonimo,
|
|
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.id_seudonimo !== alta.id_seudonimo) return false;
|
|
return this.sesion.agregar(alta.id_seudonimo, alta.registro);
|
|
}
|
|
}
|