feat(indicadores): habilitar dashboards Angular TEA CaCu y CaMa
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 { FigureSelection, Indicador, PlotlyFigure } from './indicadores.models';
|
||||
import { Dominio, 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);
|
||||
@@ -9,21 +9,34 @@ function record(value: unknown): value is Record<string, unknown> {
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class IndicadoresService {
|
||||
private readonly http = inject(HttpClient);
|
||||
catalogo() {
|
||||
return this.http.get<unknown>('/tea/indicadores').pipe(map(value => {
|
||||
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' && typeof item['titulo'] === 'string'
|
||||
&& typeof item['id'] === 'string' && item['id'].trim().length > 0 && typeof item['titulo'] === 'string'
|
||||
&& typeof item['contrato'] === 'string' && Array.isArray(item['figuras'])
|
||||
&& item['figuras'].every((figure: unknown) => typeof figure === 'string'))) {
|
||||
&& item['figuras'].every((figure: unknown) => typeof figure === 'string' && figure.trim().length > 0)
|
||||
&& 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[];
|
||||
}));
|
||||
}
|
||||
figura(selection: FigureSelection) {
|
||||
const dominio = requireDominio(selection.dominio ?? 'tea');
|
||||
if (selection.periodos?.length) {
|
||||
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', selection.periodos.join(','));
|
||||
const url = `/tea/indicadores/${encodeURIComponent(selection.indicador)}/figuras/${encodeURIComponent(selection.figura)}`;
|
||||
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');
|
||||
|
||||
Reference in New Issue
Block a user