From deb8a00dfb2edbd5d3a2a2e84caec89679cce5ad Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 7 Oct 2022 10:47:54 +0200 Subject: [PATCH] feat: add backRoutes --- .../methods/expedition/deleteExpeditions.js | 48 +++++++++++++++ .../methods/expedition/moveExpeditions.js | 59 +++++++++++++++++++ modules/ticket/back/models/expedition.js | 2 + modules/ticket/front/expedition/index.html | 4 +- modules/ticket/front/expedition/index.js | 35 +++++------ modules/ticket/front/expedition/locale/es.yml | 5 +- 6 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 modules/ticket/back/methods/expedition/deleteExpeditions.js create mode 100644 modules/ticket/back/methods/expedition/moveExpeditions.js diff --git a/modules/ticket/back/methods/expedition/deleteExpeditions.js b/modules/ticket/back/methods/expedition/deleteExpeditions.js new file mode 100644 index 0000000000..b902b9f0c5 --- /dev/null +++ b/modules/ticket/back/methods/expedition/deleteExpeditions.js @@ -0,0 +1,48 @@ + +module.exports = Self => { + Self.remoteMethod('deleteExpeditions', { + description: 'Delete the selected expeditions', + accessType: 'WRITE', + accepts: [{ + arg: 'expeditionsIds', + type: ['number'], + required: true, + description: 'The expeditions ids to delete' + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/deleteExpeditions`, + verb: 'POST' + } + }); + + Self.deleteExpeditions = async(expeditionsIds, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const deletedExpeditions = await models.Expedition.destroyAll({ + id: {inq: expeditionsIds} + }, myOptions); + + if (tx) await tx.commit(); + + return deletedExpeditions; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/methods/expedition/moveExpeditions.js b/modules/ticket/back/methods/expedition/moveExpeditions.js new file mode 100644 index 0000000000..2c521dda7c --- /dev/null +++ b/modules/ticket/back/methods/expedition/moveExpeditions.js @@ -0,0 +1,59 @@ + +module.exports = Self => { + Self.remoteMethod('moveExpeditions', { + description: 'Move the selected expeditions to another ticket', + accessType: 'WRITE', + accepts: [{ + arg: 'expeditionsIds', + type: ['number'], + required: true, + description: 'The expeditions ids to nove' + }, + { + arg: 'ticketId', + type: 'number', + required: true, + description: 'the ticket id to which the expeditions are added' + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/moveExpeditions`, + verb: 'POST' + } + }); + + Self.moveExpeditions = async(expeditionsIds, ticketId, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const promises = []; + for (let expeditionsId of expeditionsIds) { + const expeditionToUpdate = await models.Expedition.findById(expeditionsId); + const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticketId, myOptions); + promises.push(expeditionUpdated); + } + + const updated = await Promise.all(promises); + + if (tx) await tx.commit(); + + return updated; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/models/expedition.js b/modules/ticket/back/models/expedition.js index 9d65643737..46cde68902 100644 --- a/modules/ticket/back/models/expedition.js +++ b/modules/ticket/back/models/expedition.js @@ -1,3 +1,5 @@ module.exports = function(Self) { require('../methods/expedition/filter')(Self); + require('../methods/expedition/deleteExpeditions')(Self); + require('../methods/expedition/moveExpeditions')(Self); }; diff --git a/modules/ticket/front/expedition/index.html b/modules/ticket/front/expedition/index.html index caef67f59e..35f56872c2 100644 --- a/modules/ticket/front/expedition/index.html +++ b/modules/ticket/front/expedition/index.html @@ -14,10 +14,10 @@ + disabled="!$ctrl.totalChecked"> diff --git a/modules/ticket/front/expedition/index.js b/modules/ticket/front/expedition/index.js index 4f7b34e7cb..a81439d644 100644 --- a/modules/ticket/front/expedition/index.js +++ b/modules/ticket/front/expedition/index.js @@ -17,7 +17,7 @@ class Controller extends Section { const checkedRows = []; for (let row of rows) { if (row.checked) - checkedRows.push(row); + checkedRows.push(row.id); } return checkedRows; @@ -27,18 +27,14 @@ class Controller extends Section { return this.checked.length; } - async onRemove() { - const params = []; - for (let expedition of this.checked) - params.push(expedition.id); - - for (let id of params) { - await this.$http.delete(`Expeditions/${id}`) - .then(() => { - this.vnApp.showSuccess(this.$t('Expedition removed')); - this.$state.reload(); - }); - } + onRemove() { + const params = {expeditionsIds: this.checked}; + const query = `Expeditions/deleteExpeditions`; + this.$http.post(query, params) + .then(() => { + this.vnApp.showSuccess(this.$t('Expedition removed')); + this.$state.reload(); + }); } createTicket(routeFk) { @@ -54,13 +50,12 @@ class Controller extends Section { this.$http.post(query, ticketParams).then(res => { if (routeFk) this.$http.patch(`Tickets/${res.data.id}`, {routeFk: routeFk}); - const params = []; - for (let expedition of this.checked) - params.push(expedition.id); - const expeditionParams = {ticketFk: res.data.id}; - for (let id of params) - this.$http.patch(`Expeditions/${id}`, expeditionParams); - + const params = { + expeditionsIds: this.checked, + ticketId: res.data.id + }; + const query = `Expeditions/moveExpeditions`; + this.$http.post(query, params); this.vnApp.showSuccess(this.$t('Data saved!')); this.$state.go('ticket.card.summary', {id: res.data.id}); }); diff --git a/modules/ticket/front/expedition/locale/es.yml b/modules/ticket/front/expedition/locale/es.yml index 9c7872fd70..bc3ec33455 100644 --- a/modules/ticket/front/expedition/locale/es.yml +++ b/modules/ticket/front/expedition/locale/es.yml @@ -1,2 +1,5 @@ Status log: Hitorial de estados -Expedition removed: Expedición eliminada \ No newline at end of file +Expedition removed: Expedición eliminada +Move: Mover +New ticket without route: Nuevo ticket sin ruta +New ticket with route: Nuevo ticket con ruta \ No newline at end of file