diff --git a/modules/route/back/methods/route/getSuggestedTickets.js b/modules/route/back/methods/route/getSuggestedTickets.js index fc637c60f..339c8d826 100644 --- a/modules/route/back/methods/route/getSuggestedTickets.js +++ b/modules/route/back/methods/route/getSuggestedTickets.js @@ -20,6 +20,12 @@ module.exports = Self => { }); Self.getSuggestedTickets = async id => { + const ticketsInRoute = await Self.app.models.Ticket.find({ + where: {routeFk: id}, + fields: ['id'] + }); + const idsToExclude = ticketsInRoute.map(ticket => ticket.id); + const route = await Self.app.models.Route.findById(id); const zoneAgencyModes = await Self.app.models.ZoneAgencyMode.find({ @@ -32,11 +38,26 @@ module.exports = Self => { for (let zoneAgencyMode of zoneAgencyModes) zoneIds.push(zoneAgencyMode.zoneFk); - const tickets = await Self.app.models.Ticket.find({ + let tickets = await Self.app.models.Ticket.find({ where: { - ageconyModeFk: route.agencyModeFk, - zoneFk: {inq: zoneIds} - } + agencyModeFk: route.agencyModeFk, + zoneFk: {inq: zoneIds}, + id: {nin: idsToExclude} + }, + include: [ + { + relation: 'warehouse', + scope: { + fields: ['id', 'name'] + } + }, + { + relation: 'address', + scope: { + fields: ['id', 'street', 'postalCode', 'city'], + } + }, + ] }); return tickets; diff --git a/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js b/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js index 56fb56a62..e73cfebac 100644 --- a/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js +++ b/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js @@ -1,14 +1,30 @@ const app = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); describe('route getSuggestedTickets()', () => { it('should return an array of suggested tickets', async() => { - const result = await app.models.Route.getSuggestedTickets(1); + const activeCtx = { + accessToken: {userId: 19}, + headers: {origin: 'http://localhost'} + }; + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + + const routeID = 1; + const ticketInRoute = await app.models.Ticket.findOne({where: {routeFk: routeID}}); + + await ticketInRoute.updateAttribute('routeFk', null); + + const result = await app.models.Route.getSuggestedTickets(routeID); const length = result.length; const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; - expect(result.length).toEqual(4); + expect(result.length).toEqual(1); expect(anyResult.zoneFk).toEqual(1); expect(anyResult.agencyModeFk).toEqual(1); + + await ticketInRoute.updateAttribute('routeFk', routeID); }); }); diff --git a/modules/route/front/tickets/index.js b/modules/route/front/tickets/index.js index f1103e517..865831841 100644 --- a/modules/route/front/tickets/index.js +++ b/modules/route/front/tickets/index.js @@ -106,14 +106,26 @@ class Controller extends Section { setTicketsRoute() { let tickets = this.getSelectedItems(this.possibleTickets); if (tickets.length === 0) return; - for (let i = 0; i < tickets.length; i++) { - delete tickets[i].checked; - tickets[i].routeFk = this.route.id; + + const updates = []; + + for (let ticket of tickets) { + delete ticket.checked; + const update = { + where: {id: ticket.id}, + data: {routeFk: this.route.id} + }; + + updates.push(update); } - return this.$.possibleTicketsModel.save().then(() => { - this.$.model.data = this.$.model.data.concat(tickets); - }); + const data = {creates: [], updates: updates, deletes: []}; + + return this.$http.post(`Tickets/crud`, data) + .then(() => { + this.$.model.data = this.$.model.data.concat(tickets); + this.vnApp.showSuccess(this.$t('Data saved!')); + }); } onDrop($event) { diff --git a/modules/route/front/tickets/index.spec.js b/modules/route/front/tickets/index.spec.js index 8a8f09489..77618e2e7 100644 --- a/modules/route/front/tickets/index.spec.js +++ b/modules/route/front/tickets/index.spec.js @@ -37,42 +37,6 @@ describe('Route', () => { }); }); - describe('buildPossibleTicketsFilter()', () => { - it('should build the possible tickets filter', () => { - let expectedFilter = { - include: [ - { - relation: 'warehouse', - scope: { - fields: ['name'] - } - }, { - relation: 'address' - } - ], - where: { - landed: { - between: [ - jasmine.any(Date), - jasmine.any(Date) - ] - }, - routeFk: null, - zoneFk: 67 - } - }; - controller.route = { - finished: new Date(), - routeFk: null, - zoneFk: 67 - }; - - controller.buildPossibleTicketsFilter(); - - expect(controller.possibleTicketsFilter).toEqual(expectedFilter); - }); - }); - describe('getHighestPriority()', () => { it('should return the highest value found in priorities plus 1', () => { controller.$.model = {data: [ @@ -228,13 +192,13 @@ describe('Route', () => { }); describe('setTicketsRoute()', () => { - it('should perform a POST query to add tickets to the route', done => { - controller.$.possibleTicketsModel = {save: () => {}}; - jest.spyOn(controller.$.possibleTicketsModel, 'save').mockReturnValue(Promise.resolve()); + it('should perform a POST query to add tickets to the route', () => { controller.$.model = {data: [ {id: 1, checked: false} ]}; + const existingTicket = controller.$.model.data[0]; + controller.route = {id: 111}; controller.possibleTickets = [ @@ -245,15 +209,16 @@ describe('Route', () => { ]; let expectedResult = [ - {checked: false, id: 1}, - {id: 3, routeFk: 111}, - {id: 5, routeFk: 111} + existingTicket, + {id: 3}, + {id: 5} ]; - controller.setTicketsRoute().then(() => { - expect(controller.$.model.data).toEqual(expectedResult); - done(); - }).catch(done.fail); + $httpBackend.expectPOST(`Tickets/crud`).respond(); + controller.setTicketsRoute(); + $httpBackend.flush(); + + expect(controller.$.model.data).toEqual(expectedResult); }); });