diff --git a/modules/route/back/methods/route/getSuggestedTickets.js b/modules/route/back/methods/route/getSuggestedTickets.js index c2afd60b4..fe268f8cc 100644 --- a/modules/route/back/methods/route/getSuggestedTickets.js +++ b/modules/route/back/methods/route/getSuggestedTickets.js @@ -19,14 +19,17 @@ module.exports = Self => { } }); - Self.getSuggestedTickets = async id => { - const route = await Self.app.models.Route.findById(id); + Self.getSuggestedTickets = async(id, options) => { + let myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); + const route = await Self.app.models.Route.findById(id, null, myOptions); const zoneAgencyModes = await Self.app.models.ZoneAgencyMode.find({ where: { agencyModeFk: route.agencyModeFk } - }); + }, myOptions); const zoneIds = []; for (let zoneAgencyMode of zoneAgencyModes) @@ -38,10 +41,9 @@ module.exports = Self => { maxDate.setHours(23, 59, 59, 59); let tickets = await Self.app.models.Ticket.find({ where: { - agencyModeFk: route.agencyModeFk, zoneFk: {inq: zoneIds}, routeFk: null, - shipped: {between: [minDate, maxDate]} + landed: {between: [minDate, maxDate]} }, include: [ { @@ -57,7 +59,7 @@ module.exports = Self => { } }, ] - }); + }, myOptions); return tickets; }; diff --git a/modules/route/back/methods/route/insertTicket.js b/modules/route/back/methods/route/insertTicket.js index 91393431e..d716bd8ba 100644 --- a/modules/route/back/methods/route/insertTicket.js +++ b/modules/route/back/methods/route/insertTicket.js @@ -26,26 +26,46 @@ module.exports = Self => { } }); - Self.insertTicket = async(routeId, ticketId) => { + Self.insertTicket = async(routeId, ticketId, options) => { const models = Self.app.models; - const route = await models.Route.findById(routeId); - const minDate = new Date(route.finished); - minDate.setHours(0, 0, 0, 0); + let tx; + let myOptions = {}; - const maxDate = new Date(route.finished); - maxDate.setHours(23, 59, 59, 59); - const ticket = await models.Ticket.findOne({ - where: { - id: ticketId, - routeFk: null, - landed: {between: [minDate, maxDate]}, - } - }); + if (typeof options == 'object') + Object.assign(myOptions, options); - if (!ticket) - throw new UserError('The selected ticket is not suitable for this route'); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } - return ticket.updateAttribute('routeFk', route.id); + try { + const route = await models.Route.findById(routeId, null, myOptions); + const minDate = new Date(route.created); + minDate.setHours(0, 0, 0, 0); + + const maxDate = new Date(route.created); + maxDate.setHours(23, 59, 59, 59); + const ticket = await models.Ticket.findOne({ + where: { + id: ticketId, + routeFk: null, + landed: {between: [minDate, maxDate]}, + } + }, myOptions); + + if (!ticket) + throw new UserError('The selected ticket is not suitable for this route'); + + const result = await ticket.updateAttribute('routeFk', route.id, myOptions); + + if (tx) await tx.commit(); + + return result; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js b/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js index b74f3b710..3583df7cd 100644 --- a/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js +++ b/modules/route/back/methods/route/specs/getSuggestedTickets.spec.js @@ -2,6 +2,8 @@ const app = require('vn-loopback/server/server'); const LoopBackContext = require('loopback-context'); describe('route getSuggestedTickets()', () => { + const routeID = 1; + const ticketId = 12; it('should return an array of suggested tickets', async() => { const activeCtx = { accessToken: {userId: 19}, @@ -11,20 +13,30 @@ describe('route getSuggestedTickets()', () => { active: activeCtx }); - const routeID = 1; - const ticketInRoute = await app.models.Ticket.findById(12); + const tx = await app.models.Ticket.beginTransaction({}); - await ticketInRoute.updateAttribute('routeFk', null); + try { + const options = {transaction: tx}; + const ticketInRoute = await app.models.Ticket.findById(ticketId, null, options); - const result = await app.models.Route.getSuggestedTickets(routeID); + await ticketInRoute.updateAttributes({ + routeFk: null, + landed: new Date() + }, options); - const length = result.length; - const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; + const result = await app.models.Route.getSuggestedTickets(routeID, options); - expect(result.length).toEqual(1); - expect(anyResult.zoneFk).toEqual(1); - expect(anyResult.agencyModeFk).toEqual(1); + const length = result.length; + const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; - await ticketInRoute.updateAttribute('routeFk', routeID); + expect(result.length).toEqual(1); + expect(anyResult.zoneFk).toEqual(1); + expect(anyResult.agencyModeFk).toEqual(1); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/route/back/methods/route/specs/insertTicket.spec.js b/modules/route/back/methods/route/specs/insertTicket.spec.js index 0f8c35ada..9e7806496 100644 --- a/modules/route/back/methods/route/specs/insertTicket.spec.js +++ b/modules/route/back/methods/route/specs/insertTicket.spec.js @@ -3,7 +3,6 @@ const LoopBackContext = require('loopback-context'); describe('route insertTicket()', () => { const deliveryId = 56; - let originalTicket; const routeId = 1; const activeCtx = { accessToken: {userId: deliveryId}, @@ -19,12 +18,24 @@ describe('route insertTicket()', () => { it('should add the ticket to a route', async() => { const ticketId = 12; - originalTicket = await app.models.Ticket.findById(ticketId); - await originalTicket.updateAttribute('routeFk', null); + const tx = await app.models.Ticket.beginTransaction({}); - const result = await app.models.Route.insertTicket(routeId, ticketId); + try { + const options = {transaction: tx}; + const ticketInRoute = await app.models.Ticket.findById(ticketId, null, options); + await ticketInRoute.updateAttributes({ + routeFk: null, + landed: new Date() + }, options); - expect(result.routeFk).toEqual(routeId); + const result = await app.models.Route.insertTicket(routeId, ticketId, options); + + expect(result.routeFk).toEqual(routeId); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should throw and error if the ticket is not suitable for the route', async() => { diff --git a/modules/route/front/tickets/index.html b/modules/route/front/tickets/index.html index 858215854..012c45fe2 100644 --- a/modules/route/front/tickets/index.html +++ b/modules/route/front/tickets/index.html @@ -63,14 +63,14 @@ display-controls=true> - {{::ticket.street}} + {{::ticket.street}} {{::ticket.city}} {{::ticket.postalCode}} - {{ticket.nickname}} + {{::ticket.nickname}} {{::ticket.packages}} @@ -84,8 +84,8 @@ @@ -149,18 +149,18 @@ ng-model="ticket.checked"> - {{ticket.id}} + {{::ticket.id}} - {{ticket.nickname}} + {{::ticket.nickname}} - {{ticket.packages}} - {{ticket.warehouse.name}} - {{ticket.address.postalCode}} - {{ticket.address.street}} + {{::ticket.packages}} + {{::ticket.warehouse.name}} + {{::ticket.address.postalCode}} + {{::ticket.address.street}}