First upload
This commit is contained in:
151
server/services/soap2.service.js
Normal file
151
server/services/soap2.service.js
Normal file
@@ -0,0 +1,151 @@
|
||||
import { XMLParser } from "fast-xml-parser";
|
||||
import { trx } from "./db.service.js";
|
||||
import { actions } from "../actions/soap.actions.js";
|
||||
|
||||
const parser = new XMLParser({
|
||||
removeNSPrefix: true,
|
||||
ignoreAttributes: false,
|
||||
parseTagValue: true,
|
||||
trimValues: true,
|
||||
});
|
||||
|
||||
const ALIAS_MAP = {
|
||||
Polizas: { service: "DescargaPolizas", method: "Descargar" },
|
||||
Recibos: { service: "DescargaCompletaRecibos", method: "Descargar" },
|
||||
Siniestros: { service: "DescargaSiniestros", method: "Descargar" },
|
||||
Cartera: { service: "DescargaCartera", method: "DescargarCartera" },
|
||||
TramiteSiniestro: { service: "ConsultaAgendaTramitacion", method: "ConsultaTramitacion" },
|
||||
Poliza: { service: "ConsultaPolizas", method: "ConsultarPoliza" },
|
||||
Recibo: { service: "ConsultaRecibos", method: "ConsultarRecibo" },
|
||||
Siniestro: { service: "ConsultaSiniestros", method: "ConsultarSiniestro" },
|
||||
};
|
||||
|
||||
const cleanNil = (obj) => {
|
||||
if (obj !== null && typeof obj === "object") {
|
||||
if (obj["@_nil"] === "true" || obj["@_nil"] === true) return null;
|
||||
if (Array.isArray(obj)) return obj.map(cleanNil);
|
||||
const newObj = {};
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
newObj[key] = cleanNil(value);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
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 getXmlBody = (service, method, q) => {
|
||||
const templates = {
|
||||
DescargaPolizas: `<des:DescargarPolizasRequest><des:Clave>${q.Clave}</des:Clave><des:CodigoRamo>${q.CodigoRamo}</des:CodigoRamo><des:Empresa>${q.Empresa}</des:Empresa><des:FechaFinal>${q.FechaFinal}</des:FechaFinal><des:FechaInicial>${q.FechaInicial}</des:FechaInicial><des:Identificador>${q.Identificador}</des:Identificador><des:Plataforma>${q.Plataforma}</des:Plataforma><des:TipoSuplemento1>NP</des:TipoSuplemento1><des:TipoSuplemento2>AN</des:TipoSuplemento2><des:TipoSuplemento3>RE</des:TipoSuplemento3><des:TipoSuplemento4>SP</des:TipoSuplemento4></des:DescargarPolizasRequest>`,
|
||||
DescargaSiniestros: `<des:DescargarSiniestrosRequest><des:AceptarCicos>true</des:AceptarCicos><des:AceptarSdm>true</des:AceptarSdm><des:Clave>${q.Clave}</des:Clave><des:CodigoRamo>${q.CodigoRamo}</des:CodigoRamo><des:Empresa>${q.Empresa}</des:Empresa><des:FechaFinal>${q.FechaFinal}</des:FechaFinal><des:FechaInicial>${q.FechaInicial}</des:FechaInicial><des:Identificador>${q.Identificador}</des:Identificador><des:IncluirAperturas>true</des:IncluirAperturas><des:Plataforma>${q.Plataforma}</des:Plataforma></des:DescargarSiniestrosRequest>`,
|
||||
DescargaCompletaRecibos: `<des:DescargaCompletaRecibosRequest><des:Clave>${q.Clave}</des:Clave><des:Empresa>${q.Empresa}</des:Empresa><des:FechaFinal>${q.FechaFinal}</des:FechaFinal><des:FechaInicial>${q.FechaInicial}</des:FechaInicial><des:Identificador>${q.Identificador}</des:Identificador><des:IncluirAnulados>true</des:IncluirAnulados><des:IncluirCobrados>true</des:IncluirCobrados><des:IncluirDevueltos>true</des:IncluirDevueltos><des:IncluirNuevos>true</des:IncluirNuevos></des:DescargaCompletaRecibosRequest>`,
|
||||
DescargaCartera: `<des:DescargarCarteraRequest><des:CodigoMediador>${q.CodigoMediador}</des:CodigoMediador><des:Empresa>${q.Empresa}</des:Empresa><des:FechaRenovacion>${q.FechaRenovacion}</des:FechaRenovacion><des:Identificador>${q.Identificador}</des:Identificador><des:Plataforma>${q.Plataforma}</des:Plataforma></des:DescargarCarteraRequest>`,
|
||||
ConsultaAgendaTramitacion: `<int:ConsultaTramitacionRequest xmlns:int="http://reale.net/wcf/internalServices"><int:Datos><int:CodigoExpediente>${q.CodigoExpediente}</int:CodigoExpediente><int:CodigoPoliza>${q.CodigoPoliza}</int:CodigoPoliza><int:CodigoSiniestro>${q.CodigoSiniestro}</int:CodigoSiniestro><int:Empresa>${q.Empresa}</int:Empresa></int:Datos></int:ConsultaTramitacionRequest>`,
|
||||
ConsultaPolizas: `<des:ConsultarPolizaRequest><des:CodigoPoliza>${q.CodigoPoliza}</des:CodigoPoliza><des:CodigoRamo>${q.CodigoRamo}</des:CodigoRamo><des:CodigoSuplemento>${q.CodigoSuplemento || 0}</des:CodigoSuplemento><des:Empresa>${q.Empresa}</des:Empresa><des:Identificador>${q.Identificador}</des:Identificador><des:Plataforma>${q.Plataforma}</des:Plataforma></des:ConsultarPolizaRequest>`,
|
||||
ConsultaRecibos: `<des:ConsultarReciboRequest><des:CodigoPoliza>${q.CodigoPoliza}</des:CodigoPoliza><des:CodigoRamo>${q.CodigoRamo}</des:CodigoRamo><des:CodigoRecibo>${q.CodigoRecibo || 0}</des:CodigoRecibo><des:CodigoSuplemento>${q.CodigoSuplemento || 0}</des:CodigoSuplemento><des:Empresa>${q.Empresa}</des:Empresa><des:Identificador>${q.Identificador}</des:Identificador><des:Plataforma>${q.Plataforma}</des:Plataforma></des:ConsultarReciboRequest>`,
|
||||
ConsultaSiniestros: `<des:ConsultarSiniestroRequest><des:CodigoPoliza>${q.CodigoPoliza}</des:CodigoPoliza><des:CodigoRamo>${q.CodigoRamo}</des:CodigoRamo><des:CodigoSiniestro>${q.CodigoSiniestro}</des:CodigoSiniestro><des:CodigoSuplemento>${q.CodigoSuplemento || 0}</des:CodigoSuplemento><des:Empresa>${q.Empresa}</des:Empresa><des:Identificador>${q.Identificador}</des:Identificador><des:Plataforma>${q.Plataforma}</des:Plataforma></des:ConsultarSiniestroRequest>`,
|
||||
};
|
||||
return templates[service] || `<des:${method}Request><des:Identificador>${q.Identificador}</des:Identificador></des:${method}Request>`;
|
||||
};
|
||||
|
||||
export const soapCall = async (params, query, save = true) => {
|
||||
const { CodigoMediador, alias } = params;
|
||||
const target = ALIAS_MAP[alias] || { service: params.service, method: params.method };
|
||||
const { service, method } = target;
|
||||
|
||||
if (!service) throw new Error(`Servicio o Alias '${alias || params.service}' no definido`);
|
||||
|
||||
const url = `https://lba.realeonline.net/Reale.B2b.Services.Multitarificadores.IisHost/${service}.svc`;
|
||||
const Identificador = query.Identificador || process.env[`SOAP_${CodigoMediador}`];
|
||||
if (!Identificador) throw new Error(`Identificador no configurado para ${CodigoMediador}`);
|
||||
|
||||
const q = {
|
||||
...query,
|
||||
Identificador,
|
||||
CodigoMediador,
|
||||
Clave: query.Clave || "10000",
|
||||
Empresa: query.Empresa || "4",
|
||||
Plataforma: query.Plataforma || "10000",
|
||||
CodigoRamo: query.CodigoRamo || "0",
|
||||
CodigoSuplemento: query.CodigoSuplemento || "0",
|
||||
CodigoPoliza: query.CodigoPoliza || "0",
|
||||
CodigoSiniestro: query.CodigoSiniestro || "0",
|
||||
CodigoExpediente: query.CodigoExpediente || "0",
|
||||
FechaFinal: query.FechaFinal || getDate(),
|
||||
FechaInicial: query.FechaInicial || (alias === "Cartera" ? getFirstMonth(0) : getFirstMonth(-1)),
|
||||
FechaRenovacion: query.FechaRenovacion || getFirstMonth(1),
|
||||
};
|
||||
|
||||
let nsBase = "http://reale.net/B2b/Multitarificadores";
|
||||
let nsName = service;
|
||||
let contract = `I${service}`;
|
||||
let actMethod = method;
|
||||
let soapHeader = "";
|
||||
|
||||
if (service === "DescargaCartera") {
|
||||
nsName = "DescargaFicheros";
|
||||
contract = "IDescargaCartera";
|
||||
}
|
||||
else if (service === "ConsultaPolizas") {
|
||||
nsName = "ConsultaPolizas";
|
||||
contract = "IConsultaPolizas";
|
||||
}
|
||||
else if (service === "DescargaCompletaRecibos" && method === "Descargar") {
|
||||
actMethod = "DescargarNew";
|
||||
}
|
||||
else if (service === "ConsultaAgendaTramitacion") {
|
||||
nsBase = "http://reale.net/wcf/internalServices";
|
||||
nsName = "";
|
||||
contract = "IAgendaTramitacion";
|
||||
actMethod = "ConsultaTramitacion";
|
||||
soapHeader = `<int:Plataforma xmlns:int="http://reale.net/wcf/internalServices">${q.Plataforma}</int:Plataforma><int:Identificador xmlns:int="http://reale.net/wcf/internalServices">${q.Identificador}</int:Identificador>`;
|
||||
}
|
||||
|
||||
const fullNs = nsName ? `${nsBase}/${nsName}` : nsBase;
|
||||
const soapAction = `"${fullNs}/${contract}/${actMethod}"`.replace(/([^:])\/\//g, "$1/");
|
||||
|
||||
const soapEnvelope = `
|
||||
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
|
||||
xmlns:des="http://reale.net/B2b/Multitarificadores/${nsName || "DescargaFicheros"}"
|
||||
xmlns:int="http://reale.net/wcf/internalServices">
|
||||
<soapenv:Header>${soapHeader}</soapenv:Header>
|
||||
<soapenv:Body>${getXmlBody(service, method, q)}</soapenv:Body>
|
||||
</soapenv:Envelope>`.trim();
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "text/xml;charset=UTF-8", "SOAPAction": soapAction },
|
||||
body: soapEnvelope,
|
||||
});
|
||||
|
||||
const xmlData = await response.text();
|
||||
const rawJson = parser.parse(xmlData);
|
||||
|
||||
if (!response.ok) {
|
||||
const fault = rawJson.Envelope?.Body?.Fault?.faultstring;
|
||||
throw new Error(typeof fault === "object" ? JSON.stringify(fault) : fault || `Error ${response.status}`);
|
||||
}
|
||||
|
||||
const resultBody = rawJson.Envelope?.Body;
|
||||
const responseName = Object.keys(resultBody || {})[0];
|
||||
const result = cleanNil(resultBody?.[responseName] || {});
|
||||
|
||||
// CUIDADO DA ERROR CUANDO USAS /Poliza /Siniestro /Recibo porque no hay un action para ellos
|
||||
if (save) await trx({ action: service, data: result }, actions);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error(`[SOAP ERROR] ${service}/${method}:`, error.message);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user