47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
const axios = require('axios');
|
|
const fs = require('fs');
|
|
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: ['object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/cancelShipment`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.cancelShipment = async expeditionFk => {
|
|
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');
|
|
const [resultElement] = xmlDoc.getElementsByTagName('Mensaje');
|
|
|
|
return resultElement.textContent;
|
|
};
|
|
};
|