112 lines
6.2 KiB
JavaScript
112 lines
6.2 KiB
JavaScript
import * as soap from 'soap';
|
|
import { HttpClient } from 'soap';
|
|
import { trx } from './db.service.js';
|
|
import { actions } from '../actions/soap.actions.js';
|
|
|
|
// --- Helpers de Fecha ---
|
|
const fmt = (d) => (!isNaN(d.getTime()) ? d.toLocaleDateString('sv-SE') : null);
|
|
|
|
const getFirstMonth = (m = 0) => {
|
|
const now = new Date();
|
|
return fmt(new Date(now.getFullYear(), now.getMonth() + m, 1));
|
|
};
|
|
|
|
const getDate = (d = 0) => {
|
|
const t = new Date();
|
|
t.setDate(t.getDate() + d);
|
|
return fmt(t);
|
|
};
|
|
|
|
const SOAP_SERVICES = {
|
|
DescargaPolizas: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/DescargaPolizas.svc?wsdl",
|
|
LiquidacionMediador: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/LiquidacionMediador.svc?wsdl",
|
|
DescargaSiniestros: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/DescargaSiniestros.svc?wsdl",
|
|
DescargaCompletaRecibos: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/DescargaCompletaRecibos.svc?wsdl",
|
|
DescargaCartera: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/DescargaCartera.svc?wsdl",
|
|
ConsultaPolizas: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/ConsultaPolizas.svc?wsdl",
|
|
ConsultaRecibos: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/ConsultaRecibos.svc?wsdl",
|
|
ConsultaAgendaTramitacion: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/ConsultaAgendaTramitacion.svc?wsdl",
|
|
TramitacionSiniestro: "https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/TramitacionSiniestro.svc?wsdl",
|
|
};
|
|
|
|
export const soapCall = async (params, query, save = true) => {
|
|
const { CodigoMediador, service, method } = params;
|
|
const url = SOAP_SERVICES[service];
|
|
|
|
if (!url) throw new Error(`Servicio SOAP '${service}' no encontrado`);
|
|
|
|
// --- Identificador desde ENV o Query ---
|
|
const Identificador = query.Identificador || process.env[`SOAP_${CodigoMediador}`];
|
|
if (!Identificador) throw new Error(`Identificador no configurado para mediador ${CodigoMediador}`);
|
|
|
|
// --- Normalización de Argumentos ---
|
|
const Mediador = query.Mediador || CodigoMediador;
|
|
const CodigoPoliza = query.CodigoPoliza || "0";
|
|
const CodigoSiniestro = query.CodigoSiniestro || "0";
|
|
const CodigoExpediente = query.CodigoExpediente || "0";
|
|
const Observaciones = query.Observaciones || "";
|
|
const CodigoRecibo = query.CodigoRecibo || "0";
|
|
const CodigoSuplemento = query.CodigoSuplemento || "1000";
|
|
const Clave = query.Clave || "10000";
|
|
const Empresa = query.Empresa || "4";
|
|
const CodigoEmpresa = query.CodigoEmpresa || Empresa;
|
|
const Plataforma = query.Plataforma || "10000";
|
|
const CodigoRamo = query.CodigoRamo || "0";
|
|
const FechaFinal = query.FechaFinal || getDate();
|
|
const FechaRenovacion = query.FechaRenovacion || getFirstMonth(-1);
|
|
const FechaInicial = query.FechaInicial || getFirstMonth(-1);
|
|
const FechaLiquidacion = query.FechaLiquidacion || getDate();
|
|
|
|
const HEADER = `<int:Plataforma xmlns:int="http://reale.net/wcf/internalServices">${Plataforma}</int:Plataforma><int:Identificador xmlns:int="http://reale.net/wcf/internalServices">${Identificador}</int:Identificador>`;
|
|
|
|
// --- Diccionario de Configuración ---
|
|
const SOAP_CONFIG = {
|
|
DescargaPolizas: { Descargar: { args: { Clave, CodigoRamo, Empresa, FechaFinal, FechaInicial, Identificador, Plataforma, TipoSuplemento1: "NP", TipoSuplemento2: "AN", TipoSuplemento3: "RE", TipoSuplemento4: "SP" }, header: null } },
|
|
LiquidacionMediador: {
|
|
Consulta: { args: { CodigoMediador, Empresa, FechaLiquidacion }, header: HEADER },
|
|
ObtenerFechasLiquidacion: { args: { CodigoMediador, Empresa }, header: HEADER }
|
|
},
|
|
DescargaSiniestros: { Descargar: { args: { AceptarCicos: true, AceptarSdm: true, Clave, CodigoRamo, Empresa, FechaFinal, FechaInicial, Identificador, IncluirAperturas: true, IncluirAperturasCicos: true, IncluirAperturasSdm: true, IncluirPagosImdemnizacion: true, IncluirTerminados: true, Plataforma }, header: HEADER } },
|
|
DescargaCompletaRecibos: { DescargarNew: { args: { Clave, CodigoRamo, Empresa, FechaFinal, FechaInicial, Identificador, Plataforma, IncluirAnulados: true, IncluirCobrados: true, IncluirDevueltos: true, IncluirNuevos: true }, header: null } },
|
|
DescargaCartera: {
|
|
DescargarCartera: { args: { CodigoMediador, Empresa, FechaRenovacion, Identificador, Plataforma }, header: null },
|
|
ObtenerListaRamos: { args: { Empresa }, header: null },
|
|
ObtenerListaRenovaciones: { args: { Empresa, Mediador }, header: null }
|
|
},
|
|
ConsultaPolizas: {
|
|
ConsultarPoliza: { args: { CodigoPoliza, CodigoRamo, CodigoSuplemento, Empresa, Identificador, Plataforma }, header: null },
|
|
ObtenerListaPolizasMediador: { args: { CodigoMediador, Empresa, Identificador }, header: null },
|
|
ObtenerListaSuplementosPoliza: { args: { CodigoPoliza, CodigoRamo, Empresa, Identificador }, header: null }
|
|
},
|
|
ConsultaRecibos: {
|
|
ConsultarRecibo: { args: { CodigoPoliza, CodigoRamo, CodigoRecibo, CodigoSuplemento, Empresa, Identificador, Plataforma }, header: null },
|
|
ObtenerListaRecibosPoliza: { args: { CodigoPoliza, CodigoRamo, Empresa, Identificador }, header: null }
|
|
},
|
|
ConsultaAgendaTramitacion: { ConsultaTramitacion: { args: { Datos: { CodigoExpediente, CodigoPoliza, CodigoSiniestro, Empresa } }, header: HEADER } },
|
|
TramitacionSiniestro: { InsertarTramite: { args: { CodigoEmpresa, CodigoExpediente, CodigoSiniestro, Identificador, Observaciones, Plataforma }, header: null } }
|
|
};
|
|
|
|
const config = SOAP_CONFIG[service]?.[method];
|
|
if (!config) throw new Error(`Método '${method}' no configurado para el servicio '${service}'`);
|
|
|
|
try {
|
|
const client = await soap.createClientAsync(url, {
|
|
httpClient: new HttpClient(),
|
|
request: { timeout: 120000 }
|
|
});
|
|
|
|
if (config.header) client.addSoapHeader(config.header);
|
|
|
|
const methodName = `${method}Async`;
|
|
const [result] = await client[methodName](config.args);
|
|
|
|
if (save) {
|
|
await trx({ action: service, data: result }, actions);
|
|
}
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error(`[SOAP ERROR] ${service}/${method}:`, error.message);
|
|
throw error;
|
|
}
|
|
}; |