From f3e43686a3db0f7a1e43434fd8e3396f5436b657 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 24 Aug 2023 14:59:28 +0200 Subject: [PATCH 1/6] refs #5811 feat: al borrar expedicion borrar tmb de viaexpress --- .../viaexpress-config/deleteShipment.ejs | 11 +++++ .../viaexpress-config/deleteShipment.js | 45 +++++++++++++++++++ .../deleteShipmentRenderer.js | 44 ++++++++++++++++++ modules/ticket/back/models/expedition.json | 3 ++ 4 files changed, 103 insertions(+) create mode 100644 back/methods/viaexpress-config/deleteShipment.ejs create mode 100644 back/methods/viaexpress-config/deleteShipment.js create mode 100644 back/methods/viaexpress-config/deleteShipmentRenderer.js diff --git a/back/methods/viaexpress-config/deleteShipment.ejs b/back/methods/viaexpress-config/deleteShipment.ejs new file mode 100644 index 000000000..3be459a8d --- /dev/null +++ b/back/methods/viaexpress-config/deleteShipment.ejs @@ -0,0 +1,11 @@ + + + + + <%= viaexpressConfig.client %> + <%= viaexpressConfig.user %> + <%= viaexpressConfig.password %> + <%= externalId %> + + + diff --git a/back/methods/viaexpress-config/deleteShipment.js b/back/methods/viaexpress-config/deleteShipment.js new file mode 100644 index 000000000..40509899b --- /dev/null +++ b/back/methods/viaexpress-config/deleteShipment.js @@ -0,0 +1,45 @@ +const axios = require('axios'); +const {DOMParser} = require('xmldom'); + +module.exports = Self => { + Self.remoteMethod('deleteShipment', { + description: 'Create an expedition and return a label', + accessType: 'WRITE', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/deleteShipment`, + verb: 'POST' + } + }); + + Self.deleteShipment = async expeditionFk => { + const models = Self.app.models; + + const viaexpressConfig = await models.ViaexpressConfig.findOne({ + fields: ['url'] + }); + + const renderedXml = await models.ViaexpressConfig.deleteShipmentRenderer(expeditionFk); + const response = await axios.post(`${viaexpressConfig.url}ServicioVxClientes.asmx`, 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('DeleteEnvioResult')[0]; + const result = resultElement.textContent; + + return result; + }; +}; diff --git a/back/methods/viaexpress-config/deleteShipmentRenderer.js b/back/methods/viaexpress-config/deleteShipmentRenderer.js new file mode 100644 index 000000000..d9106a9ec --- /dev/null +++ b/back/methods/viaexpress-config/deleteShipmentRenderer.js @@ -0,0 +1,44 @@ +const fs = require('fs'); +const ejs = require('ejs'); + +module.exports = Self => { + Self.remoteMethod('deleteShipmentRenderer', { + description: 'Renders the data from an XML', + accessType: 'READ', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/deleteShipmentRenderer`, + verb: 'GET' + } + }); + + Self.deleteShipmentRenderer = async expeditionFk => { + const models = Self.app.models; + + const viaexpressConfig = await models.ViaexpressConfig.findOne({ + fields: ['client', 'user', 'password'] + }); + + const expedition = await models.Expedition.findOne({ + fields: ['id', 'externalId'], + where: {id: expeditionFk} + }); + + const data = { + viaexpressConfig, + externalId: expedition.externalId + }; + + const template = fs.readFileSync(__dirname + '/deleteShipment.ejs', 'utf-8'); + const renderedXml = ejs.render(template, data); + return renderedXml; + }; +}; diff --git a/modules/ticket/back/models/expedition.json b/modules/ticket/back/models/expedition.json index e32a3b23d..1ad083ce3 100644 --- a/modules/ticket/back/models/expedition.json +++ b/modules/ticket/back/models/expedition.json @@ -20,6 +20,9 @@ }, "counter": { "type": "number" + }, + "externalId": { + "type": "string" } }, "relations": { From 900dffbbba344b62662e7fa8ce388d0d9006a20e Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 23 Nov 2023 10:26:28 +0100 Subject: [PATCH 2/6] refs #5811 delete viaexpress expeditions included --- back/models/viaexpress-config.js | 2 + loopback/locale/en.json | 3 +- loopback/locale/es.json | 5 +- .../methods/expedition/deleteExpeditions.js | 66 +++++++++++-------- modules/ticket/back/models/expedition.json | 2 +- 5 files changed, 45 insertions(+), 33 deletions(-) diff --git a/back/models/viaexpress-config.js b/back/models/viaexpress-config.js index d0335b28b..429c0a0d8 100644 --- a/back/models/viaexpress-config.js +++ b/back/models/viaexpress-config.js @@ -1,4 +1,6 @@ module.exports = Self => { require('../methods/viaexpress-config/internationalExpedition')(Self); require('../methods/viaexpress-config/renderer')(Self); + require('../methods/viaexpress-config/deleteShipment')(Self); + require('../methods/viaexpress-config/deleteShipmentRenderer')(Self); }; diff --git a/loopback/locale/en.json b/loopback/locale/en.json index ed6aa474f..7d5b5ed47 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -199,5 +199,6 @@ "You can only add negative amounts in refund tickets": "You can only add negative amounts in refund tickets", "Try again": "Try again", "keepPrice": "keepPrice", - "Cannot past travels with entries": "Cannot past travels with entries" + "Cannot past travels with entries": "Cannot past travels with entries", + "It was not able to remove the next expeditions:": "It was not able to remove the next expeditions: {{expeditions}}" } \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 41acc36c3..01384efb4 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -328,5 +328,6 @@ "User disabled": "Usuario desactivado", "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" -} + "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}}" +} \ No newline at end of file diff --git a/modules/ticket/back/methods/expedition/deleteExpeditions.js b/modules/ticket/back/methods/expedition/deleteExpeditions.js index 2419d3a5e..72c48fd3a 100644 --- a/modules/ticket/back/methods/expedition/deleteExpeditions.js +++ b/modules/ticket/back/methods/expedition/deleteExpeditions.js @@ -1,6 +1,7 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethod('deleteExpeditions', { + Self.remoteMethodCtx('deleteExpeditions', { description: 'Delete the selected expeditions', accessType: 'WRITE', accepts: [{ @@ -9,44 +10,51 @@ module.exports = Self => { required: true, description: 'The expeditions ids to delete' }], - returns: { - type: ['object'], - root: true - }, http: { path: `/deleteExpeditions`, verb: 'POST' } }); - Self.deleteExpeditions = async(expeditionIds, options) => { + Self.deleteExpeditions = async(ctx, expeditionIds) => { const models = Self.app.models; - const myOptions = {}; - let tx; + const $t = ctx.req.__; + const notDeletedExpeditions = []; - if (typeof options == 'object') - Object.assign(myOptions, options); + for (let expeditionId of expeditionIds) { + const filter = { + fields: [], + where: { + id: expeditionId + }, + include: [ + { + relation: 'agencyMode', + scope: { + fields: ['code'], + } + } + ] + }; - if (!myOptions.transaction) { - tx = await Self.beginTransaction({}); - myOptions.transaction = tx; + const expedition = await models.Expedition.findOne(filter); + const {code} = expedition.agencyMode(); + + if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') { + const isDeleted = await models.ViaexpressConfig.deleteShipment(expeditionId); + + if (isDeleted === 'true') + await models.Expedition.destroyById(expeditionId); + + else if (!isDeleted) + notDeletedExpeditions.push(expeditionId); + } else + await models.Expedition.destroyById(expeditionId); } - - try { - const promises = []; - for (let expeditionId of expeditionIds) { - const deletedExpedition = models.Expedition.destroyById(expeditionId, myOptions); - promises.push(deletedExpedition); - } - - const deletedExpeditions = await Promise.all(promises); - - if (tx) await tx.commit(); - - return deletedExpeditions; - } catch (e) { - if (tx) await tx.rollback(); - throw e; + if (notDeletedExpeditions.length) { + throw new UserError( + $t(`It was not able to remove the next expeditions:`, {expeditions: notDeletedExpeditions.join()}) + ); } }; }; diff --git a/modules/ticket/back/models/expedition.json b/modules/ticket/back/models/expedition.json index 1ad083ce3..069c6e281 100644 --- a/modules/ticket/back/models/expedition.json +++ b/modules/ticket/back/models/expedition.json @@ -33,7 +33,7 @@ }, "agencyMode": { "type": "belongsTo", - "model": "agency-mode", + "model": "AgencyMode", "foreignKey": "agencyModeFk" }, "worker": { From 2e67ef4f5d40e86cfc6bfa251b2da278509e2a5f Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 23 Nov 2023 10:32:07 +0100 Subject: [PATCH 3/6] refs #5811 refactor deleteExpeditions --- modules/ticket/back/methods/expedition/deleteExpeditions.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/ticket/back/methods/expedition/deleteExpeditions.js b/modules/ticket/back/methods/expedition/deleteExpeditions.js index 72c48fd3a..a53c5d82c 100644 --- a/modules/ticket/back/methods/expedition/deleteExpeditions.js +++ b/modules/ticket/back/methods/expedition/deleteExpeditions.js @@ -43,11 +43,9 @@ module.exports = Self => { if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') { const isDeleted = await models.ViaexpressConfig.deleteShipment(expeditionId); - if (isDeleted === 'true') - await models.Expedition.destroyById(expeditionId); + if (isDeleted === 'true') await models.Expedition.destroyById(expeditionId); - else if (!isDeleted) - notDeletedExpeditions.push(expeditionId); + else notDeletedExpeditions.push(expeditionId); } else await models.Expedition.destroyById(expeditionId); } From db15285858f366732b256725756b57306e7437a5 Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 23 Nov 2023 11:21:56 +0100 Subject: [PATCH 4/6] refs #5811 test updated --- .../methods/expedition/deleteExpeditions.js | 20 ++++++++++++++----- .../specs/deleteExpeditions.spec.js | 13 ++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/modules/ticket/back/methods/expedition/deleteExpeditions.js b/modules/ticket/back/methods/expedition/deleteExpeditions.js index a53c5d82c..3c98c8017 100644 --- a/modules/ticket/back/methods/expedition/deleteExpeditions.js +++ b/modules/ticket/back/methods/expedition/deleteExpeditions.js @@ -13,6 +13,10 @@ module.exports = Self => { http: { path: `/deleteExpeditions`, verb: 'POST' + }, + returns: { + type: ['object'], + root: true } }); @@ -20,6 +24,7 @@ module.exports = Self => { const models = Self.app.models; const $t = ctx.req.__; const notDeletedExpeditions = []; + const deletedExpeditions = []; for (let expeditionId of expeditionIds) { const filter = { @@ -43,16 +48,21 @@ module.exports = Self => { if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') { const isDeleted = await models.ViaexpressConfig.deleteShipment(expeditionId); - if (isDeleted === 'true') await models.Expedition.destroyById(expeditionId); - - else notDeletedExpeditions.push(expeditionId); - } else - await models.Expedition.destroyById(expeditionId); + if (isDeleted === 'true') { + const deletedExpedition = await models.Expedition.destroyById(expeditionId); + deletedExpeditions.push(deletedExpedition); + } else notDeletedExpeditions.push(expeditionId); + } else { + const deletedExpedition = await models.Expedition.destroyById(expeditionId); + deletedExpeditions.push(deletedExpedition); + } } + if (notDeletedExpeditions.length) { throw new UserError( $t(`It was not able to remove the next expeditions:`, {expeditions: notDeletedExpeditions.join()}) ); } + return deletedExpeditions; }; }; diff --git a/modules/ticket/back/methods/expedition/specs/deleteExpeditions.spec.js b/modules/ticket/back/methods/expedition/specs/deleteExpeditions.spec.js index 61937989e..bf8bafe34 100644 --- a/modules/ticket/back/methods/expedition/specs/deleteExpeditions.spec.js +++ b/modules/ticket/back/methods/expedition/specs/deleteExpeditions.spec.js @@ -2,17 +2,16 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('ticket deleteExpeditions()', () => { + let ctx; beforeAll(async() => { - const activeCtx = { + ctx = { accessToken: {userId: 9}, - http: { - req: { - headers: {origin: 'http://localhost'} - } + req: { + headers: {origin: 'http://localhost'} } }; spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ - active: activeCtx + active: ctx }); }); @@ -23,7 +22,7 @@ describe('ticket deleteExpeditions()', () => { const options = {transaction: tx}; const expeditionIds = [12, 13]; - const result = await models.Expedition.deleteExpeditions(expeditionIds, options); + const result = await models.Expedition.deleteExpeditions(ctx, expeditionIds, options); expect(result.length).toEqual(2); From eeef279483a5adc30d3ad3e31197d1f0e0141c5f Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 23 Nov 2023 13:34:29 +0100 Subject: [PATCH 5/6] refs #5811 replace decription --- back/methods/viaexpress-config/deleteShipment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/viaexpress-config/deleteShipment.js b/back/methods/viaexpress-config/deleteShipment.js index 40509899b..babd3eeee 100644 --- a/back/methods/viaexpress-config/deleteShipment.js +++ b/back/methods/viaexpress-config/deleteShipment.js @@ -3,7 +3,7 @@ const {DOMParser} = require('xmldom'); module.exports = Self => { Self.remoteMethod('deleteShipment', { - description: 'Create an expedition and return a label', + description: 'Delete a shipment by providing the expedition ID, interacting with Viaexpress API', accessType: 'WRITE', accepts: [{ arg: 'expeditionFk', From 8ac3887d8025840b3b90c4cbaae3af4e3d2171c4 Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 24 Nov 2023 10:53:38 +0100 Subject: [PATCH 6/6] refs #5811 rename methods --- .../{deleteShipment.ejs => deleteExpedition.ejs} | 0 .../{deleteShipment.js => deleteExpedition.js} | 8 ++++---- ...eteShipmentRenderer.js => deleteExpeditionRenderer.js} | 8 ++++---- back/models/viaexpress-config.js | 4 ++-- .../ticket/back/methods/expedition/deleteExpeditions.js | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) rename back/methods/viaexpress-config/{deleteShipment.ejs => deleteExpedition.ejs} (100%) rename back/methods/viaexpress-config/{deleteShipment.js => deleteExpedition.js} (88%) rename back/methods/viaexpress-config/{deleteShipmentRenderer.js => deleteExpeditionRenderer.js} (79%) diff --git a/back/methods/viaexpress-config/deleteShipment.ejs b/back/methods/viaexpress-config/deleteExpedition.ejs similarity index 100% rename from back/methods/viaexpress-config/deleteShipment.ejs rename to back/methods/viaexpress-config/deleteExpedition.ejs diff --git a/back/methods/viaexpress-config/deleteShipment.js b/back/methods/viaexpress-config/deleteExpedition.js similarity index 88% rename from back/methods/viaexpress-config/deleteShipment.js rename to back/methods/viaexpress-config/deleteExpedition.js index babd3eeee..189745554 100644 --- a/back/methods/viaexpress-config/deleteShipment.js +++ b/back/methods/viaexpress-config/deleteExpedition.js @@ -2,7 +2,7 @@ const axios = require('axios'); const {DOMParser} = require('xmldom'); module.exports = Self => { - Self.remoteMethod('deleteShipment', { + Self.remoteMethod('deleteExpedition', { description: 'Delete a shipment by providing the expedition ID, interacting with Viaexpress API', accessType: 'WRITE', accepts: [{ @@ -15,19 +15,19 @@ module.exports = Self => { root: true }, http: { - path: `/deleteShipment`, + path: `/deleteExpedition`, verb: 'POST' } }); - Self.deleteShipment = async expeditionFk => { + Self.deleteExpedition = async expeditionFk => { const models = Self.app.models; const viaexpressConfig = await models.ViaexpressConfig.findOne({ fields: ['url'] }); - const renderedXml = await models.ViaexpressConfig.deleteShipmentRenderer(expeditionFk); + const renderedXml = await models.ViaexpressConfig.deleteExpeditionRenderer(expeditionFk); const response = await axios.post(`${viaexpressConfig.url}ServicioVxClientes.asmx`, renderedXml, { headers: { 'Content-Type': 'application/soap+xml; charset=utf-8' diff --git a/back/methods/viaexpress-config/deleteShipmentRenderer.js b/back/methods/viaexpress-config/deleteExpeditionRenderer.js similarity index 79% rename from back/methods/viaexpress-config/deleteShipmentRenderer.js rename to back/methods/viaexpress-config/deleteExpeditionRenderer.js index d9106a9ec..645eaddd1 100644 --- a/back/methods/viaexpress-config/deleteShipmentRenderer.js +++ b/back/methods/viaexpress-config/deleteExpeditionRenderer.js @@ -2,7 +2,7 @@ const fs = require('fs'); const ejs = require('ejs'); module.exports = Self => { - Self.remoteMethod('deleteShipmentRenderer', { + Self.remoteMethod('deleteExpeditionRenderer', { description: 'Renders the data from an XML', accessType: 'READ', accepts: [{ @@ -15,12 +15,12 @@ module.exports = Self => { root: true }, http: { - path: `/deleteShipmentRenderer`, + path: `/deleteExpeditionRenderer`, verb: 'GET' } }); - Self.deleteShipmentRenderer = async expeditionFk => { + Self.deleteExpeditionRenderer = async expeditionFk => { const models = Self.app.models; const viaexpressConfig = await models.ViaexpressConfig.findOne({ @@ -37,7 +37,7 @@ module.exports = Self => { externalId: expedition.externalId }; - const template = fs.readFileSync(__dirname + '/deleteShipment.ejs', 'utf-8'); + const template = fs.readFileSync(__dirname + '/deleteExpedition.ejs', 'utf-8'); const renderedXml = ejs.render(template, data); return renderedXml; }; diff --git a/back/models/viaexpress-config.js b/back/models/viaexpress-config.js index 429c0a0d8..d00c99e82 100644 --- a/back/models/viaexpress-config.js +++ b/back/models/viaexpress-config.js @@ -1,6 +1,6 @@ module.exports = Self => { require('../methods/viaexpress-config/internationalExpedition')(Self); require('../methods/viaexpress-config/renderer')(Self); - require('../methods/viaexpress-config/deleteShipment')(Self); - require('../methods/viaexpress-config/deleteShipmentRenderer')(Self); + require('../methods/viaexpress-config/deleteExpedition')(Self); + require('../methods/viaexpress-config/deleteExpeditionRenderer')(Self); }; diff --git a/modules/ticket/back/methods/expedition/deleteExpeditions.js b/modules/ticket/back/methods/expedition/deleteExpeditions.js index 3c98c8017..55ca474d7 100644 --- a/modules/ticket/back/methods/expedition/deleteExpeditions.js +++ b/modules/ticket/back/methods/expedition/deleteExpeditions.js @@ -46,7 +46,7 @@ module.exports = Self => { const {code} = expedition.agencyMode(); if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') { - const isDeleted = await models.ViaexpressConfig.deleteShipment(expeditionId); + const isDeleted = await models.ViaexpressConfig.deleteExpedition(expeditionId); if (isDeleted === 'true') { const deletedExpedition = await models.Expedition.destroyById(expeditionId);