debug(alertas): logging en generador y endpoint /automaticas; wrap cada alerta en try/catch
This commit is contained in:
@@ -125,7 +125,9 @@ export async function resolverAlertaManual(req: Request, res: Response, next: Ne
|
|||||||
export async function getAlertasAutomaticas(req: Request, res: Response, next: NextFunction) {
|
export async function getAlertasAutomaticas(req: Request, res: Response, next: NextFunction) {
|
||||||
try {
|
try {
|
||||||
const contribuyenteId = req.query.contribuyenteId as string | undefined;
|
const contribuyenteId = req.query.contribuyenteId as string | undefined;
|
||||||
|
console.log(`[AlertasCtrl] GET /automaticas tenant=${req.user!.tenantId} contribuyente=${contribuyenteId || 'null'} user=${req.user!.userId} role=${req.user!.role}`);
|
||||||
const alertas = await generarAlertasAutomaticas(req.tenantPool!, req.user!.tenantId, contribuyenteId || null);
|
const alertas = await generarAlertasAutomaticas(req.tenantPool!, req.user!.tenantId, contribuyenteId || null);
|
||||||
|
console.log(`[AlertasCtrl] GET /automaticas devuelve ${alertas.length} alertas: ${alertas.map(a => a.id).join(', ') || 'ninguna'}`);
|
||||||
res.json(alertas);
|
res.json(alertas);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
|
|||||||
@@ -609,30 +609,46 @@ async function alertaOpinionCumplimiento(pool: Pool, contribuyenteId?: string |
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Genera todas las alertas automáticas para un tenant.
|
* Genera todas las alertas automáticas para un tenant.
|
||||||
|
* Cada alerta se envuelve en try/catch para que un fallo en una no
|
||||||
|
* bloquee el resto (robustez ante timeouts o errores transitorios).
|
||||||
*/
|
*/
|
||||||
export async function generarAlertasAutomaticas(
|
export async function generarAlertasAutomaticas(
|
||||||
pool: Pool,
|
pool: Pool,
|
||||||
tenantId: string,
|
tenantId: string,
|
||||||
contribuyenteId?: string | null,
|
contribuyenteId?: string | null,
|
||||||
): Promise<AlertaAuto[]> {
|
): Promise<AlertaAuto[]> {
|
||||||
const alertas = await Promise.all([
|
const generadores: { name: string; fn: () => Promise<AlertaAuto | null> }[] = [
|
||||||
alertaListaNegraPropia(pool, tenantId, contribuyenteId),
|
{ name: 'lista-negra-propia', fn: () => alertaListaNegraPropia(pool, tenantId, contribuyenteId) },
|
||||||
alertaClienteListaNegra(pool, contribuyenteId),
|
{ name: 'lista-negra-clientes', fn: () => alertaClienteListaNegra(pool, contribuyenteId) },
|
||||||
alertaProveedorListaNegra(pool, contribuyenteId),
|
{ name: 'lista-negra-proveedores', fn: () => alertaProveedorListaNegra(pool, contribuyenteId) },
|
||||||
alertaDiscrepanciaRegimen(pool, tenantId, contribuyenteId),
|
{ name: 'discrepancia-regimen', fn: () => alertaDiscrepanciaRegimen(pool, tenantId, contribuyenteId) },
|
||||||
alertaConcentracionClientes(pool, contribuyenteId),
|
{ name: 'concentracion-clientes', fn: () => alertaConcentracionClientes(pool, contribuyenteId) },
|
||||||
alertaConcentracionProveedores(pool, contribuyenteId),
|
{ name: 'concentracion-proveedores', fn: () => alertaConcentracionProveedores(pool, contribuyenteId) },
|
||||||
alertaRiesgoCambiario(pool, contribuyenteId),
|
{ name: 'riesgo-cambiario', fn: () => alertaRiesgoCambiario(pool, contribuyenteId) },
|
||||||
alertaRiesgoCancelaciones(pool, contribuyenteId),
|
{ name: 'riesgo-cancelaciones', fn: () => alertaRiesgoCancelaciones(pool, contribuyenteId) },
|
||||||
alertaRiesgoTransaccional(pool, contribuyenteId),
|
{ name: 'riesgo-transaccional', fn: () => alertaRiesgoTransaccional(pool, contribuyenteId) },
|
||||||
alertaCancelacionPeriodoAnterior(pool, contribuyenteId),
|
{ name: 'cancelacion-periodo-anterior', fn: () => alertaCancelacionPeriodoAnterior(pool, contribuyenteId) },
|
||||||
alertaOpinionCumplimiento(pool, contribuyenteId),
|
{ name: 'opinion-cumplimiento', fn: () => alertaOpinionCumplimiento(pool, contribuyenteId) },
|
||||||
alertaTipoRelacionSospechosa(pool, contribuyenteId),
|
{ name: 'tipo-relacion-sospechosa', fn: () => alertaTipoRelacionSospechosa(pool, contribuyenteId) },
|
||||||
alertaTareasProximasVencer(pool, contribuyenteId),
|
{ name: 'tareas-proximas-vencer', fn: () => alertaTareasProximasVencer(pool, contribuyenteId) },
|
||||||
alertaResicoPfLimiteIngresos(pool, contribuyenteId),
|
{ name: 'resico-pf-limite-ingresos', fn: () => alertaResicoPfLimiteIngresos(pool, contribuyenteId) },
|
||||||
]);
|
];
|
||||||
|
|
||||||
return alertas.filter((a): a is AlertaAuto => a !== null);
|
const alertas: AlertaAuto[] = [];
|
||||||
|
for (const g of generadores) {
|
||||||
|
try {
|
||||||
|
const a = await g.fn();
|
||||||
|
if (a) alertas.push(a);
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error(`[AlertasAuto] Fallo ${g.name} (tenant=${tenantId}, contribuyente=${contribuyenteId}):`, err.message || err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alertas.length > 0) {
|
||||||
|
console.log(`[AlertasAuto] tenant=${tenantId} contribuyente=${contribuyenteId || 'null'} generadas=${alertas.map(a => a.id).join(', ')}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return alertas;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user