2024-01-16 14:04:06 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const fs = require('fs');
|
2024-01-18 06:36:21 +00:00
|
|
|
const ejs = require('ejs');
|
2024-01-16 14:04:06 +00:00
|
|
|
const {DOMParser} = require('xmldom');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('cancelShipment', {
|
2024-02-19 11:54:24 +00:00
|
|
|
description: 'Cancel a shipment by providing the expedition ID, interacting with MRW WebService',
|
2024-01-16 14:04:06 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'expeditionFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/cancelShipment`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-18 06:36:21 +00:00
|
|
|
Self.cancelShipment = async expeditionFk => {
|
2024-01-16 14:04:06 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
|
|
|
|
const mrw = await models.MrwConfig.findOne();
|
|
|
|
const {externalId} = await models.Expedition.findById(expeditionFk);
|
|
|
|
|
|
|
|
const template = fs.readFileSync(__dirname + '/cancelShipment.ejs', 'utf-8');
|
|
|
|
const renderedXml = ejs.render(template, {mrw, externalId});
|
|
|
|
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-05-20 10:17:10 +00:00
|
|
|
return xmlDoc.getElementsByTagName('Mensaje')[0].textContent;
|
2024-01-16 14:04:06 +00:00
|
|
|
};
|
|
|
|
};
|