From d37d314304e7eea559ac3ff8bfdc21da2e05453c Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Tue, 5 Mar 2019 12:01:53 +0100 Subject: [PATCH] #1202 refactor del cambio de estado de un ticket --- .../methods/ticket-tracking/changeState.js | 17 +++++---- modules/ticket/front/summary/index.js | 35 +++++++++---------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/modules/ticket/back/methods/ticket-tracking/changeState.js b/modules/ticket/back/methods/ticket-tracking/changeState.js index e93eb25f3..f70ae93a5 100644 --- a/modules/ticket/back/methods/ticket-tracking/changeState.js +++ b/modules/ticket/back/methods/ticket-tracking/changeState.js @@ -23,23 +23,28 @@ module.exports = Self => { } }); - Self.changeState = async(ctx, data) => { + Self.changeState = async(ctx, params) => { let userId = ctx.req.accessToken.userId; let $ = Self.app.models; - if (!data.stateFk) + if (!params.stateFk && !params.code) throw new UserError('State cannot be blank'); + if (params.code) { + let state = await $.State.findOne({where: {code: params.code}, fields: ['id']}); + params.stateFk = state.id; + } + let isProduction = await $.Account.hasRole(userId, 'production'); let isSalesPerson = await $.Account.hasRole(userId, 'salesPerson'); let ticket = await $.TicketState.findById( - data.ticketFk, + params.ticketFk, {fields: ['stateFk']} ); let oldState = await $.State.findById(ticket.stateFk); - let newState = await $.State.findById(data.stateFk); + let newState = await $.State.findById(params.stateFk); let isAllowed = isProduction || isSalesPerson && oldState.isEditable() @@ -50,9 +55,9 @@ module.exports = Self => { if (newState.code != 'PICKER_DESIGNED') { let worker = await $.Worker.findOne({where: {userFk: userId}}); - data.workerFk = worker.id; + params.workerFk = worker.id; } - return await $.TicketTracking.create(data); + return await $.TicketTracking.create(params); }; }; diff --git a/modules/ticket/front/summary/index.js b/modules/ticket/front/summary/index.js index 3c99acaa4..52ffa40cf 100644 --- a/modules/ticket/front/summary/index.js +++ b/modules/ticket/front/summary/index.js @@ -17,12 +17,7 @@ class Controller { set ticket(value) { this._ticket = value; - if (!value) return; - - this.$http.get(`/ticket/api/Tickets/${this.ticket.id}/summary`).then(res => { - if (res && res.data) - this.summary = res.data; - }); + if (value) this.getSummary(); } get formattedAddress() { @@ -34,6 +29,13 @@ class Controller { return `${address.street} - ${address.city} ${province}`; } + getSummary() { + this.$http.get(`/ticket/api/Tickets/${this.ticket.id}/summary`).then(res => { + if (res && res.data) + this.summary = res.data; + }); + } + showDescriptor(event, itemFk) { this.quicklinks = { btnThree: { @@ -64,25 +66,22 @@ class Controller { } setOkState() { - let filter = {where: {code: 'OK'}, fields: ['id']}; - let json = encodeURIComponent(JSON.stringify(filter)); - this.$http.get(`/ticket/api/States?filter=${json}`).then(res => { - this.changeTicketState(res.data[0].id); - }); - } - - changeTicketState(value) { - let params; + let params = {}; if (this.$state.params.id) - params = {ticketFk: this.$state.params.id, stateFk: value}; + params = {ticketFk: this.$state.params.id}; if (!this.$state.params.id) - params = {ticketFk: this.ticket.id, stateFk: value}; + params = {ticketFk: this.ticket.id}; + + params.code = 'OK'; this.$http.post(`/ticket/api/TicketTrackings/changeState`, params).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); - if (this.card) this.card.reload(); + if (this.card) + this.card.reload(); + else + this.getSummary(); }); } }