552e2c194f
The card for cama_histopatologia_birads now shows rates with their Wilson interval per BI-RADS category and a two-ring donut of the biopsy result, with group and category selection, period navigation and the published values table. A pure adapter turns the stratified C2 response into rows and figures; the view only asks for the data and draws what the adapter returns. BI-RADS 4 is a row of its own. The denominator is benign, B3, in situ and invasive; no biopsy and no data are reported apart. A published zero is a zero, a suppressed cell is suppressed, and a cell or stratum the response does not carry is unavailable, never zero: no rate or donut total is drawn over an incomplete matrix. The card keeps its place first in the summary and stays pending while the catalog does not publish the indicator. The `birads` stratum is added to the accepted dimensions of the data request. The CaCu cytology confirmation view is not touched.
111 lines
6.9 KiB
TypeScript
111 lines
6.9 KiB
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
|
import { map } from 'rxjs';
|
|
import { DatosScope, Dominio, IndicadorDatos, IndicadorMetadatos, FigureSelection, Indicador, parsePeriodos, PlotlyFigure, requireDominio } from './indicadores.models';
|
|
|
|
function record(value: unknown): value is Record<string, unknown> {
|
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
}
|
|
@Injectable({ providedIn: 'root' })
|
|
export class IndicadoresService {
|
|
private readonly http = inject(HttpClient);
|
|
catalogo(dominio: Dominio = 'tea') {
|
|
const domain = requireDominio(dominio);
|
|
return this.http.get<unknown>(`/${domain}/indicadores`).pipe(map(value => {
|
|
if (!Array.isArray(value) || !value.every(item => record(item)
|
|
&& typeof item['id'] === 'string' && item['id'].trim().length > 0 && typeof item['titulo'] === 'string'
|
|
&& typeof item['contrato'] === 'string' && Array.isArray(item['figuras'])
|
|
&& (item['version'] === undefined || typeof item['version'] === 'string')
|
|
&& item['figuras'].every((figure: unknown) => typeof figure === 'string' && figure.trim().length > 0)
|
|
&& new Set(item['figuras']).size === item['figuras'].length
|
|
&& Array.isArray(item['tipos_periodo']) && item['tipos_periodo'].length > 0
|
|
&& item['tipos_periodo'].every((tipo: unknown) => tipo === 'semestre' || tipo === 'mes')
|
|
&& new Set(item['tipos_periodo']).size === item['tipos_periodo'].length)) {
|
|
throw new Error('Respuesta de catálogo inválida');
|
|
}
|
|
if (new Set(value.map(item => item['id'])).size !== value.length) throw new Error('IDs de catálogo duplicados');
|
|
return value as Indicador[];
|
|
}));
|
|
}
|
|
metadatos(dominio: Dominio, indicador: string) {
|
|
return this.http.get<unknown>(`/${requireDominio(dominio)}/indicadores/${encodeURIComponent(indicador)}/metadatos`).pipe(map(value => {
|
|
if (!record(value)) throw new Error('Metadatos inválidos');
|
|
const description = value['descripcion'];
|
|
if (description !== undefined && description !== null && typeof description !== 'string') throw new Error('Metadatos inválidos');
|
|
const ficha = value['ficha'];
|
|
if (ficha !== undefined && ficha !== null) {
|
|
if (!record(ficha)) throw new Error('Metadatos inválidos');
|
|
for (const field of ['codigo', 'para', 'unidad', 'pregunta', 'definicion', 'nota']) {
|
|
const content = ficha[field];
|
|
if (content !== undefined && content !== null && typeof content !== 'string') throw new Error('Metadatos inválidos');
|
|
}
|
|
}
|
|
for (const field of ['referencias', 'poblaciones']) {
|
|
const list = value[field];
|
|
if (list !== undefined && list !== null && (!Array.isArray(list) || !list.every(v => record(v) && (field === 'referencias'
|
|
? typeof v['tipo'] === 'string' && typeof v['etiqueta'] === 'string' && (v['ref'] === null || typeof v['ref'] === 'string')
|
|
: typeof v['grupo'] === 'string' && typeof v['codigo'] === 'string' && typeof v['descripcion'] === 'string')))) throw new Error('Metadatos inválidos');
|
|
}
|
|
const figureDetails = value['figuras_detalle'];
|
|
if (figureDetails !== undefined && figureDetails !== null && (!Array.isArray(figureDetails) || !figureDetails.every(v => record(v)
|
|
&& typeof v['figura'] === 'string' && v['figura'].trim().length > 0
|
|
&& (v['descripcion'] === null || typeof v['descripcion'] === 'string')
|
|
&& (v['como_leer'] === null || Array.isArray(v['como_leer']) && v['como_leer'].every(line => typeof line === 'string'))))) {
|
|
throw new Error('Metadatos inválidos');
|
|
}
|
|
if (Array.isArray(figureDetails) && new Set(figureDetails.map(item => (item as Record<string, unknown>)['figura'])).size !== figureDetails.length) {
|
|
throw new Error('Metadatos inválidos');
|
|
}
|
|
return value as IndicadorMetadatos;
|
|
}));
|
|
}
|
|
datos(dominio: Dominio, indicador: string, tipo: 'semestre' | 'mes', periodos?: string[], scope: DatosScope = {}) {
|
|
const domain = requireDominio(dominio);
|
|
if (periodos && (!periodos.length || parsePeriodos(periodos.join(','), tipo) === null)) {
|
|
throw new Error('Periodos inválidos');
|
|
}
|
|
let params = new HttpParams();
|
|
if (scope.estrato) {
|
|
if (!['grupo_edad', 'estatus_seguimiento', 'citologia_alto_grado', 'birads'].includes(scope.estrato)) throw new Error('Dimensión no admitida');
|
|
params = params.set('estrato', scope.estrato);
|
|
}
|
|
if (scope.filtros?.length) params = params.set('filtro', scope.filtros.join(','));
|
|
if (periodos) params = params.set('periodos', [...new Set(periodos)].join(','));
|
|
return this.http.get<unknown>(`/${domain}/indicadores/${encodeURIComponent(indicador)}/datos`, { params }).pipe(map(value => {
|
|
if (!record(value) || value['id'] !== indicador || typeof value['contrato'] !== 'string'
|
|
|| value['tipo_periodo'] !== tipo
|
|
|| !Array.isArray(value['periodos']) || !value['periodos'].every(p => typeof p === 'string')
|
|
|| !Array.isArray(value['filas']) || !value['filas'].every(record)
|
|
|| (value['notas'] !== undefined && (!Array.isArray(value['notas']) || !value['notas'].every(n => typeof n === 'string')))) {
|
|
throw new Error('Respuesta de datos inválida');
|
|
}
|
|
const publishedPeriods = value['periodos'] as string[];
|
|
if ((value['estrato'] ?? undefined) !== scope.estrato) throw new Error('Dimensión recibida distinta de la solicitada');
|
|
if ((periodos && publishedPeriods.some(p => !periodos.includes(p)))
|
|
|| value['filas'].some(row => typeof row['periodo'] !== 'string' || !publishedPeriods.includes(row['periodo']))) {
|
|
throw new Error('Los datos recibidos no corresponden a los periodos solicitados');
|
|
}
|
|
return value as unknown as IndicadorDatos;
|
|
}));
|
|
}
|
|
figura(selection: FigureSelection) {
|
|
const dominio = requireDominio(selection.dominio ?? 'tea');
|
|
if (selection.periodos !== undefined) {
|
|
if (!selection.periodos.length) throw new Error('Selección explícita vacía');
|
|
const text = selection.periodos.join(',');
|
|
const tipos = selection.tipoPeriodo ? [selection.tipoPeriodo] : (['semestre', 'mes'] as const);
|
|
if (!tipos.some(tipo => parsePeriodos(text, tipo) !== null)) {
|
|
throw new Error('Periodos inválidos o de tipos mezclados');
|
|
}
|
|
}
|
|
let params = new HttpParams().set('tema', selection.tema);
|
|
if (selection.periodos?.length) params = params.set('periodos', [...new Set(selection.periodos.map(periodo => periodo.trim()))].join(','));
|
|
const url = `/${dominio}/indicadores/${encodeURIComponent(selection.indicador)}/figuras/${encodeURIComponent(selection.figura)}`;
|
|
return this.http.get<unknown>(url, { params }).pipe(map(value => {
|
|
if (!record(value) || !Array.isArray(value['data']) || !value['data'].every(record)
|
|
|| !record(value['layout'])) throw new Error('Respuesta de figura inválida');
|
|
return value as unknown as PlotlyFigure;
|
|
}));
|
|
}
|
|
}
|