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: 'boolean', 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 clientType = await models.MrwConfig.getClientType(expeditionFk); const template = fs.readFileSync(__dirname + '/cancelShipment.ejs', 'utf-8'); const renderedXml = ejs.render(template, {mrw, externalId, clientType}); await Self.rawSql('CALL util.debugAdd(?,?);', ['cancelShipment', renderedXml]); 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 result = xmlDoc.getElementsByTagName('Mensaje')[0].textContent; return result.toLowerCase().includes('se ha cancelado correctamente'); }; };