From b44942c7d72bf08627a5d25c123938d42f070fe5 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Tue, 16 Jun 2020 12:36:34 +0200 Subject: [PATCH 1/4] open delivery notes on ticket index --- modules/ticket/front/index/index.html | 7 +++++++ modules/ticket/front/index/index.js | 18 ++++++++++++++++++ modules/ticket/front/index/locale/es.yml | 1 + 3 files changed, 26 insertions(+) diff --git a/modules/ticket/front/index/index.html b/modules/ticket/front/index/index.html index f75e9d9f7..4d64f1391 100644 --- a/modules/ticket/front/index/index.html +++ b/modules/ticket/front/index/index.html @@ -120,6 +120,13 @@
+ + Date: Wed, 17 Jun 2020 15:38:37 +0200 Subject: [PATCH 2/4] setDelivered and open delivery notes and tests --- .../methods/ticket-tracking/changeState.js | 2 +- .../methods/ticket-tracking/setDelivered.js | 44 +++++++++++++++++++ .../specs/setDelivered.spec.js | 40 +++++++++++++++++ modules/ticket/back/models/ticket-tracking.js | 1 + modules/ticket/front/index/index.html | 4 +- modules/ticket/front/index/index.js | 9 +++- modules/ticket/front/index/index.spec.js | 19 +++++++- modules/ticket/front/index/locale/es.yml | 2 +- 8 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 modules/ticket/back/methods/ticket-tracking/setDelivered.js create mode 100644 modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js diff --git a/modules/ticket/back/methods/ticket-tracking/changeState.js b/modules/ticket/back/methods/ticket-tracking/changeState.js index 065692137..fae9af218 100644 --- a/modules/ticket/back/methods/ticket-tracking/changeState.js +++ b/modules/ticket/back/methods/ticket-tracking/changeState.js @@ -53,6 +53,6 @@ module.exports = Self => { if (!isAllowed) throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED'); - return await models.TicketTracking.create(params); + return models.TicketTracking.create(params); }; }; diff --git a/modules/ticket/back/methods/ticket-tracking/setDelivered.js b/modules/ticket/back/methods/ticket-tracking/setDelivered.js new file mode 100644 index 000000000..62e5a7717 --- /dev/null +++ b/modules/ticket/back/methods/ticket-tracking/setDelivered.js @@ -0,0 +1,44 @@ +module.exports = Self => { + Self.remoteMethodCtx('setDelivered', { + description: 'Changes the state of the received ticket ids to delivered', + accessType: 'WRITE', + accepts: [ + { + arg: 'ticketIds', + description: 'the array of ticket ids to set as delivered', + type: [], + required: true, + http: {source: 'body'} + } + ], + returns: { + type: 'string', + root: true + }, + http: { + path: `/setDelivered`, + verb: 'POST' + } + }); + + Self.setDelivered = async(ctx, ticketIds) => { + let userId = ctx.req.accessToken.userId; + let models = Self.app.models; + + let state = await models.State.findOne({where: {code: 'delivered'}, fields: ['id']}); + + let worker = await models.Worker.findOne({where: {userFk: userId}}); + + let promises = []; + for (let i = 0; i < ticketIds.length; i++) { + let promise = models.TicketTracking.changeState(ctx, { + stateFk: state.id, + workerFk: worker.id, + ticketFk: ticketIds[i] + }); + promises.push(promise); + } + + return Promise.all(promises); + }; +}; diff --git a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js new file mode 100644 index 000000000..8a5963474 --- /dev/null +++ b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js @@ -0,0 +1,40 @@ +const app = require('vn-loopback/server/server'); + +describe('ticket setDelivered()', () => { + let ticketOne; + let ticketTwo; + + beforeAll(async done => { + let originalTicketOne = await app.models.Ticket.findById(8); + let originalTicketTwo = await app.models.Ticket.findById(10); + + originalTicketOne.id = null; + originalTicketTwo.id = null; + ticketOne = await app.models.Ticket.create(originalTicketOne); + ticketTwo = await app.models.Ticket.create(originalTicketTwo); + + done(); + }); + + afterAll(async done => { + await app.models.Ticket.destroyById(ticketOne.id); + await app.models.Ticket.destroyById(ticketTwo.id); + + done(); + }); + + it('should return an array of the tickets states changed to delivered', async() => { + let ctx = {req: {accessToken: {userId: 49}}}; + let delivered = await app.models.State.findOne({where: {code: 'delivered'}, fields: ['id']}); + + let params = [ticketOne.id, ticketTwo.id]; + let res = await app.models.TicketTracking.setDelivered(ctx, params); + + expect(res[0].ticketFk).toEqual(ticketOne.id); + expect(res[0].stateFk).toEqual(delivered.id); + + expect(res[1].ticketFk).toEqual(ticketTwo.id); + expect(res[1].stateFk).toEqual(delivered.id); + }); +}); + diff --git a/modules/ticket/back/models/ticket-tracking.js b/modules/ticket/back/models/ticket-tracking.js index 2b2a93536..2e6d3403e 100644 --- a/modules/ticket/back/models/ticket-tracking.js +++ b/modules/ticket/back/models/ticket-tracking.js @@ -1,5 +1,6 @@ module.exports = function(Self) { require('../methods/ticket-tracking/changeState')(Self); + require('../methods/ticket-tracking/setDelivered')(Self); Self.validatesPresenceOf('stateFk', {message: 'State cannot be blank'}); Self.validatesPresenceOf('workerFk', {message: 'Worker cannot be blank'}); diff --git a/modules/ticket/front/index/index.html b/modules/ticket/front/index/index.html index 4d64f1391..47695f38c 100644 --- a/modules/ticket/front/index/index.html +++ b/modules/ticket/front/index/index.html @@ -123,8 +123,8 @@ { + this.openDeliveryNotes(ids); + this.$.model.refresh(); + }); + } + + openDeliveryNotes(ids) { for (let id of ids) { const params = { authorization: this.vnToken.token, diff --git a/modules/ticket/front/index/index.spec.js b/modules/ticket/front/index/index.spec.js index de619ef26..43b5189bf 100644 --- a/modules/ticket/front/index/index.spec.js +++ b/modules/ticket/front/index/index.spec.js @@ -1,6 +1,7 @@ import './index.js'; describe('Component vnTicketIndex', () => { let controller; + let $httpBackend; let $window; let tickets = [{ id: 1, @@ -21,7 +22,8 @@ describe('Component vnTicketIndex', () => { beforeEach(ngModule('ticket')); - beforeEach(inject(($componentController, _$window_) => { + beforeEach(inject(($componentController, _$window_, _$httpBackend_) => { + $httpBackend = _$httpBackend_; $window = _$window_; const $element = angular.element(''); controller = $componentController('vnTicketIndex', {$element}); @@ -85,6 +87,21 @@ describe('Component vnTicketIndex', () => { }); }); + describe('setDelivered()/openDeliveryNotes()', () => { + it('should perform a post to setDelivered and open tabs with the delivery notes', () => { + controller.$.model = {data: tickets, refresh: () => {}}; + + $window.open = jest.fn(); + + $httpBackend.expect('POST', 'TicketTrackings/setDelivered').respond('ok'); + controller.setDelivered(); + $httpBackend.flush(); + + expect($window.open).toHaveBeenCalledWith(`api/report/delivery-note?ticketId=${tickets[1].id}`); + expect($window.open).toHaveBeenCalledWith(`api/report/delivery-note?ticketId=${tickets[2].id}`); + }); + }); + describe('checked()', () => { it('should return an array of checked tickets', () => { controller.$.model = {data: tickets}; diff --git a/modules/ticket/front/index/locale/es.yml b/modules/ticket/front/index/locale/es.yml index 180c975cc..e6e8427db 100644 --- a/modules/ticket/front/index/locale/es.yml +++ b/modules/ticket/front/index/locale/es.yml @@ -2,7 +2,7 @@ Weekly tickets: Tickets programados Go to lines: Ir a lineas Not available: No disponible Payment on account...: Pago a cuenta... -Open delivery note(s): Abrir albarán/es +Set as delivered and open delivery note(s): Marcar como servido/s y abrir albarán/es Closure: Cierre You cannot make a payment on account from multiple clients: No puedes realizar un pago a cuenta de clientes diferentes Filter by selection: Filtro por selección From 4d973572131f13cbbfdee2149c2b9baac96545b6 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Thu, 18 Jun 2020 13:50:52 +0200 Subject: [PATCH 3/4] update estate without model.refresh() --- .../back/methods/ticket-tracking/changeState.js | 2 +- .../back/methods/ticket-tracking/setDelivered.js | 14 ++++++++++---- .../ticket-tracking/specs/setDelivered.spec.js | 10 +++------- modules/ticket/front/index/index.html | 2 +- modules/ticket/front/index/index.js | 10 ++++++++-- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/modules/ticket/back/methods/ticket-tracking/changeState.js b/modules/ticket/back/methods/ticket-tracking/changeState.js index fae9af218..e28413b24 100644 --- a/modules/ticket/back/methods/ticket-tracking/changeState.js +++ b/modules/ticket/back/methods/ticket-tracking/changeState.js @@ -14,7 +14,7 @@ module.exports = Self => { } ], returns: { - type: 'string', + type: 'Object', root: true }, http: { diff --git a/modules/ticket/back/methods/ticket-tracking/setDelivered.js b/modules/ticket/back/methods/ticket-tracking/setDelivered.js index 62e5a7717..94cbad7a2 100644 --- a/modules/ticket/back/methods/ticket-tracking/setDelivered.js +++ b/modules/ticket/back/methods/ticket-tracking/setDelivered.js @@ -6,13 +6,13 @@ module.exports = Self => { { arg: 'ticketIds', description: 'the array of ticket ids to set as delivered', - type: [], + type: ['Number'], required: true, http: {source: 'body'} } ], returns: { - type: 'string', + type: 'Object', root: true }, http: { @@ -25,7 +25,12 @@ module.exports = Self => { let userId = ctx.req.accessToken.userId; let models = Self.app.models; - let state = await models.State.findOne({where: {code: 'delivered'}, fields: ['id']}); + let state = await models.State.findOne({ + where: { + code: 'delivered' + }, + fields: ['id', 'name', 'alertLevel', 'code'] + }); let worker = await models.Worker.findOne({where: {userFk: userId}}); @@ -38,7 +43,8 @@ module.exports = Self => { }); promises.push(promise); } + await Promise.all(promises); - return Promise.all(promises); + return state; }; }; diff --git a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js index 8a5963474..520ccfb03 100644 --- a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js +++ b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js @@ -23,18 +23,14 @@ describe('ticket setDelivered()', () => { done(); }); - it('should return an array of the tickets states changed to delivered', async() => { + it('should return the state which has been applied to the given tickets', async() => { let ctx = {req: {accessToken: {userId: 49}}}; let delivered = await app.models.State.findOne({where: {code: 'delivered'}, fields: ['id']}); let params = [ticketOne.id, ticketTwo.id]; - let res = await app.models.TicketTracking.setDelivered(ctx, params); + let state = await app.models.TicketTracking.setDelivered(ctx, params); - expect(res[0].ticketFk).toEqual(ticketOne.id); - expect(res[0].stateFk).toEqual(delivered.id); - - expect(res[1].ticketFk).toEqual(ticketTwo.id); - expect(res[1].stateFk).toEqual(delivered.id); + expect(state.id).toEqual(delivered.id); }); }); diff --git a/modules/ticket/front/index/index.html b/modules/ticket/front/index/index.html index 47695f38c..abd772229 100644 --- a/modules/ticket/front/index/index.html +++ b/modules/ticket/front/index/index.html @@ -89,7 +89,7 @@ {{::ticket.province}} - {{::ticket.state}} + {{ticket.state}} {{::ticket.agencyMode}} diff --git a/modules/ticket/front/index/index.js b/modules/ticket/front/index/index.js index 7034df848..0cf076d61 100644 --- a/modules/ticket/front/index/index.js +++ b/modules/ticket/front/index/index.js @@ -11,9 +11,15 @@ export default class Controller extends Section { for (let ticket of checkedTickets) ids.push(ticket.id); - this.$http.post('TicketTrackings/setDelivered', ids).then(() => { + this.$http.post('TicketTrackings/setDelivered', ids).then(res => { + let state = res.data; + for (let ticket of checkedTickets) { + ticket.stateFk = state.id; + ticket.state = state.name; + ticket.alertLevel = state.alertLevel; + ticket.alertLevelCode = state.code; + } this.openDeliveryNotes(ids); - this.$.model.refresh(); }); } From e6a72d0f09cde13b80947d43ca66ee1774228120 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Thu, 18 Jun 2020 15:26:15 +0200 Subject: [PATCH 4/4] clean code refactor --- .../back/methods/ticket-tracking/setDelivered.js | 4 ++-- modules/ticket/front/index/index.js | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/ticket/back/methods/ticket-tracking/setDelivered.js b/modules/ticket/back/methods/ticket-tracking/setDelivered.js index 94cbad7a2..509f5b446 100644 --- a/modules/ticket/back/methods/ticket-tracking/setDelivered.js +++ b/modules/ticket/back/methods/ticket-tracking/setDelivered.js @@ -35,11 +35,11 @@ module.exports = Self => { let worker = await models.Worker.findOne({where: {userFk: userId}}); let promises = []; - for (let i = 0; i < ticketIds.length; i++) { + for (let id of ticketIds) { let promise = models.TicketTracking.changeState(ctx, { stateFk: state.id, workerFk: worker.id, - ticketFk: ticketIds[i] + ticketFk: id }); promises.push(promise); } diff --git a/modules/ticket/front/index/index.js b/modules/ticket/front/index/index.js index 0cf076d61..2c8b9b83c 100644 --- a/modules/ticket/front/index/index.js +++ b/modules/ticket/front/index/index.js @@ -4,6 +4,10 @@ import UserError from 'core/lib/user-error'; import './style.scss'; export default class Controller extends Section { + constructor($element, $, vnReport) { + super($element, $); + this.vnReport = vnReport; + } setDelivered() { const checkedTickets = this.checked; let ids = []; @@ -25,13 +29,9 @@ export default class Controller extends Section { openDeliveryNotes(ids) { for (let id of ids) { - const params = { - authorization: this.vnToken.token, - ticketId: id - }; - - const serializedParams = this.$httpParamSerializer(params); - window.open(`api/report/delivery-note?${serializedParams}`); ``; + this.vnReport.show('delivery-note', { + ticketId: id, + }); } } @@ -156,6 +156,7 @@ export default class Controller extends Section { return [minHour, maxHour]; } } +Controller.$inject = ['$element', '$scope', 'vnReport']; ngModule.component('vnTicketIndex', { template: require('./index.html'),