From 7e3660aece556117b4605f1322ca098283facbf2 Mon Sep 17 00:00:00 2001 From: pablone Date: Tue, 16 Jan 2024 15:04:06 +0100 Subject: [PATCH] feat(cancelShipment): refs #6403 mrwintegration --- .vscode/settings.json | 2 +- back/methods/mrw-config/cancelShipment.ejs | 20 +++++++ back/methods/mrw-config/cancelShipment.js | 46 ++++++++++++++ back/methods/mrw-config/getLabel.ejs | 25 ++++++++ back/methods/mrw-config/renderer.js | 59 ------------------ back/methods/mrw-config/shipmentCreation.ejs | 42 +++++++++++++ back/methods/mrw-config/shipmentCreation.js | 63 ++++++++++++++++---- back/methods/mrw-config/template.ejs | 40 ------------- back/models/mrw-config.js | 2 +- back/models/mrw-config.json | 3 + db/changes/240401/00-mrwWebService.sql | 7 ++- 11 files changed, 195 insertions(+), 114 deletions(-) create mode 100644 back/methods/mrw-config/cancelShipment.ejs create mode 100644 back/methods/mrw-config/cancelShipment.js create mode 100644 back/methods/mrw-config/getLabel.ejs delete mode 100644 back/methods/mrw-config/renderer.js create mode 100644 back/methods/mrw-config/shipmentCreation.ejs delete mode 100644 back/methods/mrw-config/template.ejs diff --git a/.vscode/settings.json b/.vscode/settings.json index 40ec5c0d3..36b7e21d8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,7 @@ // Carácter predeterminado de final de línea. "files.eol": "\n", "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "search.useIgnoreFiles": false, "editor.defaultFormatter": "dbaeumer.vscode-eslint", diff --git a/back/methods/mrw-config/cancelShipment.ejs b/back/methods/mrw-config/cancelShipment.ejs new file mode 100644 index 000000000..9ef401bc8 --- /dev/null +++ b/back/methods/mrw-config/cancelShipment.ejs @@ -0,0 +1,20 @@ + + + + <%= mrw.franchiseCode %> + <%= mrw.subscriberCode %> + + <%= mrw.user %> + <%= mrw.password %> + + + + + + + <%= externalId %> + + + + + \ No newline at end of file diff --git a/back/methods/mrw-config/cancelShipment.js b/back/methods/mrw-config/cancelShipment.js new file mode 100644 index 000000000..fe8af1caa --- /dev/null +++ b/back/methods/mrw-config/cancelShipment.js @@ -0,0 +1,46 @@ +const axios = require('axios'); +const fs = require('fs'); +const {DOMParser} = require('xmldom'); + +module.exports = Self => { + Self.remoteMethod('cancelShipment', { + description: 'Cancel a shipment by providing the expedition ID, interacting with MRW WS', + accessType: 'WRITE', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/cancelShipment`, + verb: 'POST' + } + }); + + Self.deleteExpedition = 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')[0]; + const result = resultElement.textContent; + + return result; + }; +}; diff --git a/back/methods/mrw-config/getLabel.ejs b/back/methods/mrw-config/getLabel.ejs new file mode 100644 index 000000000..09bdb3f6c --- /dev/null +++ b/back/methods/mrw-config/getLabel.ejs @@ -0,0 +1,25 @@ + + + + <%= mrw.franchiseCode %> + <%= mrw.subscriberCode %> + + <%= mrw.user %> + <%= mrw.password %> + + + + + + <%= shipmentId %> + 1 + + + + 0 + 0 + 0 + + + + \ No newline at end of file diff --git a/back/methods/mrw-config/renderer.js b/back/methods/mrw-config/renderer.js deleted file mode 100644 index 33ac5cdd0..000000000 --- a/back/methods/mrw-config/renderer.js +++ /dev/null @@ -1,59 +0,0 @@ -const fs = require('fs'); -const ejs = require('ejs'); - -module.exports = Self => { - Self.remoteMethod('renderer', { - description: 'Renders the data from an XML', - accessType: 'READ', - accepts: [{ - arg: 'expeditionFk', - type: 'number', - required: true - }], - returns: { - type: ['object'], - root: true - }, - http: { - path: `/renderer`, - verb: 'GET' - } - }); - - Self.renderer = async expeditionFk => { - const mrw = await Self.app.models.MrwConfig.findOne(); - - const [expedition] = 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, - e.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] - ); - - const template = fs.readFileSync(__dirname + '/template.ejs', 'utf-8'); - return ejs.render(template, {mrw, expedition}); - }; -}; diff --git a/back/methods/mrw-config/shipmentCreation.ejs b/back/methods/mrw-config/shipmentCreation.ejs new file mode 100644 index 000000000..e84cfce39 --- /dev/null +++ b/back/methods/mrw-config/shipmentCreation.ejs @@ -0,0 +1,42 @@ + + + + + <%= mrw.franchiseCode %> + <%= mrw.subscriberCode %> + + <%= mrw.user %> + <%= mrw.password %> + + + + + + + + + <%= expeditionData.street %> + + + <%= expeditionData.postalCode %> + <%= expeditionData.city %> + + + + <%= expeditionData.fi %> + <%= expeditionData.clientName %> + <%= expeditionData.phone %> + + + <%= expeditionData.created %> + <%= expeditionData.expeditionDataId %> + <%= expeditionData.serviceType %> + 1 + <%= expeditionData.kg %> + + + + + + + \ No newline at end of file diff --git a/back/methods/mrw-config/shipmentCreation.js b/back/methods/mrw-config/shipmentCreation.js index c75fb793d..b1574784f 100644 --- a/back/methods/mrw-config/shipmentCreation.js +++ b/back/methods/mrw-config/shipmentCreation.js @@ -1,5 +1,7 @@ const axios = require('axios'); const {DOMParser} = require('xmldom'); +const fs = require('fs'); +const ejs = require('ejs'); module.exports = Self => { Self.remoteMethod('shipmentCreation', { @@ -22,25 +24,62 @@ module.exports = Self => { Self.shipmentCreation = async expeditionFk => { const models = Self.app.models; + const mrw = await models.MrwConfig.findOne(); - const MrwConfig = await models.MrwConfig.findOne({ - fields: ['url'] - }); + 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] + ); - const renderedXml = await models.MrwConfig.renderer(expeditionFk); - const response = await axios.post(MrwConfig.url, renderedXml, { + const shipmentTemplate = fs.readFileSync(__dirname + '/shipmentCreation.ejs', 'utf-8'); + const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData}); + const shipmentResponse = await axios.post(mrw.url, renderedShipment, { 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); + const shipmentXmlDoc = parser.parseFromString(shipmentResponse.data, 'text/xml'); + const shipmentId = shipmentXmlDoc.getElementsByTagName('NumeroEnvio')[0].textContent; - return transmEnvioResponse.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'); + const base64Binary = getLableXmlDoc.getElementsByTagName('EtiquetaFile')[0].textContent; + + const expedition = await models.Expedition.findById(expeditionFk); + await expedition.updateAttribute('externalId', shipmentId); + + return base64Binary; }; }; diff --git a/back/methods/mrw-config/template.ejs b/back/methods/mrw-config/template.ejs deleted file mode 100644 index 1641c9ec2..000000000 --- a/back/methods/mrw-config/template.ejs +++ /dev/null @@ -1,40 +0,0 @@ - - - - <%= mrw.franchiseCode %> - <%= mrw.subscriberCode %> - <%= mrw.user %> - <%= mrw.password %> - - - - - - - - <%= expedition.street %> - <%= expedition.postalCode %> - <%= expedition.city %> - <%= expedition.countryCode %> - - <%= expedition.fi %> - <%= expedition.clientName %> - <%= expedition.phone %> - - - <%= expedition.created %> - <%= expedition.expeditionId %> - <%= expedition.serviceType %> - - - <%= expedition.height %> - <%= expedition.depth %> - <%= expedition.width %> - - - <%= expedition.kg %> - - - - - \ No newline at end of file diff --git a/back/models/mrw-config.js b/back/models/mrw-config.js index ccc0ca3ac..069ef345a 100644 --- a/back/models/mrw-config.js +++ b/back/models/mrw-config.js @@ -1,4 +1,4 @@ module.exports = Self => { require('../methods/mrw-config/shipmentCreation')(Self); - require('../methods/mrw-config/renderer')(Self); + require('../methods/mrw-config/cancelShipment')(Self); }; diff --git a/back/models/mrw-config.json b/back/models/mrw-config.json index 39e5f4318..f0cf799b1 100644 --- a/back/models/mrw-config.json +++ b/back/models/mrw-config.json @@ -18,6 +18,9 @@ "user": { "type": "string" }, + "password": { + "type": "string" + }, "franchiseCode": { "type": "string" }, diff --git a/db/changes/240401/00-mrwWebService.sql b/db/changes/240401/00-mrwWebService.sql index 9a802104f..220f5138e 100644 --- a/db/changes/240401/00-mrwWebService.sql +++ b/db/changes/240401/00-mrwWebService.sql @@ -13,7 +13,10 @@ COLLATE=utf8mb3_unicode_ci; INSERT INTO vn.mrwConfig (url, `user`,password,franchiseCode,subscriberCode) - VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '009731', '04301'); + VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '04301', '009731'); + +INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) + VALUES('MrwConfig', 'cancelShipment', 'WRITE', 'ALLOW', 'ROLE', 'employee'); INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) VALUES ('MrwConfig','renderer','READ','ALLOW','ROLE','employee'); @@ -33,3 +36,5 @@ INSERT INTO vn.ticket (id, clientFk, warehouseFk, shipped, nickname, refFk, addr INSERT INTO vn.expedition (agencyModeFk,ticketFk,freightItemFk,created,counter,workerFk,packagingFk,hostFk,stateTypeFk,hasNewRoute,isBox,editorFk) VALUES (25,33,71,'2001-01-01 01:00:00.000',1,18,'94','',3,0,71,100); + +INSERT INTO vn.mrwService (agencyModeCodeFk, clientType, serviceType, kg) VALUES('MRW', 9731, 205, 10); \ No newline at end of file