From 73c0ca53828fe3436aa96a6dd16a53cf5542802f Mon Sep 17 00:00:00 2001 From: pablone Date: Mon, 15 Jan 2024 15:00:36 +0100 Subject: [PATCH 01/11] feat(mrwXml): refs #6403 render and transmEnvio integrated --- back/methods/mrw-config/renderer.js | 59 +++++++++++++++++++++ back/methods/mrw-config/shipmentCreation.js | 46 ++++++++++++++++ back/methods/mrw-config/template.ejs | 40 ++++++++++++++ back/model-config.json | 3 ++ back/models/mrw-config.js | 4 ++ back/models/mrw-config.json | 28 ++++++++++ db/changes/240401/00-mrwWebService.sql | 35 ++++++++++++ 7 files changed, 215 insertions(+) create mode 100644 back/methods/mrw-config/renderer.js create mode 100644 back/methods/mrw-config/shipmentCreation.js create mode 100644 back/methods/mrw-config/template.ejs create mode 100644 back/models/mrw-config.js create mode 100644 back/models/mrw-config.json create mode 100644 db/changes/240401/00-mrwWebService.sql diff --git a/back/methods/mrw-config/renderer.js b/back/methods/mrw-config/renderer.js new file mode 100644 index 000000000..33ac5cdd0 --- /dev/null +++ b/back/methods/mrw-config/renderer.js @@ -0,0 +1,59 @@ +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.js b/back/methods/mrw-config/shipmentCreation.js new file mode 100644 index 000000000..c75fb793d --- /dev/null +++ b/back/methods/mrw-config/shipmentCreation.js @@ -0,0 +1,46 @@ +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; + }; +}; diff --git a/back/methods/mrw-config/template.ejs b/back/methods/mrw-config/template.ejs new file mode 100644 index 000000000..1641c9ec2 --- /dev/null +++ b/back/methods/mrw-config/template.ejs @@ -0,0 +1,40 @@ + + + + <%= 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/model-config.json b/back/model-config.json index ebc0e321b..b501680d0 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -156,6 +156,9 @@ }, "ViaexpressConfig": { "dataSource": "vn" + }, + "MrwConfig": { + "dataSource": "vn" } } diff --git a/back/models/mrw-config.js b/back/models/mrw-config.js new file mode 100644 index 000000000..ccc0ca3ac --- /dev/null +++ b/back/models/mrw-config.js @@ -0,0 +1,4 @@ +module.exports = Self => { + require('../methods/mrw-config/shipmentCreation')(Self); + require('../methods/mrw-config/renderer')(Self); +}; diff --git a/back/models/mrw-config.json b/back/models/mrw-config.json new file mode 100644 index 000000000..39e5f4318 --- /dev/null +++ b/back/models/mrw-config.json @@ -0,0 +1,28 @@ +{ + "name": "MrwConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "mrwConfig" + } + }, + "properties": { + "id": { + "type": "number", + "required": true + }, + "url": { + "type": "string", + "required": true + }, + "user": { + "type": "string" + }, + "franchiseCode": { + "type": "string" + }, + "subscriberCode": { + "type": "string" + } + } +} diff --git a/db/changes/240401/00-mrwWebService.sql b/db/changes/240401/00-mrwWebService.sql new file mode 100644 index 000000000..9a802104f --- /dev/null +++ b/db/changes/240401/00-mrwWebService.sql @@ -0,0 +1,35 @@ +CREATE TABLE IF NOT EXISTS vn.mrwConfig ( + id INT auto_increment NULL, + url varchar(100) NULL, + `user` varchar(100) NULL, + password varchar(100) NULL, + franchiseCode varchar(100) NULL, + subscriberCode varchar(100) NULL, + CONSTRAINT mrwConfig_pk PRIMARY KEY (id) +) +ENGINE=InnoDB +DEFAULT CHARSET=utf8mb3 +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'); + +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('MrwConfig','renderer','READ','ALLOW','ROLE','employee'); + +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('MrwConfig','shipmentCreation','WRITE','ALLOW','ROLE','employee'); + + +INSERT INTO vn.agency (name,warehouseFk,warehouseAliasFk,isOwn,isAnyVolumeAllowed) + VALUES ('MRW',1,1,0,0); + +INSERT INTO vn.agencyMode (id, name, description, deliveryMethodFk, m3, web, agencyFk, inflation, isVolumetric, reportMail, showAgencyName, isActive, isExternalAgency, flag, code, isRiskFree, hasWeightVolumetric) + VALUES(25, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0); + +INSERT INTO vn.ticket (id, clientFk, warehouseFk, shipped, nickname, refFk, addressFk, workerFk, observations, isSigned, isLabeled, isPrinted, packages, location, `hour`, created, isBlocked, solution, routeFk, priority, hasPriority, companyFk, agencyModeFk, landed, isBoxed, isDeleted, zoneFk, zonePrice, zoneBonus, totalWithVat, totalWithoutVat, weight, clonedFrom, cmrFk, editorFk) + VALUES(33, 1101, 1, '2001-01-01 00:00:00.000', 'MRW', NULL, 1, NULL, NULL, 0, 0, 0, 1, NULL, 0, '2001-01-01 00:00:00.000', 1, NULL, 6, NULL, 1, 442, 25, '2001-01-02', 0, 0, 3, 5.00, 1.00, 8.88, 8.07, NULL, NULL, NULL, 100); + +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); From 7e3660aece556117b4605f1322ca098283facbf2 Mon Sep 17 00:00:00 2001 From: pablone Date: Tue, 16 Jan 2024 15:04:06 +0100 Subject: [PATCH 02/11] 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 From 854447f7c1d12f0cb1d0dad2b2421ffc7b672426 Mon Sep 17 00:00:00 2001 From: pablone Date: Thu, 18 Jan 2024 07:36:21 +0100 Subject: [PATCH 03/11] feat: refs #6403 mrw cancelShipment --- back/methods/mrw-config/cancelShipment.js | 6 ++-- ...hipmentCreation.ejs => createShipment.ejs} | 0 ...{shipmentCreation.js => createShipment.js} | 12 +++---- back/models/mrw-config.js | 2 +- db/changes/240401/00-mrwWebService.sql | 32 +++++++++---------- 5 files changed, 25 insertions(+), 27 deletions(-) rename back/methods/mrw-config/{shipmentCreation.ejs => createShipment.ejs} (100%) rename back/methods/mrw-config/{shipmentCreation.js => createShipment.js} (90%) diff --git a/back/methods/mrw-config/cancelShipment.js b/back/methods/mrw-config/cancelShipment.js index fe8af1caa..86e3277a9 100644 --- a/back/methods/mrw-config/cancelShipment.js +++ b/back/methods/mrw-config/cancelShipment.js @@ -1,5 +1,6 @@ const axios = require('axios'); const fs = require('fs'); +const ejs = require('ejs'); const {DOMParser} = require('xmldom'); module.exports = Self => { @@ -21,7 +22,7 @@ module.exports = Self => { } }); - Self.deleteExpedition = async expeditionFk => { + Self.cancelShipment = async expeditionFk => { const models = Self.app.models; const mrw = await models.MrwConfig.findOne(); @@ -39,8 +40,7 @@ module.exports = Self => { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlString, 'text/xml'); const resultElement = xmlDoc.getElementsByTagName('Mensaje')[0]; - const result = resultElement.textContent; - return result; + return resultElement.textContent; }; }; diff --git a/back/methods/mrw-config/shipmentCreation.ejs b/back/methods/mrw-config/createShipment.ejs similarity index 100% rename from back/methods/mrw-config/shipmentCreation.ejs rename to back/methods/mrw-config/createShipment.ejs diff --git a/back/methods/mrw-config/shipmentCreation.js b/back/methods/mrw-config/createShipment.js similarity index 90% rename from back/methods/mrw-config/shipmentCreation.js rename to back/methods/mrw-config/createShipment.js index b1574784f..0ed4ccf65 100644 --- a/back/methods/mrw-config/shipmentCreation.js +++ b/back/methods/mrw-config/createShipment.js @@ -4,8 +4,8 @@ const fs = require('fs'); const ejs = require('ejs'); module.exports = Self => { - Self.remoteMethod('shipmentCreation', { - description: 'Create an expedition and return a label', + Self.remoteMethod('createShipment', { + description: 'Create an expedition and return a base64Binary label', accessType: 'WRITE', accepts: [{ arg: 'expeditionFk', @@ -17,12 +17,12 @@ module.exports = Self => { root: true }, http: { - path: `/shipmentCreation`, + path: `/createShipment`, verb: 'POST' } }); - Self.shipmentCreation = async expeditionFk => { + Self.createShipment = async expeditionFk => { const models = Self.app.models; const mrw = await models.MrwConfig.findOne(); @@ -56,7 +56,7 @@ module.exports = Self => { WHERE e.id = ?`, [expeditionFk] ); - const shipmentTemplate = fs.readFileSync(__dirname + '/shipmentCreation.ejs', 'utf-8'); + const shipmentTemplate = fs.readFileSync(__dirname + '/createShipment.ejs', 'utf-8'); const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData}); const shipmentResponse = await axios.post(mrw.url, renderedShipment, { headers: { @@ -75,7 +75,7 @@ module.exports = Self => { } }); const getLableXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml'); - const base64Binary = getLableXmlDoc.getElementsByTagName('EtiquetaFile')[0].textContent; + const base64Binary = getLableXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent; const expedition = await models.Expedition.findById(expeditionFk); await expedition.updateAttribute('externalId', shipmentId); diff --git a/back/models/mrw-config.js b/back/models/mrw-config.js index 069ef345a..f764b91cc 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/createShipment')(Self); require('../methods/mrw-config/cancelShipment')(Self); }; diff --git a/db/changes/240401/00-mrwWebService.sql b/db/changes/240401/00-mrwWebService.sql index 220f5138e..9c85cbf49 100644 --- a/db/changes/240401/00-mrwWebService.sql +++ b/db/changes/240401/00-mrwWebService.sql @@ -1,38 +1,36 @@ -CREATE TABLE IF NOT EXISTS vn.mrwConfig ( - id INT auto_increment NULL, - url varchar(100) NULL, +CREATE TABLE IF NOT EXISTS `vn`.`mrwConfig` ( + `id` INT auto_increment NULL, + `url` varchar(100) NULL, `user` varchar(100) NULL, - password varchar(100) NULL, - franchiseCode varchar(100) NULL, - subscriberCode varchar(100) NULL, + `password` varchar(100) NULL, + `franchiseCode` varchar(100) NULL, + `subscriberCode` varchar(100) NULL, CONSTRAINT mrwConfig_pk PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; +ALTER TABLE `vn`.`packingSite` ADD `hasNewLabelMrwMethod` BOOL NULL; -INSERT INTO vn.mrwConfig (url, `user`,password,franchiseCode,subscriberCode) +INSERT INTO `vn`.`mrwConfig` (`url`, `user`, `password`, `franchiseCode`, `subscriberCode`) VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '04301', '009731'); -INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) +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'); - -INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) - VALUES ('MrwConfig','shipmentCreation','WRITE','ALLOW','ROLE','employee'); +INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`principalType`,`principalId`) + VALUES ('MrwConfig','createShipment','WRITE','ALLOW','ROLE','employee'); -INSERT INTO vn.agency (name,warehouseFk,warehouseAliasFk,isOwn,isAnyVolumeAllowed) +INSERT INTO `vn`.`agency` (`name`,`warehouseFk`,`warehouseAliasFk`,`isOwn`,`isAnyVolumeAllowed`) VALUES ('MRW',1,1,0,0); -INSERT INTO vn.agencyMode (id, name, description, deliveryMethodFk, m3, web, agencyFk, inflation, isVolumetric, reportMail, showAgencyName, isActive, isExternalAgency, flag, code, isRiskFree, hasWeightVolumetric) +INSERT INTO `vn`.`agencyMode` (`id`, `name`, `description`, `deliveryMethodFk`, `m3`, `web`, `agencyFk`, `inflation`, `isVolumetric`, `reportMail`, `showAgencyName`, `isActive`, `isExternalAgency`, `flag`, `code`, `isRiskFree`, `hasWeightVolumetric`) VALUES(25, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0); -INSERT INTO vn.ticket (id, clientFk, warehouseFk, shipped, nickname, refFk, addressFk, workerFk, observations, isSigned, isLabeled, isPrinted, packages, location, `hour`, created, isBlocked, solution, routeFk, priority, hasPriority, companyFk, agencyModeFk, landed, isBoxed, isDeleted, zoneFk, zonePrice, zoneBonus, totalWithVat, totalWithoutVat, weight, clonedFrom, cmrFk, editorFk) - VALUES(33, 1101, 1, '2001-01-01 00:00:00.000', 'MRW', NULL, 1, NULL, NULL, 0, 0, 0, 1, NULL, 0, '2001-01-01 00:00:00.000', 1, NULL, 6, NULL, 1, 442, 25, '2001-01-02', 0, 0, 3, 5.00, 1.00, 8.88, 8.07, NULL, NULL, NULL, 100); +INSERT INTO `vn`.`ticket` (`id`, `clientFk`, `warehouseFk`, `shipped`, `nickname`, `refFk`, `addressFk`, `workerFk`, `observations`, `isSigned`, `isLabeled`, `isPrinted`, `packages`, `location`, `hour`, `created`, `isBlocked`, `solution`, `routeFk`, `priority`, `hasPriority`, `companyFk`, `agencyModeFk`, `landed`, `isBoxed`, `isDeleted`, `zoneFk`, `zonePrice`, `zoneBonus`, `totalWithVat`, `totalWithoutVat`, `weight`, `clonedFrom`, `cmrFk`, `editorFk`) + VALUES 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); From 065dd0581d51609157b1ce94c2743a33593413ba Mon Sep 17 00:00:00 2001 From: pablone Date: Wed, 24 Jan 2024 09:06:05 +0100 Subject: [PATCH 04/11] feat(spec): refs #6403 add test --- back/methods/mrw-config/createShipment.ejs | 1 + back/methods/mrw-config/createShipment.js | 11 +++++------ db/changes/240401/00-mrwWebService.sql | 12 ++++++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/back/methods/mrw-config/createShipment.ejs b/back/methods/mrw-config/createShipment.ejs index e84cfce39..bf8a07dab 100644 --- a/back/methods/mrw-config/createShipment.ejs +++ b/back/methods/mrw-config/createShipment.ejs @@ -32,6 +32,7 @@ <%= expeditionData.expeditionDataId %> <%= expeditionData.serviceType %> 1 + <%= expeditionData.weekDays %> <%= expeditionData.kg %> diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index 0ed4ccf65..f6f57e06f 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -40,15 +40,14 @@ module.exports = Self => { 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 + LPAD(IF(mw.params IS NULL, ms.serviceType, mw.serviceType), 4 ,'0') serviceType, + IFNULL(mw.weekdays, "N") weekDays 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 + LEFT JOIN mrwServiceWeekday mw ON mw.params = DATE_FORMAT(e.created, '%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 @@ -74,8 +73,8 @@ module.exports = Self => { 'Content-Type': 'text/xml; charset=utf-8' } }); - const getLableXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml'); - const base64Binary = getLableXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent; + const getLabelXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml'); + const base64Binary = getLabelXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent; const expedition = await models.Expedition.findById(expeditionFk); await expedition.updateAttribute('externalId', shipmentId); diff --git a/db/changes/240401/00-mrwWebService.sql b/db/changes/240401/00-mrwWebService.sql index 9c85cbf49..eb6aab887 100644 --- a/db/changes/240401/00-mrwWebService.sql +++ b/db/changes/240401/00-mrwWebService.sql @@ -13,6 +13,8 @@ COLLATE=utf8mb3_unicode_ci; ALTER TABLE `vn`.`packingSite` ADD `hasNewLabelMrwMethod` BOOL NULL; +INSERT INTO vn.mrwService (agencyModeCodeFk, clientType, serviceType, kg) VALUES('mrw', 9731, 205, 10); + INSERT INTO `vn`.`mrwConfig` (`url`, `user`, `password`, `franchiseCode`, `subscriberCode`) VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '04301', '009731'); @@ -27,12 +29,14 @@ INSERT INTO `vn`.`agency` (`name`,`warehouseFk`,`warehouseAliasFk`,`isOwn`,`isAn VALUES ('MRW',1,1,0,0); INSERT INTO `vn`.`agencyMode` (`id`, `name`, `description`, `deliveryMethodFk`, `m3`, `web`, `agencyFk`, `inflation`, `isVolumetric`, `reportMail`, `showAgencyName`, `isActive`, `isExternalAgency`, `flag`, `code`, `isRiskFree`, `hasWeightVolumetric`) - VALUES(25, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0); + VALUES(25, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0), + (26, 'mrwEcom', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'mrwEcom', 0, 0); INSERT INTO `vn`.`ticket` (`id`, `clientFk`, `warehouseFk`, `shipped`, `nickname`, `refFk`, `addressFk`, `workerFk`, `observations`, `isSigned`, `isLabeled`, `isPrinted`, `packages`, `location`, `hour`, `created`, `isBlocked`, `solution`, `routeFk`, `priority`, `hasPriority`, `companyFk`, `agencyModeFk`, `landed`, `isBoxed`, `isDeleted`, `zoneFk`, `zonePrice`, `zoneBonus`, `totalWithVat`, `totalWithoutVat`, `weight`, `clonedFrom`, `cmrFk`, `editorFk`) - VALUES + VALUES(44, 1101, 1, '2001-01-01 00:00:00.000', 'MRW', NULL, 1, NULL, NULL, 0, 0, 0, 1, NULL, 0, '2001-01-01 00:00:00.000', 1, NULL, 6, NULL, 1, 442, 25, '2001-01-02', 0, 0, 3, 5.00, 1.00, 8.88, 8.07, NULL, NULL, NULL, 100); 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); + VALUES (25,44,71, CURDATE(),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 +INSERT INTO vn.mrwService (agencyModeCodeFk, clientType, serviceType, kg) VALUES('MRW', 9731, 205, 10); +INSERT INTO vn.mrwServiceWeekday (agencyModeCodeFk, weekdays, serviceType, params) VALUES('mrwEcom', 'sat', 800, 'EntregaSabado=S'); \ No newline at end of file From 6c9215bb517ea1700a3a0552b46fd38d94d07c48 Mon Sep 17 00:00:00 2001 From: pablone Date: Wed, 7 Feb 2024 16:52:43 +0100 Subject: [PATCH 05/11] feat(mrw): refs #6403 big commit --- back/methods/mrw-config/createShipment.js | 49 +++++++++---------- back/methods/mrw-config/expeditionData.sql | 27 ++++++++++ .../mrw-config/specs/createShipment.spec.js | 45 +++++++++++++++++ db/changes/240401/00-mrwWebService.sql | 26 ++++------ loopback/locale/es.json | 12 ++++- 5 files changed, 115 insertions(+), 44 deletions(-) create mode 100644 back/methods/mrw-config/expeditionData.sql create mode 100644 back/methods/mrw-config/specs/createShipment.spec.js diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index f6f57e06f..505b7b167 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -2,6 +2,7 @@ const axios = require('axios'); const {DOMParser} = require('xmldom'); const fs = require('fs'); const ejs = require('ejs'); +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('createShipment', { @@ -26,35 +27,20 @@ module.exports = Self => { const models = Self.app.models; const mrw = await models.MrwConfig.findOne(); + if (!mrw) + throw new UserError(`Some mrwConfig parameters are not set`); + 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(IF(mw.params IS NULL, ms.serviceType, mw.serviceType), 4 ,'0') serviceType, - IFNULL(mw.weekdays, "N") weekDays - 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 - LEFT JOIN mrwServiceWeekday mw ON mw.params = DATE_FORMAT(e.created, '%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 = ?`, [expeditionFk] + fs.readFileSync(__dirname + '/expeditionData.sql', 'utf-8'), + [expeditionFk] ); + if (!expeditionData) + throw new UserError(`This expedition is not a MRW shipment`); + + if (expeditionData?.created < Date.vnNew()) + throw new UserError(`This ticket has a shipped date earlier than today`); + const shipmentTemplate = fs.readFileSync(__dirname + '/createShipment.ejs', 'utf-8'); const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData}); const shipmentResponse = await axios.post(mrw.url, renderedShipment, { @@ -66,6 +52,11 @@ module.exports = Self => { const shipmentXmlDoc = parser.parseFromString(shipmentResponse.data, 'text/xml'); const shipmentId = shipmentXmlDoc.getElementsByTagName('NumeroEnvio')[0].textContent; + if (!shipmentId) { + const message = shipmentXmlDoc.getElementsByTagName('Mensaje')[0]?.textContent; + throw new UserError(message); + } + const getLabelTemplate = fs.readFileSync(__dirname + '/getLabel.ejs', 'utf-8'); const renderedGetLabel = ejs.render(getLabelTemplate, {mrw, shipmentId}); const getLabelResponse = await axios.post(mrw.url, renderedGetLabel, { @@ -76,6 +67,12 @@ module.exports = Self => { const getLabelXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml'); const base64Binary = getLabelXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent; + if (!base64Binary) { + const message = getLabelXmlDoc.getElementsByTagName('Mensaje')[0]?.textContent; + if (!message) + throw new UserError(`The MRW web service is not returning the expected response`); + throw new UserError(message); + } const expedition = await models.Expedition.findById(expeditionFk); await expedition.updateAttribute('externalId', shipmentId); diff --git a/back/methods/mrw-config/expeditionData.sql b/back/methods/mrw-config/expeditionData.sql new file mode 100644 index 000000000..2a325cc27 --- /dev/null +++ b/back/methods/mrw-config/expeditionData.sql @@ -0,0 +1,27 @@ +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, + 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 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 + 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 \ No newline at end of file diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js new file mode 100644 index 000000000..2b5d555a1 --- /dev/null +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -0,0 +1,45 @@ +const models = require('vn-loopback/server/server').models; +const axios = require('axios'); + +const expeditionFk = 14; +const mockShipmentId = 'baseMockShipmentId'; +const mockBase64Binary = 'base64BinaryString'; +const ticket1 = { + 'id': '44', + 'clientFk': 1101, + 'shipped': Date.vnNew(), + 'nickname': 'MRW', + 'addressFk': 1, + 'agencyModeFk': 26 +}; + +const expedition1 = { + 'id': 14, + 'agencyModeFk': 26, + 'ticketFk': 44, + 'freightItemFk': 71, + 'created': '2001-01-01', + 'counter': 1, + 'workerFk': 18, + 'packagingFk': '94', + 'hostFk': '', + 'stateTypeFk': 3, + 'hasNewRoute': 0, + 'isBox': 71, + 'editorFk': 100 +}; +fdescribe('MRWConfig createShipment()', () => { + it('should create a shipment and return a base64Binary label', async() => { + const tx = await models.MrwConfig.beginTransaction({}); + const options = {transaction: tx}; + + await models.Ticket.create(ticket1, options); + const expedition = await models.Expedition.create(expedition1, options); + + spyOn(axios, 'post').and.returnValues([{data: mockShipmentId}, {data: mockBase64Binary}]); + + const base64Binary = await models.MrwConfig.createShipment(expedition.id, options); + + expect(base64Binary).toEqual(mockBase64Binary); + }); +}); diff --git a/db/changes/240401/00-mrwWebService.sql b/db/changes/240401/00-mrwWebService.sql index eb6aab887..a3c42ad8b 100644 --- a/db/changes/240401/00-mrwWebService.sql +++ b/db/changes/240401/00-mrwWebService.sql @@ -7,13 +7,19 @@ CREATE TABLE IF NOT EXISTS `vn`.`mrwConfig` ( `subscriberCode` varchar(100) NULL, CONSTRAINT mrwConfig_pk PRIMARY KEY (id) ) -ENGINE=InnoDB +ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; ALTER TABLE `vn`.`packingSite` ADD `hasNewLabelMrwMethod` BOOL NULL; -INSERT INTO vn.mrwService (agencyModeCodeFk, clientType, serviceType, kg) VALUES('mrw', 9731, 205, 10); + +INSERT INTO `vn`.`agency` (`name`,`warehouseFk`,`warehouseAliasFk`,`isOwn`,`isAnyVolumeAllowed`) + VALUES ('MRW',1,1,0,0); + +INSERT INTO `vn`.`agencyMode` (`id`, `name`, `description`, `deliveryMethodFk`, `m3`, `web`, `agencyFk`, `inflation`, `isVolumetric`, `reportMail`, `showAgencyName`, `isActive`, `isExternalAgency`, `flag`, `code`, `isRiskFree`, `hasWeightVolumetric`) + VALUES(26, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0), + (27, 'mrwEcom', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'mrwEcom', 0, 0); INSERT INTO `vn`.`mrwConfig` (`url`, `user`, `password`, `franchiseCode`, `subscriberCode`) VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '04301', '009731'); @@ -24,19 +30,7 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `pri INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`principalType`,`principalId`) VALUES ('MrwConfig','createShipment','WRITE','ALLOW','ROLE','employee'); +INSERT INTO `vn`.`mrwService` (`agencyModeCodeFk`, `clientType`, `serviceType`, `kg`) VALUES('MRW', 9731, 205, 10); +INSERT INTO `vn`.`mrwServiceWeekday` (`agencyModeCodeFk`, `weekdays`, `serviceType`, `params`) VALUES('mrwEcom', 'sat', 800, 'EntregaSabado=S'); -INSERT INTO `vn`.`agency` (`name`,`warehouseFk`,`warehouseAliasFk`,`isOwn`,`isAnyVolumeAllowed`) - VALUES ('MRW',1,1,0,0); -INSERT INTO `vn`.`agencyMode` (`id`, `name`, `description`, `deliveryMethodFk`, `m3`, `web`, `agencyFk`, `inflation`, `isVolumetric`, `reportMail`, `showAgencyName`, `isActive`, `isExternalAgency`, `flag`, `code`, `isRiskFree`, `hasWeightVolumetric`) - VALUES(25, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0), - (26, 'mrwEcom', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'mrwEcom', 0, 0); - -INSERT INTO `vn`.`ticket` (`id`, `clientFk`, `warehouseFk`, `shipped`, `nickname`, `refFk`, `addressFk`, `workerFk`, `observations`, `isSigned`, `isLabeled`, `isPrinted`, `packages`, `location`, `hour`, `created`, `isBlocked`, `solution`, `routeFk`, `priority`, `hasPriority`, `companyFk`, `agencyModeFk`, `landed`, `isBoxed`, `isDeleted`, `zoneFk`, `zonePrice`, `zoneBonus`, `totalWithVat`, `totalWithoutVat`, `weight`, `clonedFrom`, `cmrFk`, `editorFk`) - VALUES(44, 1101, 1, '2001-01-01 00:00:00.000', 'MRW', NULL, 1, NULL, NULL, 0, 0, 0, 1, NULL, 0, '2001-01-01 00:00:00.000', 1, NULL, 6, NULL, 1, 442, 25, '2001-01-02', 0, 0, 3, 5.00, 1.00, 8.88, 8.07, NULL, NULL, NULL, 100); - -INSERT INTO vn.expedition (agencyModeFk,ticketFk,freightItemFk,created,counter,workerFk,packagingFk,hostFk,stateTypeFk,hasNewRoute,isBox,editorFk) - VALUES (25,44,71, CURDATE(),1,18,'94','',3,0,71,100); - -INSERT INTO vn.mrwService (agencyModeCodeFk, clientType, serviceType, kg) VALUES('MRW', 9731, 205, 10); -INSERT INTO vn.mrwServiceWeekday (agencyModeCodeFk, weekdays, serviceType, params) VALUES('mrwEcom', 'sat', 800, 'EntregaSabado=S'); \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index a8134909e..e0025c5c0 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -329,5 +329,13 @@ "The amount cannot be less than the minimum": "La cantidad no puede ser menor que la cantidad mínima", "quantityLessThanMin": "La cantidad no puede ser menor que la cantidad mínima", "Cannot past travels with entries": "No se pueden pasar envíos con entradas", - "It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}" -} + "It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}", + "Esa expedición no es mrw": "Esa expedición no es mrw", + "This expedition is not configured as MRW": "This expedition is not configured as MRW", + "Some config parameters are not set": "Some config parameters are not set", + "The MRW web service is not returning the expected response": "The MRW web service is not returning the expected response", + "1) No se han encontrado datos con estos criterios.": "1) No se han encontrado datos con estos criterios.", + "1) Fecha de recogida incorrecta (fecha cerrada).": "1) Fecha de recogida incorrecta (fecha cerrada).", + "1) Falta indicar la Hora Máxima de Recogida en Sábado2) Fecha de recogida incorrecta (fecha cerrada).": "1) Falta indicar la Hora Máxima de Recogida en Sábado2) Fecha de recogida incorrecta (fecha cerrada).", + "1) Falta indicar la Hora Máxima de Recogida en Sábado2) La fecha de recogida solicitada es 10/02/2024.3) El tipo de servicio no está permitido.4) ": "1) Falta indicar la Hora Máxima de Recogida en Sábado2) La fecha de recogida solicitada es 10/02/2024.3) El tipo de servicio no está permitido.4) " +} \ No newline at end of file From fe79823ce8c0f12f2bbb9ddf417e21c6365f1a4c Mon Sep 17 00:00:00 2001 From: pablone Date: Mon, 12 Feb 2024 09:24:37 +0100 Subject: [PATCH 06/11] refs #6403 --- .../mrw-config/specs/createShipment.spec.js | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js index 2b5d555a1..b80f023e5 100644 --- a/back/methods/mrw-config/specs/createShipment.spec.js +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -10,12 +10,12 @@ const ticket1 = { 'shipped': Date.vnNew(), 'nickname': 'MRW', 'addressFk': 1, - 'agencyModeFk': 26 + 'agencyModeFk': 999 }; const expedition1 = { 'id': 14, - 'agencyModeFk': 26, + 'agencyModeFk': 999, 'ticketFk': 44, 'freightItemFk': 71, 'created': '2001-01-01', @@ -28,18 +28,52 @@ const expedition1 = { 'isBox': 71, 'editorFk': 100 }; +const tusabeh = async() => { + return {}; +}; + fdescribe('MRWConfig createShipment()', () => { + beforeAll(async() => { + + }); + it('should create a shipment and return a base64Binary label', async() => { - const tx = await models.MrwConfig.beginTransaction({}); - const options = {transaction: tx}; + const options = {transaction: await models.MrwConfig.beginTransaction({})}; + await models.Agency.create( + {'id': 999, 'name': 'mrw'}, + options + ); - await models.Ticket.create(ticket1, options); - const expedition = await models.Expedition.create(expedition1, options); + await models.AgencyMode.create( + {'id': 999, 'name': 'mrw', 'agencyFk': 999, 'code': 'mrw'}, + options + ); + await models.MrwConfig.create( + { + 'url': 'https://url.com', + 'user': 'user', + 'password': 'password', + 'franchiseCode': 'franchiseCode', + 'subscriberCode': 'subscriberCode' + }, options + ); + + await models.Application.rawSql( + `INSERT INTO vn.mrwService + SET agencyModeCodeFk = 'mrw', + clientType = 1, + serviceType = 1, + kg = 1`, null, options + ); + const ticket = models.Ticket.create(ticket1, options); + const expedition = models.Expedition.create(expedition1, options); spyOn(axios, 'post').and.returnValues([{data: mockShipmentId}, {data: mockBase64Binary}]); const base64Binary = await models.MrwConfig.createShipment(expedition.id, options); expect(base64Binary).toEqual(mockBase64Binary); + expect(1).toEqual(1); }); }); + From b6be76c38fec4a38ed1cbd6bb62246e9fd306ce2 Mon Sep 17 00:00:00 2001 From: pablone Date: Fri, 16 Feb 2024 08:28:40 +0100 Subject: [PATCH 07/11] feat(test): refs #6403 add spec --- back/methods/mrw-config/createShipment.js | 88 +++++++++++-------- back/methods/mrw-config/expeditionData.sql | 3 +- .../mrw-config/specs/createShipment.spec.js | 52 ++++++----- .../mrw-config/specs/mockCreateShipment.xml | 16 ++++ .../methods/mrw-config/specs/mockGetLabel.xml | 14 +++ 5 files changed, 113 insertions(+), 60 deletions(-) create mode 100644 back/methods/mrw-config/specs/mockCreateShipment.xml create mode 100644 back/methods/mrw-config/specs/mockGetLabel.xml diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index 505b7b167..11b9527f1 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -5,7 +5,7 @@ const ejs = require('ejs'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethod('createShipment', { + Self.remoteMethodCtx('createShipment', { description: 'Create an expedition and return a base64Binary label', accessType: 'WRITE', accepts: [{ @@ -23,59 +23,71 @@ module.exports = Self => { } }); - Self.createShipment = async expeditionFk => { + Self.createShipment = async(ctx, expeditionFk, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const models = Self.app.models; - const mrw = await models.MrwConfig.findOne(); + const mrw = await models.MrwConfig.findOne(null, myOptions); + console.log('mrw: ', mrw); if (!mrw) throw new UserError(`Some mrwConfig parameters are not set`); const [expeditionData] = await Self.rawSql( fs.readFileSync(__dirname + '/expeditionData.sql', 'utf-8'), - [expeditionFk] + null, + myOptions ); - + console.log('sigue'); if (!expeditionData) throw new UserError(`This expedition is not a MRW shipment`); if (expeditionData?.created < Date.vnNew()) - throw new UserError(`This ticket has a shipped date earlier than today`); + throw new UserError(`This ticket has a shipped date earlier than today`); po; - const shipmentTemplate = fs.readFileSync(__dirname + '/createShipment.ejs', 'utf-8'); - const renderedShipment = ejs.render(shipmentTemplate, {mrw, expeditionData}); - const shipmentResponse = await axios.post(mrw.url, renderedShipment, { + const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData}); + + const parser = new DOMParser(); + const shipmentXmlDoc = parser.parseFromString(shipmentResponse, 'text/xml'); + const shipmentId = getTextByTag(shipmentXmlDoc, 'NumeroEnvio'); + + if (!shipmentId) + throw new UserError(getTextByTag(shipmentXmlDoc, 'Mensaje')); + + const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId}); + + const getLabelXmlDoc = parser.parseFromString(getLabelResponse, 'text/xml'); + const file = getTextByTag(getLabelXmlDoc, 'EtiquetaFile'); + console.log('file: ', file); + if (!file) { + const message = getTextByTag(getLabelXmlDoc, 'Mensaje') ?? + `The MRW web service is not returning the expected response`; + throw new UserError(message); + } + await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, options); + + return file; + }; + + function getTextByTag(xmlDoc, tag) { + const parser = new DOMParser(); + const doc = parser.parseFromString(xmlDoc?.data, 'text/xml'); + return doc?.getElementsByTagName(tag)[0]?.textContent; + } + + async function sendXmlDoc(xmlDock, params) { + const xmlTemplate = fs.readFileSync(__dirname + `/${xmlDock}.ejs`, 'utf-8'); + const renderedTemplate = ejs.render(xmlTemplate, params); + const {data} = await axios.post(params.mrw.url, renderedTemplate, { headers: { 'Content-Type': 'application/soap+xml; charset=utf-8' } }); - const parser = new DOMParser(); - const shipmentXmlDoc = parser.parseFromString(shipmentResponse.data, 'text/xml'); - const shipmentId = shipmentXmlDoc.getElementsByTagName('NumeroEnvio')[0].textContent; - if (!shipmentId) { - const message = shipmentXmlDoc.getElementsByTagName('Mensaje')[0]?.textContent; - throw new UserError(message); - } - - 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 getLabelXmlDoc = parser.parseFromString(getLabelResponse.data, 'text/xml'); - const base64Binary = getLabelXmlDoc.getElementsByTagName('EtiquetaFile')[0]?.textContent; - - if (!base64Binary) { - const message = getLabelXmlDoc.getElementsByTagName('Mensaje')[0]?.textContent; - if (!message) - throw new UserError(`The MRW web service is not returning the expected response`); - throw new UserError(message); - } - const expedition = await models.Expedition.findById(expeditionFk); - await expedition.updateAttribute('externalId', shipmentId); - - return base64Binary; - }; + console.log('data: ', data); + return data; + } }; diff --git a/back/methods/mrw-config/expeditionData.sql b/back/methods/mrw-config/expeditionData.sql index 2a325cc27..0329e29d8 100644 --- a/back/methods/mrw-config/expeditionData.sql +++ b/back/methods/mrw-config/expeditionData.sql @@ -14,7 +14,6 @@ SELECT CASE co.code LPAD(IF(mw.params IS NULL, ms.serviceType, mw.serviceType), 4 ,'0') serviceType, IF(mw.weekdays, 'S', 'N') weekDays 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 @@ -23,5 +22,5 @@ SELECT CASE co.code 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 = ? + WHERE e.id = 14 LIMIT 1 \ No newline at end of file diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js index b80f023e5..a843fc167 100644 --- a/back/methods/mrw-config/specs/createShipment.spec.js +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -1,8 +1,7 @@ const models = require('vn-loopback/server/server').models; const axios = require('axios'); +const fs = require('fs'); -const expeditionFk = 14; -const mockShipmentId = 'baseMockShipmentId'; const mockBase64Binary = 'base64BinaryString'; const ticket1 = { 'id': '44', @@ -28,17 +27,11 @@ const expedition1 = { 'isBox': 71, 'editorFk': 100 }; -const tusabeh = async() => { - return {}; -}; +let options; fdescribe('MRWConfig createShipment()', () => { beforeAll(async() => { - - }); - - it('should create a shipment and return a base64Binary label', async() => { - const options = {transaction: await models.MrwConfig.beginTransaction({})}; + options = {transaction: await models.MrwConfig.beginTransaction({})}; await models.Agency.create( {'id': 999, 'name': 'mrw'}, options @@ -61,19 +54,38 @@ fdescribe('MRWConfig createShipment()', () => { await models.Application.rawSql( `INSERT INTO vn.mrwService - SET agencyModeCodeFk = 'mrw', - clientType = 1, - serviceType = 1, - kg = 1`, null, options + SET agencyModeCodeFk = 'mrw', + clientType = 1, + serviceType = 1, + kg = 1`, null, options ); - const ticket = models.Ticket.create(ticket1, options); - const expedition = models.Expedition.create(expedition1, options); - spyOn(axios, 'post').and.returnValues([{data: mockShipmentId}, {data: mockBase64Binary}]); + await models.Ticket.create(ticket1, options); + await models.Expedition.create(expedition1, options); + }); - const base64Binary = await models.MrwConfig.createShipment(expedition.id, options); + it('should create a shipment and return a base64Binary label', async() => { + try { + const returnsValues = [ + {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, + {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} + ]; + spyOn(axios, 'post').and.callFake(() => + Promise.resolve(returnsValues.pop()) + ); + const ctx = {args: {isChargedToMana: false}}; + const base64Binary = await models.MrwConfig.createShipment(ctx, expedition1.id, options); - expect(base64Binary).toEqual(mockBase64Binary); - expect(1).toEqual(1); + expect(base64Binary).toEqual(mockBase64Binary); + + await options.transaction.rollback(); + } catch (e) { + await options.transaction.rollback(); + throw e; + } }); }); +async function dbPopulate() { + +} + diff --git a/back/methods/mrw-config/specs/mockCreateShipment.xml b/back/methods/mrw-config/specs/mockCreateShipment.xml new file mode 100644 index 000000000..dacc5d3f3 --- /dev/null +++ b/back/methods/mrw-config/specs/mockCreateShipment.xml @@ -0,0 +1,16 @@ + + + + + + 1 + + 1 + 1 + http://url.com + + + + \ No newline at end of file diff --git a/back/methods/mrw-config/specs/mockGetLabel.xml b/back/methods/mrw-config/specs/mockGetLabel.xml new file mode 100644 index 000000000..0401ce2c7 --- /dev/null +++ b/back/methods/mrw-config/specs/mockGetLabel.xml @@ -0,0 +1,14 @@ + + + + + + 1 + + base64BinaryString + + + + \ No newline at end of file From 85b1391006403f6c3dbbc559d23abd010d2fbfc2 Mon Sep 17 00:00:00 2001 From: pablone Date: Fri, 16 Feb 2024 08:31:41 +0100 Subject: [PATCH 08/11] fix(sql): refs #6403 merge master --- .../10889-redMedeola/00-firstScript.sql} | 1 + 1 file changed, 1 insertion(+) rename db/{changes/240401/00-mrwWebService.sql => versions/10889-redMedeola/00-firstScript.sql} (98%) diff --git a/db/changes/240401/00-mrwWebService.sql b/db/versions/10889-redMedeola/00-firstScript.sql similarity index 98% rename from db/changes/240401/00-mrwWebService.sql rename to db/versions/10889-redMedeola/00-firstScript.sql index a3c42ad8b..78fdf2917 100644 --- a/db/changes/240401/00-mrwWebService.sql +++ b/db/versions/10889-redMedeola/00-firstScript.sql @@ -1,3 +1,4 @@ +-- Place your SQL code here CREATE TABLE IF NOT EXISTS `vn`.`mrwConfig` ( `id` INT auto_increment NULL, `url` varchar(100) NULL, From c10cb7c051de6e055002b74e0dc6d709ac4cf9e5 Mon Sep 17 00:00:00 2001 From: pablone Date: Fri, 16 Feb 2024 14:15:13 +0100 Subject: [PATCH 09/11] feat(spec): refs #6403 mrwIntegration --- back/methods/mrw-config/createShipment.js | 90 +++++++++++-------- back/methods/mrw-config/expeditionData.sql | 26 ------ .../mrw-config/specs/createShipment.spec.js | 67 +++++++++----- .../10889-redMedeola/00-firstScript.sql | 13 --- 4 files changed, 99 insertions(+), 97 deletions(-) delete mode 100644 back/methods/mrw-config/expeditionData.sql diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index 11b9527f1..be3dbd96e 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -5,7 +5,7 @@ const ejs = require('ejs'); const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('createShipment', { + Self.remoteMethod('createShipment', { description: 'Create an expedition and return a base64Binary label', accessType: 'WRITE', accepts: [{ @@ -23,71 +23,89 @@ module.exports = Self => { } }); - Self.createShipment = async(ctx, expeditionFk, options) => { + Self.createShipment = async(expeditionFk, options) => { const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); + if (!myOptions.transaction) + myOptions.transaction = await Self.beginTransaction({}); + const models = Self.app.models; const mrw = await models.MrwConfig.findOne(null, myOptions); - console.log('mrw: ', mrw); if (!mrw) throw new UserError(`Some mrwConfig parameters are not set`); - const [expeditionData] = await Self.rawSql( - fs.readFileSync(__dirname + '/expeditionData.sql', 'utf-8'), - null, - myOptions - ); - console.log('sigue'); + 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); + if (!expeditionData) throw new UserError(`This expedition is not a MRW shipment`); - if (expeditionData?.created < Date.vnNew()) - throw new UserError(`This ticket has a shipped date earlier than today`); po; + 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`); - const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData}); - - const parser = new DOMParser(); - const shipmentXmlDoc = parser.parseFromString(shipmentResponse, 'text/xml'); - const shipmentId = getTextByTag(shipmentXmlDoc, 'NumeroEnvio'); + const shipmentResponse = await sendXmlDoc('createShipment', {mrw, expeditionData}, 'application/soap+xml'); + const shipmentId = getTextByTag(shipmentResponse, 'NumeroEnvio'); if (!shipmentId) - throw new UserError(getTextByTag(shipmentXmlDoc, 'Mensaje')); + throw new UserError(getTextByTag(shipmentResponse, 'Mensaje')); - const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId}); + const getLabelResponse = await sendXmlDoc('getLabel', {mrw, shipmentId}, 'text/xml'); + const file = getTextByTag(getLabelResponse, 'EtiquetaFile'); - const getLabelXmlDoc = parser.parseFromString(getLabelResponse, 'text/xml'); - const file = getTextByTag(getLabelXmlDoc, 'EtiquetaFile'); - console.log('file: ', file); - if (!file) { - const message = getTextByTag(getLabelXmlDoc, 'Mensaje') ?? - `The MRW web service is not returning the expected response`; - throw new UserError(message); - } - await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, options); + await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, myOptions); + await myOptions.transaction.commit(); return file; }; function getTextByTag(xmlDoc, tag) { - const parser = new DOMParser(); - const doc = parser.parseFromString(xmlDoc?.data, 'text/xml'); - return doc?.getElementsByTagName(tag)[0]?.textContent; + return xmlDoc?.getElementsByTagName(tag)[0]?.textContent; } - async function sendXmlDoc(xmlDock, params) { + async function sendXmlDoc(xmlDock, params, contentType) { + const parser = new DOMParser(); + const xmlTemplate = fs.readFileSync(__dirname + `/${xmlDock}.ejs`, 'utf-8'); const renderedTemplate = ejs.render(xmlTemplate, params); - const {data} = await axios.post(params.mrw.url, renderedTemplate, { + const data = await axios.post(params.mrw.url, renderedTemplate, { headers: { - 'Content-Type': 'application/soap+xml; charset=utf-8' + 'Content-Type': `${contentType}; charset=utf-8` } }); - - console.log('data: ', data); - return data; + return parser.parseFromString(data.data, 'text/xml'); } }; diff --git a/back/methods/mrw-config/expeditionData.sql b/back/methods/mrw-config/expeditionData.sql deleted file mode 100644 index 0329e29d8..000000000 --- a/back/methods/mrw-config/expeditionData.sql +++ /dev/null @@ -1,26 +0,0 @@ -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, - 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 = 14 - LIMIT 1 \ No newline at end of file diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js index a843fc167..5babadb78 100644 --- a/back/methods/mrw-config/specs/createShipment.spec.js +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -13,7 +13,7 @@ const ticket1 = { }; const expedition1 = { - 'id': 14, + 'id': 17, 'agencyModeFk': 999, 'ticketFk': 44, 'freightItemFk': 71, @@ -27,6 +27,11 @@ const expedition1 = { 'isBox': 71, 'editorFk': 100 }; + +const returnsValues = [ + {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, + {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} +]; let options; fdescribe('MRWConfig createShipment()', () => { @@ -64,28 +69,46 @@ fdescribe('MRWConfig createShipment()', () => { }); it('should create a shipment and return a base64Binary label', async() => { - try { - const returnsValues = [ - {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, - {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} - ]; - spyOn(axios, 'post').and.callFake(() => - Promise.resolve(returnsValues.pop()) - ); - const ctx = {args: {isChargedToMana: false}}; - const base64Binary = await models.MrwConfig.createShipment(ctx, expedition1.id, options); + spyOn(axios, 'post').and.callFake(() => + Promise.resolve(returnsValues.pop()) + ); - expect(base64Binary).toEqual(mockBase64Binary); + await models.Ticket.updateAll({id: ticket1.id}, {shipped: Date.vnNew()}, options); + const base64Binary = await models.MrwConfig.createShipment(expedition1.id, options); - await options.transaction.rollback(); - } catch (e) { - await options.transaction.rollback(); - throw e; - } + expect(base64Binary).toEqual(mockBase64Binary); + }); + + it('should fail if mrwConfig has no data', async() => { + let error; + await models.MrwConfig.createShipment(expedition1.id).catch(e => { + error = e; + }).finally(async() => { + expect(error.message).toEqual(`Some mrwConfig parameters are not set`); + }); + + expect(error).toBeDefined(); + }); + + it('should fail if expeditionFk is not a MrwExpedition', async() => { + let error; + await models.MrwConfig.createShipment(undefined, options).catch(e => { + error = e; + }).finally(async() => { + expect(error.message).toEqual(`This expedition is not a MRW shipment`); + }); + }); + + it(' should fail if the creation date of this ticket is before the current date it', async() => { + let error; + const yesterday = Date.vnNew(); + + yesterday.setDate(yesterday.getDate() - 1); + await models.Ticket.updateAll({id: ticket1.id}, {shipped: yesterday}, options); + await models.MrwConfig.createShipment(expedition1.id, options).catch(e => { + error = e; + }).finally(async() => { + expect(error.message).toEqual(`This ticket has a shipped date earlier than today`); + }); }); }); - -async function dbPopulate() { - -} - diff --git a/db/versions/10889-redMedeola/00-firstScript.sql b/db/versions/10889-redMedeola/00-firstScript.sql index 78fdf2917..ecae0234f 100644 --- a/db/versions/10889-redMedeola/00-firstScript.sql +++ b/db/versions/10889-redMedeola/00-firstScript.sql @@ -14,24 +14,11 @@ COLLATE=utf8mb3_unicode_ci; ALTER TABLE `vn`.`packingSite` ADD `hasNewLabelMrwMethod` BOOL NULL; - -INSERT INTO `vn`.`agency` (`name`,`warehouseFk`,`warehouseAliasFk`,`isOwn`,`isAnyVolumeAllowed`) - VALUES ('MRW',1,1,0,0); - -INSERT INTO `vn`.`agencyMode` (`id`, `name`, `description`, `deliveryMethodFk`, `m3`, `web`, `agencyFk`, `inflation`, `isVolumetric`, `reportMail`, `showAgencyName`, `isActive`, `isExternalAgency`, `flag`, `code`, `isRiskFree`, `hasWeightVolumetric`) - VALUES(26, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0), - (27, 'mrwEcom', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'mrwEcom', 0, 0); - -INSERT INTO `vn`.`mrwConfig` (`url`, `user`, `password`, `franchiseCode`, `subscriberCode`) - 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','createShipment','WRITE','ALLOW','ROLE','employee'); -INSERT INTO `vn`.`mrwService` (`agencyModeCodeFk`, `clientType`, `serviceType`, `kg`) VALUES('MRW', 9731, 205, 10); -INSERT INTO `vn`.`mrwServiceWeekday` (`agencyModeCodeFk`, `weekdays`, `serviceType`, `params`) VALUES('mrwEcom', 'sat', 800, 'EntregaSabado=S'); From b3ce6e8d455183d01295199e9b9de1543c710eab Mon Sep 17 00:00:00 2001 From: pablone Date: Mon, 19 Feb 2024 11:33:56 +0100 Subject: [PATCH 10/11] fix(spec): refs #6403 add mrwSpec --- back/methods/mrw-config/cancelShipment.js | 2 +- back/methods/mrw-config/createShipment.js | 2 -- .../mrw-config/specs/createShipment.spec.js | 30 +++++++++++-------- loopback/locale/es.json | 7 +++-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/back/methods/mrw-config/cancelShipment.js b/back/methods/mrw-config/cancelShipment.js index 86e3277a9..d1b7a6b72 100644 --- a/back/methods/mrw-config/cancelShipment.js +++ b/back/methods/mrw-config/cancelShipment.js @@ -39,7 +39,7 @@ module.exports = Self => { const xmlString = response.data; const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlString, 'text/xml'); - const resultElement = xmlDoc.getElementsByTagName('Mensaje')[0]; + const [resultElement] = xmlDoc.getElementsByTagName('Mensaje'); return resultElement.textContent; }; diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index be3dbd96e..2b22d44fb 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -87,8 +87,6 @@ module.exports = Self => { const file = getTextByTag(getLabelResponse, 'EtiquetaFile'); await models.Expedition.updateAll({id: expeditionFk}, {externalId: shipmentId}, myOptions); - await myOptions.transaction.commit(); - return file; }; diff --git a/back/methods/mrw-config/specs/createShipment.spec.js b/back/methods/mrw-config/specs/createShipment.spec.js index 5babadb78..e7bba524a 100644 --- a/back/methods/mrw-config/specs/createShipment.spec.js +++ b/back/methods/mrw-config/specs/createShipment.spec.js @@ -28,15 +28,15 @@ const expedition1 = { 'editorFk': 100 }; -const returnsValues = [ - {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, - {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} -]; +let tx; let options; -fdescribe('MRWConfig createShipment()', () => { - beforeAll(async() => { - options = {transaction: await models.MrwConfig.beginTransaction({})}; +describe('MRWConfig createShipment()', () => { + beforeEach(async() => { + options = tx = undefined; + tx = await models.MrwConfig.beginTransaction({}); + options = {transaction: tx}; + await models.Agency.create( {'id': 999, 'name': 'mrw'}, options @@ -68,12 +68,18 @@ fdescribe('MRWConfig createShipment()', () => { await models.Expedition.create(expedition1, options); }); - it('should create a shipment and return a base64Binary label', async() => { - spyOn(axios, 'post').and.callFake(() => - Promise.resolve(returnsValues.pop()) - ); + afterEach(async() => { + await tx.rollback(); + }); + + it('should create a shipment and return a base64Binary label', async() => { + const mockPostResponses = [ + {data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')}, + {data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')} + ]; + + spyOn(axios, 'post').and.callFake(() => Promise.resolve(mockPostResponses.pop())); - await models.Ticket.updateAll({id: ticket1.id}, {shipped: Date.vnNew()}, options); const base64Binary = await models.MrwConfig.createShipment(expedition1.id, options); expect(base64Binary).toEqual(mockBase64Binary); diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 935c8fe78..e1584c24d 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -72,7 +72,7 @@ "The secret can't be blank": "La contraseña no puede estar en blanco", "We weren't able to send this SMS": "No hemos podido enviar el SMS", "This client can't be invoiced": "Este cliente no puede ser facturado", - "You must provide the correction information to generate a corrective invoice": "Debes informar la información de corrección para generar una factura rectificativa", + "You must provide the correction information to generate a corrective invoice": "Debes informar la información de corrección para generar una factura rectificativa", "This ticket can't be invoiced": "Este ticket no puede ser facturado", "You cannot add or modify services to an invoiced ticket": "No puedes añadir o modificar servicios a un ticket facturado", "This ticket can not be modified": "Este ticket no puede ser modificado", @@ -336,5 +336,6 @@ "Incorrect pin": "Pin incorrecto", "You already have the mailAlias": "Ya tienes este alias de correo", "You are not allowed to modify the alias": "No estás autorizado a modificar el alias", - "No tickets to invoice": "No hay tickets para facturar" -} + "No tickets to invoice": "No hay tickets para facturar", + "1) El servicio solicitado no existe o no es un servicio válido.": "1) El servicio solicitado no existe o no es un servicio válido." +} \ No newline at end of file From 46043eef9aad249344711fe21c6a590fcb972c84 Mon Sep 17 00:00:00 2001 From: pablone Date: Mon, 19 Feb 2024 12:54:24 +0100 Subject: [PATCH 11/11] refactor(description): refs #6403 modify description --- back/methods/mrw-config/cancelShipment.js | 2 +- back/methods/mrw-config/createShipment.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/back/methods/mrw-config/cancelShipment.js b/back/methods/mrw-config/cancelShipment.js index d1b7a6b72..218b6a96b 100644 --- a/back/methods/mrw-config/cancelShipment.js +++ b/back/methods/mrw-config/cancelShipment.js @@ -5,7 +5,7 @@ const {DOMParser} = require('xmldom'); module.exports = Self => { Self.remoteMethod('cancelShipment', { - description: 'Cancel a shipment by providing the expedition ID, interacting with MRW WS', + description: 'Cancel a shipment by providing the expedition ID, interacting with MRW WebService', accessType: 'WRITE', accepts: [{ arg: 'expeditionFk', diff --git a/back/methods/mrw-config/createShipment.js b/back/methods/mrw-config/createShipment.js index 2b22d44fb..f0f7d66a2 100644 --- a/back/methods/mrw-config/createShipment.js +++ b/back/methods/mrw-config/createShipment.js @@ -6,7 +6,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethod('createShipment', { - description: 'Create an expedition and return a base64Binary label', + description: 'Create an expedition and return a base64Binary label from de MRW WebService', accessType: 'WRITE', accepts: [{ arg: 'expeditionFk',