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

112 lines
4.0 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
const {DOMParser} = require('xmldom');
const fs = require('fs');
const ejs = require('ejs');
2024-02-07 15:52:43 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2024-02-16 13:15:13 +00:00
Self.remoteMethod('createShipment', {
2024-01-18 06:36:21 +00:00
description: 'Create an expedition and return a base64Binary label',
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`,
verb: 'POST'
}
});
2024-02-16 13:15:13 +00:00
Self.createShipment = async(expeditionFk, options) => {
2024-02-16 07:28:40 +00:00
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2024-02-16 13:15:13 +00:00
if (!myOptions.transaction)
myOptions.transaction = await Self.beginTransaction({});
const models = Self.app.models;
2024-02-16 07:28:40 +00:00
const mrw = await models.MrwConfig.findOne(null, myOptions);
2024-02-07 15:52:43 +00:00
if (!mrw)
throw new UserError(`Some mrwConfig parameters are not set`);
2024-02-16 13:15:13 +00:00
const query =
`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(t.shipped, '%d/%m/%Y') created,
t.shipped,
e.id expeditionId,
LPAD(IF(mw.params IS NULL, ms.serviceType, mw.serviceType), 4 ,'0') serviceType,
IF(mw.weekdays, 'S', 'N') weekDays
FROM expedition e
JOIN ticket t ON e.ticketFk = t.id
JOIN agencyMode am ON am.id = t.agencyModeFk
JOIN mrwService ms ON ms.agencyModeCodeFk = am.code
LEFT JOIN mrwServiceWeekday mw ON mw.weekdays = DATE_FORMAT(t.shipped, '%a')
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 = ?
LIMIT 1`;
const [expeditionData] = await Self.rawSql(query, [expeditionFk], myOptions);
2024-02-07 15:52:43 +00:00
if (!expeditionData)
throw new UserError(`This expedition is not a MRW shipment`);
2024-02-16 13:15:13 +00:00
const today = Date.vnNew();
today.setHours(0, 0, 0, 0);
if (expeditionData?.shipped.setHours(0, 0, 0, 0) < today)
throw new UserError(`This ticket has a shipped date earlier than today`);
2024-02-16 07:28:40 +00:00
2024-02-16 13:15:13 +00:00
const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData}, 'application/soap+xml');
const shipmentId = getTextByTag(shipmentResponse, 'NumeroEnvio');
2024-02-16 07:28:40 +00:00
if (!shipmentId)
2024-02-16 13:15:13 +00:00
throw new UserError(getTextByTag(shipmentResponse, 'Mensaje'));
2024-02-16 07:28:40 +00:00
2024-02-16 13:15:13 +00:00
const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId}, 'text/xml');
const file = getTextByTag(getLabelResponse, 'EtiquetaFile');
2024-02-16 13:15:13 +00:00
await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, myOptions);
await myOptions.transaction.commit();
2024-02-07 15:52:43 +00:00
2024-02-16 07:28:40 +00:00
return file;
};
function getTextByTag(xmlDoc, tag) {
2024-02-16 13:15:13 +00:00
return xmlDoc?.getElementsByTagName(tag)[0]?.textContent;
2024-02-16 07:28:40 +00:00
}
2024-02-16 13:15:13 +00:00
async function sendXmlDoc(xmlDock, params, contentType) {
const parser = new DOMParser();
2024-02-16 07:28:40 +00:00
const xmlTemplate = fs.readFileSync(__dirname + `/${xmlDock}.ejs`, 'utf-8');
const renderedTemplate = ejs.render(xmlTemplate, params);
2024-02-16 13:15:13 +00:00
const data = await axios.post(params.mrw.url, renderedTemplate, {
headers: {
2024-02-16 13:15:13 +00:00
'Content-Type': `${contentType}; charset=utf-8`
}
});
2024-02-16 13:15:13 +00:00
return parser.parseFromString(data.data, 'text/xml');
2024-02-16 07:28:40 +00:00
}
};