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

46 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

const axios = require('axios');
const fs = require('fs');
2024-01-18 06:36:21 +00:00
const ejs = require('ejs');
const {DOMParser} = require('xmldom');
module.exports = Self => {
Self.remoteMethod('cancelShipment', {
description: 'Cancel a shipment by providing the expedition ID, interacting with MRW WebService',
accessType: 'WRITE',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/cancelShipment`,
verb: 'POST'
}
});
2024-01-18 06:36:21 +00:00
Self.cancelShipment = async expeditionFk => {
const models = Self.app.models;
const mrw = await models.MrwConfig.findOne();
const {externalId} = await models.Expedition.findById(expeditionFk);
const clientType = await models.MrwConfig.getClientType(expeditionFk);
const template = fs.readFileSync(__dirname + '/cancelShipment.ejs', 'utf-8');
const renderedXml = ejs.render(template, {mrw, externalId, clientType});
const response = await axios.post(mrw.url, renderedXml, {
headers: {
'Content-Type': 'application/soap+xml; charset=utf-8'
}
});
const xmlString = response.data;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
2024-07-17 09:18:12 +00:00
const result = xmlDoc.getElementsByTagName('Mensaje')[0].textContent;
return result.toLowerCase().includes('se ha cancelado correctamente');
};
};