2024-01-15 14:00:36 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const {DOMParser} = require('xmldom');
|
2024-01-16 14:04:06 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const ejs = require('ejs');
|
2024-01-15 14:00:36 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2024-01-18 06:36:21 +00:00
|
|
|
Self.remoteMethod('createShipment', {
|
|
|
|
description: 'Create an expedition and return a base64Binary label',
|
2024-01-15 14:00:36 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'expeditionFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2024-01-18 06:36:21 +00:00
|
|
|
path: `/createShipment`,
|
2024-01-15 14:00:36 +00:00
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-18 06:36:21 +00:00
|
|
|
Self.createShipment = async expeditionFk => {
|
2024-01-15 14:00:36 +00:00
|
|
|
const models = Self.app.models;
|
2024-01-16 14:04:06 +00:00
|
|
|
const mrw = await models.MrwConfig.findOne();
|
2024-01-15 14:00:36 +00:00
|
|
|
|
2024-01-16 14:04:06 +00:00
|
|
|
const [expeditionData] = await Self.rawSql(
|
|
|
|
`SELECT CASE co.code
|
|
|
|
WHEN 'ES' THEN a.postalCode
|
|
|
|
WHEN 'PT' THEN LEFT(a.postalCode, 4)
|
|
|
|
WHEN 'AD' THEN REPLACE(a.postalCode, 'AD', '00')
|
|
|
|
END postalCode,
|
|
|
|
a.city,
|
|
|
|
a.street,
|
|
|
|
co.code countryCode,
|
|
|
|
c.fi,
|
|
|
|
c.name clientName,
|
|
|
|
c.phone,
|
|
|
|
DATE_FORMAT(e.created, '%d/%m/%Y') created,
|
|
|
|
e.id expeditionId,
|
|
|
|
LPAD(ms.serviceType, 4 ,'0') serviceType,
|
|
|
|
pa.height,
|
|
|
|
pa.depth,
|
|
|
|
pa.width
|
|
|
|
FROM expedition e
|
|
|
|
JOIN packaging pa ON pa.id = e.packagingFk
|
|
|
|
JOIN ticket t ON e.ticketFk = t.id
|
|
|
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
|
|
|
JOIN mrwService ms ON ms.agencyModeCodeFk = am.code
|
|
|
|
JOIN client c ON t.clientFk = c.id
|
|
|
|
JOIN address a ON t.addressFk = a.id
|
|
|
|
JOIN province p ON a.provinceFk = p.id
|
|
|
|
JOIN country co ON co.id = p.countryFk
|
|
|
|
WHERE e.id = ?`, [expeditionFk]
|
|
|
|
);
|
2024-01-15 14:00:36 +00:00
|
|
|
|
2024-01-18 06:36:21 +00:00
|
|
|
const shipmentTemplate = fs.readFileSync(__dirname + '/createShipment.ejs', 'utf-8');
|
2024-01-16 14:04:06 +00:00
|
|
|
const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData});
|
|
|
|
const shipmentResponse = await axios.post(mrw.url, renderedShipment, {
|
2024-01-15 14:00:36 +00:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/soap+xml; charset=utf-8'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const parser = new DOMParser();
|
2024-01-16 14:04:06 +00:00
|
|
|
const shipmentXmlDoc = parser.parseFromString(shipmentResponse.data, 'text/xml');
|
|
|
|
const shipmentId = shipmentXmlDoc.getElementsByTagName('NumeroEnvio')[0].textContent;
|
|
|
|
|
|
|
|
const getLabelTemplate = fs.readFileSync(__dirname + '/getLabel.ejs', 'utf-8');
|
|
|
|
const renderedGetLabel = ejs.render(getLabelTemplate, {mrw, shipmentId});
|
|
|
|
const getLabelResponse = await axios.post(mrw.url, renderedGetLabel, {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/xml; charset=utf-8'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const getLableXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml');
|
2024-01-18 06:36:21 +00:00
|
|
|
const base64Binary = getLableXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent;
|
2024-01-16 14:04:06 +00:00
|
|
|
|
|
|
|
const expedition = await models.Expedition.findById(expeditionFk);
|
|
|
|
await expedition.updateAttribute('externalId', shipmentId);
|
2024-01-15 14:00:36 +00:00
|
|
|
|
2024-01-16 14:04:06 +00:00
|
|
|
return base64Binary;
|
2024-01-15 14:00:36 +00:00
|
|
|
};
|
|
|
|
};
|