From 9ea427dbfece8d68a6350e1f9861f2222c31f631 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 18 Oct 2022 13:45:46 +0200 Subject: [PATCH] refactor: move front to back funcionality --- loopback/locale/es.json | 3 +- .../methods/expedition/moveExpeditions.js | 60 +++++++++++++++---- modules/ticket/front/expedition/index.js | 17 ++---- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 67370b343..bd9de41c2 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -235,5 +235,6 @@ "Dirección incorrecta": "Dirección incorrecta", "Modifiable user details only by an administrator": "Detalles de usuario modificables solo por un administrador", "Modifiable password only via recovery or by an administrator": "Contraseña modificable solo a través de la recuperación o por un administrador", - "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente" + "Not enough privileges to edit a client": "No tienes suficientes privilegios para editar un cliente", + "This route not exists": "Esta ruta no existe" } \ No newline at end of file diff --git a/modules/ticket/back/methods/expedition/moveExpeditions.js b/modules/ticket/back/methods/expedition/moveExpeditions.js index 338568fdb..d0f8aa893 100644 --- a/modules/ticket/back/methods/expedition/moveExpeditions.js +++ b/modules/ticket/back/methods/expedition/moveExpeditions.js @@ -1,19 +1,47 @@ +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethod('moveExpeditions', { + Self.remoteMethodCtx('moveExpeditions', { description: 'Move the selected expeditions to another ticket', accessType: 'WRITE', accepts: [{ + arg: 'clientId', + type: 'number', + description: `The client id`, + required: true + }, + { + arg: 'landed', + type: 'date', + description: `The landing date` + }, + { + arg: 'warehouseId', + type: 'number', + description: `The warehouse id`, + required: true + }, + { + arg: 'addressId', + type: 'number', + description: `The address id`, + required: true + }, + { + arg: 'agencyModeId', + type: 'any', + description: `The agencyMode id` + }, + { + arg: 'routeId', + type: 'any', + description: `The route id` + }, + { arg: 'expeditionIds', 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' + description: 'The expeditions ids to move' }], returns: { type: 'object', @@ -25,8 +53,9 @@ module.exports = Self => { } }); - Self.moveExpeditions = async(expeditionIds, ticketId, options) => { + Self.moveExpeditions = async(ctx, options) => { const models = Self.app.models; + const args = ctx.args; const myOptions = {}; let tx; @@ -39,18 +68,23 @@ module.exports = Self => { } try { + if (args.routeId) { + const route = await models.Route.findById(args.routeId, null, myOptions); + if (!route) throw new UserError('This route not exists'); + } + const ticket = await models.Ticket.new(ctx, myOptions); const promises = []; - for (let expeditionsId of expeditionIds) { + for (let expeditionsId of args.expeditionIds) { const expeditionToUpdate = await models.Expedition.findById(expeditionsId); - const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticketId, myOptions); + const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticket.id, myOptions); promises.push(expeditionUpdated); } - const updated = await Promise.all(promises); + await Promise.all(promises); if (tx) await tx.commit(); - return updated; + return ticket; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/ticket/front/expedition/index.js b/modules/ticket/front/expedition/index.js index 49ba3280d..7ffe2fe5e 100644 --- a/modules/ticket/front/expedition/index.js +++ b/modules/ticket/front/expedition/index.js @@ -44,22 +44,17 @@ class Controller extends Section { } createTicket(landed, routeFk) { - const ticketParams = { + const params = { clientId: this.ticket.clientFk, landed: landed, + warehouseId: this.ticket.warehouseFk, addressId: this.ticket.addressFk, agencyModeId: this.ticket.agencyModeFk, - warehouseId: this.ticket.warehouseFk + routeId: routeFk, + expeditionIds: this.checked }; - const query = `Tickets/new`; - this.$http.post(query, ticketParams).then(res => { - if (routeFk) this.$http.patch(`Tickets/${res.data.id}`, {routeFk: routeFk}); - const params = { - expeditionIds: this.checked, - ticketId: res.data.id - }; - const query = `Expeditions/moveExpeditions`; - this.$http.post(query, params); + const query = `Expeditions/moveExpeditions`; + this.$http.post(query, params).then(res => { this.vnApp.showSuccess(this.$t('Data saved!')); this.$state.go('ticket.card.summary', {id: res.data.id}); });