From edb868f3727481e57998982c254442e2e6ef24cf Mon Sep 17 00:00:00 2001 From: edgar Date: Wed, 11 Nov 2026 23:25:08 -0600 Subject: [PATCH] Patient record reads its risk from GET /cacu/pacientes//riesgo, by the id the follow-up list shows --- .../paciente-detalle/paciente-detalle.html | 2 +- .../paciente-detalle/paciente-detalle.spec.ts | 5 +++-- .../paciente-registro-integracion.spec.ts | 6 +++-- .../paciente-riesgo-tabla.html | 1 - .../paciente-detalle/paciente-riesgo-tabla.ts | 10 +++++++-- .../paciente-riesgo.models.ts | 4 ++-- .../paciente-riesgo.provider.ts | 16 ++++++++++---- .../paciente-detalle/paciente-riesgo.spec.ts | 14 +++++++----- .../paciente-detalle/paciente-riesgo.state.ts | 22 ++++++++++++------- 9 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/app/features/indicadores/paciente-detalle/paciente-detalle.html b/src/app/features/indicadores/paciente-detalle/paciente-detalle.html index d3d931a..00f6bd0 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-detalle.html +++ b/src/app/features/indicadores/paciente-detalle/paciente-detalle.html @@ -79,7 +79,7 @@

Riesgos y recomendaciones

- +
} diff --git a/src/app/features/indicadores/paciente-detalle/paciente-detalle.spec.ts b/src/app/features/indicadores/paciente-detalle/paciente-detalle.spec.ts index ecd3f67..7f6ad00 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-detalle.spec.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-detalle.spec.ts @@ -108,7 +108,8 @@ describe('Ficha individual desde Seguimiento CaCu', () => { let root: HTMLElement; beforeEach(() => { - TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] }); + TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting(), + { provide: PACIENTE_RIESGO_PROVIDER, useValue: { obtener: () => of(null) } }] }); fixture = TestBed.createComponent(IndicadoresTracking); root = fixture.nativeElement; http = TestBed.inject(HttpTestingController); @@ -247,7 +248,7 @@ describe('Ficha individual desde Seguimiento CaCu', () => { expect(tarjeta.textContent).toContain('Información simulada'); expect(tarjeta.querySelectorAll('tbody tr')).toHaveLength(3); expect(tarjeta.textContent).not.toContain(pacientes[0].id_seudonimo); - expect(obtener).toHaveBeenCalledExactlyOnceWith(); + expect(obtener).toHaveBeenCalledExactlyOnceWith(pacientes[0].id_seudonimo); volver(); const otra = abrir(1); expect(otra.textContent).toContain('Riesgo individual no disponible para esta paciente.'); diff --git a/src/app/features/indicadores/paciente-detalle/paciente-registro-integracion.spec.ts b/src/app/features/indicadores/paciente-detalle/paciente-registro-integracion.spec.ts index dfdb390..853caeb 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-registro-integracion.spec.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-registro-integracion.spec.ts @@ -7,12 +7,13 @@ import { MatSelectHarness } from '@angular/material/select/testing'; import { MatInputHarness } from '@angular/material/input/testing'; import { MatAutocompleteHarness } from '@angular/material/autocomplete/testing'; import { MatDialog } from '@angular/material/dialog'; -import { firstValueFrom } from 'rxjs'; +import { firstValueFrom, of } from 'rxjs'; import fixtureData from '../fixtures/cacu_seguimiento.json'; import { seguimientoResponse } from '../cacu-seguimiento.service'; import { IndicadoresTracking } from '../indicadores-tracking'; import { PACIENTES_DETALLE_SINTETICOS } from './paciente-detalle.fixtures'; import { PacienteDetalleState } from './paciente-detalle.state'; +import { PACIENTE_RIESGO_PROVIDER } from './paciente-riesgo.provider'; import { PacienteRegistroDialog } from './paciente-registro-dialog'; import { PacienteRegistrosSesion } from './paciente-registros-sesion'; import { TipoRegistroTemporal } from './paciente-registro.models'; @@ -31,7 +32,8 @@ describe('Altas de demostración desde la ficha de Seguimiento', () => { let dialog: MatDialog; beforeEach(() => { - TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] }); + TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting(), + { provide: PACIENTE_RIESGO_PROVIDER, useValue: { obtener: () => of(null) } }] }); http = TestBed.inject(HttpTestingController); dialog = TestBed.inject(MatDialog); montarTracking(); diff --git a/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.html b/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.html index a5e3c7e..faa62eb 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.html +++ b/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.html @@ -4,7 +4,6 @@ @case ('cargando') {

Cargando riesgo individual…

} @case ('no-disponible') {

Riesgo individual no disponible para esta paciente.

-

La consulta de riesgo individual aún no está habilitada.

} @case ('error') {

No se pudo validar o cargar el riesgo individual.

} @case ('disponible') { diff --git a/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.ts b/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.ts index ed650ff..2b69b01 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-riesgo-tabla.ts @@ -1,4 +1,4 @@ -import { Component, inject } from '@angular/core'; +import { Component, effect, inject, input } from '@angular/core'; import { DecimalPipe } from '@angular/common'; import { PacienteRiesgoState } from './paciente-riesgo.state'; import { tieneIntervalo } from './paciente-riesgo.validation'; @@ -11,6 +11,12 @@ import { tieneIntervalo } from './paciente-riesgo.validation'; styleUrl: './paciente-riesgo-tabla.scss', }) export class PacienteRiesgoTabla { - readonly estado = inject(PacienteRiesgoState).estado; + readonly idSeudonimo = input.required(); + private readonly state = inject(PacienteRiesgoState); + readonly estado = this.state.estado; readonly tieneIntervalo = tieneIntervalo; + + constructor() { + effect(() => this.state.idSeudonimo.set(this.idSeudonimo())); + } } diff --git a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.models.ts b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.models.ts index 832a217..0ff9cec 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.models.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.models.ts @@ -40,9 +40,9 @@ export interface RiesgoPacienteCacu { riesgo: LoteRiesgoCacu | null; } -/** Deliberately no patient argument until secure identity resolution is authorized. */ +/** `null` when the patient has no risk data. */ export interface PacienteRiesgoProvider { - obtener(): Observable; + obtener(idSeudonimo: string): Observable; } export type PacienteRiesgoEstado = diff --git a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.provider.ts b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.provider.ts index c04a30a..70a3092 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.provider.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.provider.ts @@ -1,8 +1,16 @@ -import { InjectionToken } from '@angular/core'; -import { of } from 'rxjs'; +import { HttpClient, HttpErrorResponse } from '@angular/common/http'; +import { inject, InjectionToken } from '@angular/core'; +import { catchError, of, throwError } from 'rxjs'; import { PacienteRiesgoProvider } from './paciente-riesgo.models'; -// No HTTP, fixtures or identity conversion. A real adapter requires separate authorization. +// The follow-up list identifies each patient by the `pac_id` of the risk batches; a 404 is a patient without risk data. export const PACIENTE_RIESGO_PROVIDER = new InjectionToken('PacienteRiesgoProvider', { - providedIn: 'root', factory: () => ({ obtener: () => of(null) }), + providedIn: 'root', factory: () => { + const http = inject(HttpClient); + return { + obtener: (idSeudonimo: string) => http.get(`/cacu/pacientes/${encodeURIComponent(idSeudonimo)}/riesgo`).pipe( + catchError(error => error instanceof HttpErrorResponse && error.status === 404 ? of(null) : throwError(() => error)), + ), + }; + }, }); diff --git a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.spec.ts b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.spec.ts index 1cee25a..20caa5f 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.spec.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.spec.ts @@ -12,6 +12,8 @@ describe('RIESGO-CACU-02: presentación y aislamiento', () => { TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting(), ...(provider ? [{ provide: PACIENTE_RIESGO_PROVIDER, useValue: provider }] : [])] }); const fixture = TestBed.createComponent(PacienteRiesgoTabla); + fixture.componentRef.setInput('idSeudonimo', '42'); + fixture.detectChanges(); fixture.detectChanges(); return fixture; } @@ -20,14 +22,15 @@ describe('RIESGO-CACU-02: presentación y aislamiento', () => { TestBed.inject(HttpTestingController).verify(); }); - it('ejecución normal sin fixtures, HTTP, identidad o controles de preview', () => { + it('pide el riesgo por el pac_id de los lotes a la WebApi y trata un 404 como no disponible', () => { const f = setup(), root: HTMLElement = f.nativeElement; + TestBed.inject(HttpTestingController).expectOne('/cacu/pacientes/42/riesgo') + .flush({ error: 'no existe' }, { status: 404, statusText: 'Not Found' }); + f.detectChanges(); expect(root.textContent).toContain('Riesgo individual no disponible para esta paciente.'); - expect(root.textContent).toContain('aún no está habilitada'); expect(root.textContent).toContain('Recomendaciones no disponibles'); expect(root.textContent).not.toMatch(/simulad|ilustrativ|modelo-cacu|CIN3|2\.4|6\.8/); expect(root.querySelectorAll('table, button, input, select, a')).toHaveLength(0); - TestBed.inject(PACIENTE_RIESGO_PROVIDER).obtener().subscribe(v => expect(v).toBeNull()); }); it('renderiza el ejemplo contractual, estados, metadatos y porcentajes sin alterar los datos', () => { @@ -35,7 +38,7 @@ describe('RIESGO-CACU-02: presentación y aislamiento', () => { data.riesgo!.horizontes[1].estado = 'preliminary'; const baseline = structuredClone(data); const f = setup({ obtener }), root: HTMLElement = f.nativeElement; - expect(obtener).toHaveBeenCalledExactlyOnceWith(); + expect(obtener).toHaveBeenCalledExactlyOnceWith('42'); expect(root.querySelectorAll('thead th')).toHaveLength(4); expect([...root.querySelectorAll('thead th')].every(th => th.getAttribute('scope') === 'col')).toBe(true); expect(root.querySelectorAll('tbody th[scope="row"]')).toHaveLength(3); @@ -134,7 +137,8 @@ describe('RIESGO-CACU-02: presentación y aislamiento', () => { data.riesgo!.horizontes[0].probabilidad = 1; f.detectChanges(); expect(f.nativeElement.querySelector('.percentage')!.textContent).toBe('8 %'); f.destroy(); - const otra = TestBed.createComponent(PacienteRiesgoTabla); otra.detectChanges(); + const otra = TestBed.createComponent(PacienteRiesgoTabla); + otra.componentRef.setInput('idSeudonimo', 'P-OTRA'); otra.detectChanges(); expect(otra.nativeElement.textContent).toContain('Cargando riesgo individual'); expect(otra.nativeElement.querySelector('table')).toBeNull(); expect(riesgoCacuEjemplo().riesgo!.horizontes[0].probabilidad).toBe(0.08); diff --git a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.state.ts b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.state.ts index 5a109fa..9a9e868 100644 --- a/src/app/features/indicadores/paciente-detalle/paciente-riesgo.state.ts +++ b/src/app/features/indicadores/paciente-detalle/paciente-riesgo.state.ts @@ -1,16 +1,22 @@ -import { inject, Injectable } from '@angular/core'; -import { toSignal } from '@angular/core/rxjs-interop'; -import { catchError, defer, map, of } from 'rxjs'; +import { Injectable, inject, signal } from '@angular/core'; +import { toObservable, toSignal } from '@angular/core/rxjs-interop'; +import { catchError, defer, map, of, startWith, switchMap } from 'rxjs'; import { PacienteRiesgoEstado } from './paciente-riesgo.models'; import { PACIENTE_RIESGO_PROVIDER } from './paciente-riesgo.provider'; import { riesgoPacienteResponse } from './paciente-riesgo.validation'; +const CARGANDO: PacienteRiesgoEstado = { tipo: 'cargando' }; + @Injectable() export class PacienteRiesgoState { private readonly provider = inject(PACIENTE_RIESGO_PROVIDER); - readonly estado = toSignal(defer(() => this.provider.obtener()).pipe( - map((datos): PacienteRiesgoEstado => datos === null ? { tipo: 'no-disponible' } - : { tipo: 'disponible', datos: riesgoPacienteResponse(datos) }), - catchError(() => of({ tipo: 'error' })), - ), { initialValue: { tipo: 'cargando' } as PacienteRiesgoEstado }); + readonly idSeudonimo = signal(null); + readonly estado = toSignal(toObservable(this.idSeudonimo).pipe( + switchMap(id => id === null ? of(CARGANDO) : defer(() => this.provider.obtener(id)).pipe( + map((datos): PacienteRiesgoEstado => datos === null ? { tipo: 'no-disponible' } + : { tipo: 'disponible', datos: riesgoPacienteResponse(datos) }), + catchError(() => of({ tipo: 'error' })), + startWith(CARGANDO), + )), + ), { initialValue: CARGANDO }); }