salix/back/methods/mrw-config/createShipment.js

94 lines
3.2 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
const {DOMParser} = require('xmldom');
const fs = require('fs');
const ejs = require('ejs');
2024-02-07 15:52:43 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2024-02-16 07:28:40 +00:00
Self.remoteMethodCtx('createShipment', {
2024-01-18 06:36:21 +00:00
description: 'Create an expedition and return a base64Binary label',
accessType: 'WRITE',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
2024-01-18 06:36:21 +00:00
path: `/createShipment`,
verb: 'POST'
}
});
2024-02-16 07:28:40 +00:00
Self.createShipment = async(ctx, expeditionFk, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const models = Self.app.models;
2024-02-16 07:28:40 +00:00
const mrw = await models.MrwConfig.findOne(null, myOptions);
console.log('mrw: ', mrw);
2024-02-07 15:52:43 +00:00
if (!mrw)
throw new UserError(`Some mrwConfig parameters are not set`);
const [expeditionData] = await Self.rawSql(
2024-02-07 15:52:43 +00:00
fs.readFileSync(__dirname + '/expeditionData.sql', 'utf-8'),
2024-02-16 07:28:40 +00:00
null,
myOptions
);
2024-02-16 07:28:40 +00:00
console.log('sigue');
2024-02-07 15:52:43 +00:00
if (!expeditionData)
throw new UserError(`This expedition is not a MRW shipment`);
if (expeditionData?.created < Date.vnNew())
2024-02-16 07:28:40 +00:00
throw new UserError(`This ticket has a shipped date earlier than today`); po;
const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData});
2024-02-07 15:52:43 +00:00
const parser = new DOMParser();
2024-02-16 07:28:40 +00:00
const shipmentXmlDoc = parser.parseFromString(shipmentResponse, 'text/xml');
const shipmentId = getTextByTag(shipmentXmlDoc, 'NumeroEnvio');
if (!shipmentId)
throw new UserError(getTextByTag(shipmentXmlDoc, 'Mensaje'));
const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId});
2024-02-16 07:28:40 +00:00
const getLabelXmlDoc = parser.parseFromString(getLabelResponse, 'text/xml');
const file = getTextByTag(getLabelXmlDoc, 'EtiquetaFile');
console.log('file: ', file);
if (!file) {
const message = getTextByTag(getLabelXmlDoc, 'Mensaje') ??
`The MRW web service is not returning the expected response`;
2024-02-07 15:52:43 +00:00
throw new UserError(message);
}
2024-02-16 07:28:40 +00:00
await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, options);
2024-02-07 15:52:43 +00:00
2024-02-16 07:28:40 +00:00
return file;
};
function getTextByTag(xmlDoc, tag) {
const parser = new DOMParser();
const doc = parser.parseFromString(xmlDoc?.data, 'text/xml');
return doc?.getElementsByTagName(tag)[0]?.textContent;
}
async function sendXmlDoc(xmlDock, params) {
const xmlTemplate = fs.readFileSync(__dirname + `/${xmlDock}.ejs`, 'utf-8');
const renderedTemplate = ejs.render(xmlTemplate, params);
const {data} = await axios.post(params.mrw.url, renderedTemplate, {
headers: {
2024-02-16 07:28:40 +00:00
'Content-Type': 'application/soap+xml; charset=utf-8'
}
});
2024-02-16 07:28:40 +00:00
console.log('data: ', data);
return data;
}
};