diff --git a/modules/ticket/back/methods/ticket-tracking/changeState.js b/modules/ticket/back/methods/ticket-tracking/changeState.js index 065692137..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: { @@ -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..509f5b446 --- /dev/null +++ b/modules/ticket/back/methods/ticket-tracking/setDelivered.js @@ -0,0 +1,50 @@ +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: ['Number'], + required: true, + http: {source: 'body'} + } + ], + returns: { + type: 'Object', + 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', 'name', 'alertLevel', 'code'] + }); + + let worker = await models.Worker.findOne({where: {userFk: userId}}); + + let promises = []; + for (let id of ticketIds) { + let promise = models.TicketTracking.changeState(ctx, { + stateFk: state.id, + workerFk: worker.id, + ticketFk: id + }); + promises.push(promise); + } + await 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 new file mode 100644 index 000000000..520ccfb03 --- /dev/null +++ b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js @@ -0,0 +1,36 @@ +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 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 state = await app.models.TicketTracking.setDelivered(ctx, params); + + expect(state.id).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 f75e9d9f7..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}} @@ -120,6 +120,13 @@
+ + { + 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); + }); + } + + openDeliveryNotes(ids) { + for (let id of ids) { + this.vnReport.show('delivery-note', { + ticketId: id, + }); + } + } + openBalanceDialog() { const checkedTickets = this.checked; const description = []; @@ -125,6 +156,7 @@ export default class Controller extends Section { return [minHour, maxHour]; } } +Controller.$inject = ['$element', '$scope', 'vnReport']; ngModule.component('vnTicketIndex', { template: require('./index.html'), 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 10eaba350..e6e8427db 100644 --- a/modules/ticket/front/index/locale/es.yml +++ b/modules/ticket/front/index/locale/es.yml @@ -2,6 +2,7 @@ Weekly tickets: Tickets programados Go to lines: Ir a lineas Not available: No disponible Payment on account...: Pago a cuenta... +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