integrating latest dashboard changes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { map } from 'rxjs';
|
||||
import { Dominio, IndicadorDatos, FigureSelection, Indicador, parsePeriodos, PlotlyFigure, requireDominio } from './indicadores.models';
|
||||
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);
|
||||
@@ -15,6 +15,7 @@ export class IndicadoresService {
|
||||
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
|
||||
@@ -26,21 +27,64 @@ export class IndicadoresService {
|
||||
return value as Indicador[];
|
||||
}));
|
||||
}
|
||||
datos(dominio: Dominio, indicador: string, tipo: 'semestre' | 'mes', periodos?: string[]) {
|
||||
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'].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'
|
||||
|| !['mes', 'semestre'].includes(value['tipo_periodo'] as 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;
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user