diff --git a/back/methods/viaexpress-config/deleteExpedition.ejs b/back/methods/viaexpress-config/deleteExpedition.ejs new file mode 100644 index 000000000..3be459a8d --- /dev/null +++ b/back/methods/viaexpress-config/deleteExpedition.ejs @@ -0,0 +1,11 @@ + + + + + <%= viaexpressConfig.client %> + <%= viaexpressConfig.user %> + <%= viaexpressConfig.password %> + <%= externalId %> + + + diff --git a/back/methods/viaexpress-config/deleteExpedition.js b/back/methods/viaexpress-config/deleteExpedition.js new file mode 100644 index 000000000..189745554 --- /dev/null +++ b/back/methods/viaexpress-config/deleteExpedition.js @@ -0,0 +1,45 @@ +const axios = require('axios'); +const {DOMParser} = require('xmldom'); + +module.exports = Self => { + Self.remoteMethod('deleteExpedition', { + description: 'Delete a shipment by providing the expedition ID, interacting with Viaexpress API', + accessType: 'WRITE', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/deleteExpedition`, + verb: 'POST' + } + }); + + Self.deleteExpedition = async expeditionFk => { + const models = Self.app.models; + + const viaexpressConfig = await models.ViaexpressConfig.findOne({ + fields: ['url'] + }); + + 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' + } + }); + + 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/deleteExpeditionRenderer.js b/back/methods/viaexpress-config/deleteExpeditionRenderer.js new file mode 100644 index 000000000..645eaddd1 --- /dev/null +++ b/back/methods/viaexpress-config/deleteExpeditionRenderer.js @@ -0,0 +1,44 @@ +const fs = require('fs'); +const ejs = require('ejs'); + +module.exports = Self => { + Self.remoteMethod('deleteExpeditionRenderer', { + description: 'Renders the data from an XML', + accessType: 'READ', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/deleteExpeditionRenderer`, + verb: 'GET' + } + }); + + Self.deleteExpeditionRenderer = 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 + '/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 d0335b28b..d00c99e82 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/deleteExpedition')(Self); + require('../methods/viaexpress-config/deleteExpeditionRenderer')(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..55ca474d7 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,59 @@ module.exports = Self => { required: true, description: 'The expeditions ids to delete' }], - returns: { - type: ['object'], - root: true - }, http: { path: `/deleteExpeditions`, verb: 'POST' + }, + returns: { + type: ['object'], + root: true } }); - 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 = []; + const deletedExpeditions = []; - 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(); - try { - const promises = []; - for (let expeditionId of expeditionIds) { - const deletedExpedition = models.Expedition.destroyById(expeditionId, myOptions); - promises.push(deletedExpedition); + if (code && code.toLowerCase().substring(0, 10) == 'viaexpress') { + const isDeleted = await models.ViaexpressConfig.deleteExpedition(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); } - - 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()}) + ); + } + 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); diff --git a/modules/ticket/back/models/expedition.json b/modules/ticket/back/models/expedition.json index e32a3b23d..069c6e281 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": { @@ -30,7 +33,7 @@ }, "agencyMode": { "type": "belongsTo", - "model": "agency-mode", + "model": "AgencyMode", "foreignKey": "agencyModeFk" }, "worker": {