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: {
|
2024-07-17 06:16:01 +00:00
|
|
|
type: 'boolean',
|
2024-01-16 14:04:06 +00:00
|
|
|
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);
|
2024-07-10 08:41:52 +00:00
|
|
|
const clientType = await models.MrwConfig.getClientType(expeditionFk);
|
2024-01-16 14:04:06 +00:00
|
|
|
const template = fs.readFileSync(__dirname + '/cancelShipment.ejs', 'utf-8');
|
2024-07-10 08:41:52 +00:00
|
|
|
const renderedXml = ejs.render(template, {mrw, externalId, clientType});
|
2024-01-16 14:04:06 +00:00
|
|
|
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-11-13 10:03:57 +00:00
|
|
|
|
|
|
|
await Self.rawSql('CALL util.debugAdd(?,?);', ['cancelShipment', xmlDoc]);
|
|
|
|
|
2024-07-17 09:18:12 +00:00
|
|
|
const result = xmlDoc.getElementsByTagName('Mensaje')[0].textContent;
|
|
|
|
return result.toLowerCase().includes('se ha cancelado correctamente');
|
2024-01-16 14:04:06 +00:00
|
|
|
};
|
|
|
|
};
|