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": {