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

47 lines
1.4 KiB
JavaScript

const axios = require('axios');
const {DOMParser} = require('xmldom');
module.exports = Self => {
Self.remoteMethod('shipmentCreation', {
description: 'Create an expedition and return a label',
accessType: 'WRITE',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/shipmentCreation`,
verb: 'POST'
}
});
Self.shipmentCreation = async expeditionFk => {
const models = Self.app.models;
const MrwConfig = await models.MrwConfig.findOne({
fields: ['url']
});
const renderedXml = await models.MrwConfig.renderer(expeditionFk);
const response = await axios.post(MrwConfig.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 transmEnvioResponse = xmlDoc.getElementsByTagName('TransmEnvioResponse')[0];
console.log(xmlDoc.getElementsByTagName('TransmEnvioResponse'));
console.log('transmEnvioResponse: ', transmEnvioResponse);
return transmEnvioResponse.textContent;
};
};