From 2fdd2750852fcafcda70b8437b6a89e4f0cead40 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 25 Feb 2020 11:43:50 +0100 Subject: [PATCH 1/4] log in ticketUpdate --- modules/ticket/back/methods/ticket/componentUpdate.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 2cba62540..4d05e28d6 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -1,4 +1,5 @@ const UserError = require('vn-loopback/util/user-error'); +const diff = require('object-diff'); module.exports = Self => { Self.remoteMethodCtx('componentUpdate', { @@ -87,9 +88,13 @@ module.exports = Self => { if (!zoneShipped || zoneShipped.zoneFk != zoneId) throw new UserError(`You don't have privileges to change the zone`); } - + const originalTicket = await models.Ticket.findById(id); + const properties = Object.assign({}, ctx.args); + delete properties.ctx; + delete properties.option; // Force unroute const hasToBeUnrouted = true; + console.log('args', originalTicket); let query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; let res = await Self.rawSql(query, [ id, From 85db094f1d9578d99a5745312f040cf26fa16c7f Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 25 Feb 2020 12:36:33 +0100 Subject: [PATCH 2/4] componentUpdate --- modules/ticket/back/methods/ticket/componentUpdate.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 4d05e28d6..3447f52bf 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -88,13 +88,17 @@ module.exports = Self => { if (!zoneShipped || zoneShipped.zoneFk != zoneId) throw new UserError(`You don't have privileges to change the zone`); } - const originalTicket = await models.Ticket.findById(id); + const originalTicket = await models.Ticket.findById(id, {fields: + ['id', 'clientFk', 'agencyModeFk', 'addressFk', 'zoneFk', + 'warehouseFk', 'companyFk', 'shipped', 'landed', 'isDeleted'] + }); const properties = Object.assign({}, ctx.args); delete properties.ctx; delete properties.option; // Force unroute const hasToBeUnrouted = true; - console.log('args', originalTicket); + console.log('originalTicket', originalTicket); + console.log('properties', properties); let query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; let res = await Self.rawSql(query, [ id, From fdd1c7348543f0c51d8df6c5293e9a041fd7291c Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Fri, 13 Mar 2020 12:26:15 +0100 Subject: [PATCH 3/4] refactor componentUpdate --- .../back/methods/ticket/componentUpdate.js | 42 ++++++++++--------- .../ticket/specs/componentUpdate.spec.js | 7 +++- .../ticket/front/basic-data/step-two/index.js | 12 +++--- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 3447f52bf..00a6aae4b 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -12,32 +12,32 @@ module.exports = Self => { description: 'The ticket id', http: {source: 'path'} }, { - arg: 'clientId', + arg: 'clientFk', type: 'Number', description: 'The client id', required: true }, { - arg: 'agencyModeId', + arg: 'agencyModeFk', type: 'Number', description: 'The agencyMode id', required: true }, { - arg: 'addressId', + arg: 'addressFk', type: 'Number', description: 'The address id', required: true }, { - arg: 'zoneId', + arg: 'zoneFk', type: 'Number', description: 'The zone id', required: true }, { - arg: 'warehouseId', + arg: 'warehouseFk', type: 'Number', description: 'The warehouse id', required: true }, { - arg: 'companyId', + arg: 'companyFk', type: 'Number', description: 'The company id', required: true @@ -72,8 +72,8 @@ module.exports = Self => { } }); - Self.componentUpdate = async(ctx, id, clientId, agencyModeId, addressId, zoneId, warehouseId, - companyId, shipped, landed, isDeleted, option) => { + Self.componentUpdate = async(ctx, id, clientFk, agencyModeFk, addressFk, zoneFk, warehouseFk, + companyFk, shipped, landed, isDeleted, option) => { const userId = ctx.req.accessToken.userId; const models = Self.app.models; const isEditable = await models.Ticket.isEditable(ctx, id); @@ -83,31 +83,33 @@ module.exports = Self => { const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss'); if (!isProductionBoss) { - const zoneShipped = await models.Agency.getShipped(landed, addressId, agencyModeId, warehouseId); + const zoneShipped = await models.Agency.getShipped(landed, addressFk, agencyModeFk, warehouseFk); - if (!zoneShipped || zoneShipped.zoneFk != zoneId) + if (!zoneShipped || zoneShipped.zoneFk != zoneFk) throw new UserError(`You don't have privileges to change the zone`); } const originalTicket = await models.Ticket.findById(id, {fields: ['id', 'clientFk', 'agencyModeFk', 'addressFk', 'zoneFk', 'warehouseFk', 'companyFk', 'shipped', 'landed', 'isDeleted'] }); - const properties = Object.assign({}, ctx.args); - delete properties.ctx; - delete properties.option; + const updatedTicket = Object.assign({}, ctx.args); + delete updatedTicket.ctx; + delete updatedTicket.option; // Force unroute const hasToBeUnrouted = true; + console.log('updatedTicket', updatedTicket); console.log('originalTicket', originalTicket); - console.log('properties', properties); + const propertiesChange = diff(originalTicket, updatedTicket); + console.log('propertiesChange', propertiesChange); let query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; let res = await Self.rawSql(query, [ id, - clientId, - agencyModeId, - addressId, - zoneId, - warehouseId, - companyId, + clientFk, + agencyModeFk, + addressFk, + zoneFk, + warehouseFk, + companyFk, shipped, landed, isDeleted, diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index 04570390d..cb36a3b2d 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -1,6 +1,6 @@ const app = require('vn-loopback/server/server'); -describe('ticket componentUpdate()', () => { +fdescribe('ticket componentUpdate()', () => { const ticketId = 11; const today = new Date(); const tomorrow = new Date(); @@ -39,7 +39,10 @@ describe('ticket componentUpdate()', () => { const landed = tomorrow; const option = 1; - let ctx = {req: {accessToken: {userId: 101}}}; + let ctx = { + args: {ticketId: '', + } + req: {accessToken: {userId: 101}}}; await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, zoneId, warehouseId, companyId, shipped, landed, isDeleted, option); diff --git a/modules/ticket/front/basic-data/step-two/index.js b/modules/ticket/front/basic-data/step-two/index.js index ec8d94f9a..f766f5726 100644 --- a/modules/ticket/front/basic-data/step-two/index.js +++ b/modules/ticket/front/basic-data/step-two/index.js @@ -75,12 +75,12 @@ class Controller { let query = `tickets/${this.ticket.id}/componentUpdate`; let params = { - clientId: this.ticket.clientFk, - agencyModeId: this.ticket.agencyModeFk, - addressId: this.ticket.addressFk, - zoneId: this.ticket.zoneFk, - warehouseId: this.ticket.warehouseFk, - companyId: this.ticket.companyFk, + clientFk: this.ticket.clientFk, + agencyModeFk: this.ticket.agencyModeFk, + addressFk: this.ticket.addressFk, + zoneFk: this.ticket.zoneFk, + warehouseFk: this.ticket.warehouseFk, + companyFk: this.ticket.companyFk, shipped: this.ticket.shipped, landed: this.ticket.landed, isDeleted: this.ticket.isDeleted, From f6ca9eee0181a01a8b57ce4f6ed4fb0f4a4dac03 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Mon, 16 Mar 2020 09:13:44 +0100 Subject: [PATCH 4/4] ticketlog in componentUpdate --- .../back/methods/ticket/componentUpdate.js | 16 +++++++++++++--- .../methods/ticket/specs/componentUpdate.spec.js | 12 ++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 00a6aae4b..15cfa0597 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -97,10 +97,18 @@ module.exports = Self => { delete updatedTicket.option; // Force unroute const hasToBeUnrouted = true; - console.log('updatedTicket', updatedTicket); - console.log('originalTicket', originalTicket); const propertiesChange = diff(originalTicket, updatedTicket); - console.log('propertiesChange', propertiesChange); + + let logRecord = { + originFk: id, + userFk: userId, + action: 'update', + changedModel: 'Ticket', + changedModelId: id, + oldInstance: originalTicket, + newInstance: propertiesChange + }; + let query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; let res = await Self.rawSql(query, [ id, @@ -116,6 +124,8 @@ module.exports = Self => { hasToBeUnrouted, option ]); + + await models.TicketLog.create(logRecord); return res; }; }; diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index cb36a3b2d..8954e6d47 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -1,6 +1,6 @@ const app = require('vn-loopback/server/server'); -fdescribe('ticket componentUpdate()', () => { +describe('ticket componentUpdate()', () => { const ticketId = 11; const today = new Date(); const tomorrow = new Date(); @@ -40,8 +40,8 @@ fdescribe('ticket componentUpdate()', () => { const option = 1; let ctx = { - args: {ticketId: '', - } + args: {clientFk: 102, + agencyModeFk: 8}, req: {accessToken: {userId: 101}}}; await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, @@ -69,7 +69,11 @@ fdescribe('ticket componentUpdate()', () => { const landed = tomorrow; const option = 1; - let ctx = {req: {accessToken: {userId: 101}}}; + let ctx = { + args: {clientFk: 102, + agencyModeFk: 7}, + req: {accessToken: {userId: 101}}}; + await app.models.Ticket.componentUpdate(ctx, ticketId, clientId, agencyModeId, addressId, zoneId, warehouseId, companyId, shipped, landed, isDeleted, option);