From 344427da458857a3896dad0853d55a2d4c96dfd2 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 13 Nov 2024 13:46:40 +0100 Subject: [PATCH 1/5] feat: refs #8151 moveExpeditions --- .../back/methods/expedition/moveExpeditions.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/ticket/back/methods/expedition/moveExpeditions.js b/modules/ticket/back/methods/expedition/moveExpeditions.js index 1c6689912..3e9c95ec6 100644 --- a/modules/ticket/back/methods/expedition/moveExpeditions.js +++ b/modules/ticket/back/methods/expedition/moveExpeditions.js @@ -80,11 +80,19 @@ module.exports = Self => { const expeditionUpdated = expeditionToUpdate.updateAttribute('ticketFk', ticket.id, myOptions); promises.push(expeditionUpdated); } - await Promise.all(promises); + await models.Ticket.updateAll({id: ticket.id}, {packages: promises.length}, myOptions); + const state = await models.State.findOne({ + where: {code: 'PACKED'} + }, myOptions); + + await models.Ticket.state(ctx, { + ticketFk: ticket.id, + stateFk: state.id, + userFk: ctx.req.accessToken.userId + }, myOptions); if (tx) await tx.commit(); - return ticket; } catch (e) { if (tx) await tx.rollback(); From 018393fa93fec4d0642521852eb31686b569ac70 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 13 Nov 2024 14:04:02 +0100 Subject: [PATCH 2/5] feat: refs #8151 Added test --- .../methods/expedition/moveExpeditions.js | 4 +- .../expedition/specs/moveExpeditions.spec.js | 44 ++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/modules/ticket/back/methods/expedition/moveExpeditions.js b/modules/ticket/back/methods/expedition/moveExpeditions.js index 3e9c95ec6..3ad41897f 100644 --- a/modules/ticket/back/methods/expedition/moveExpeditions.js +++ b/modules/ticket/back/methods/expedition/moveExpeditions.js @@ -82,9 +82,7 @@ module.exports = Self => { } await Promise.all(promises); await models.Ticket.updateAll({id: ticket.id}, {packages: promises.length}, myOptions); - const state = await models.State.findOne({ - where: {code: 'PACKED'} - }, myOptions); + const state = await models.State.findOne({where: {code: 'PACKED'}}); await models.Ticket.state(ctx, { ticketFk: ticket.id, diff --git a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js index c1c7c2c12..4c4971a0d 100644 --- a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js +++ b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('ticket moveExpeditions()', () => { +fdescribe('ticket moveExpeditions()', () => { const ctx = beforeAll.getCtx(); it('should move the selected expeditions to new ticket', async() => { @@ -32,5 +32,45 @@ describe('ticket moveExpeditions()', () => { throw e; } }); -}); + it('should move expeditions to a new ticket, set state to PACKED, and update packages count correctly', async() => { + const tx = await models.Expedition.beginTransaction({}); + const myCtx = ctx; + + try { + const options = {transaction: tx}; + myCtx.args = { + clientId: 1101, + landed: Date.vnNew(), + warehouseId: 1, + addressId: 121, + agencyModeId: 1, + routeId: null, + expeditionIds: [1, 2] + }; + + const ticket = await models.Expedition.moveExpeditions(myCtx, options); + + const newestTicketIdInFixtures = 27; + + expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures); + + const updatedTicket = await models.Ticket.findById(ticket.id, null, options); + const packedState = await models.State.findOne({where: {code: 'PACKED'}}, options); + + const state = await models.TicketState.findOne({where: {ticketFk: ticket.id}}, options); + + expect(state.stateFk).toBe(packedState.id); + expect(state.userFk).toBe(myCtx.req.accessToken.userId); + + const expectedPackagesCount = myCtx.args.expeditionIds.length; + + expect(updatedTicket.$packages).toBe(expectedPackagesCount); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); From d4a60f63c9125798d1a58474832542d11e80cc04 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 13 Nov 2024 14:05:03 +0100 Subject: [PATCH 3/5] feat: refs #8151 Added test --- .../back/methods/expedition/specs/moveExpeditions.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js index 4c4971a0d..2f0fd673c 100644 --- a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js +++ b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -fdescribe('ticket moveExpeditions()', () => { +describe('ticket moveExpeditions()', () => { const ctx = beforeAll.getCtx(); it('should move the selected expeditions to new ticket', async() => { From 534b50f51a65ccea3d282aee57041c9c3641c6ac Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 10:45:28 +0100 Subject: [PATCH 4/5] feat: refs #8151 Requested changes --- .../expedition/specs/moveExpeditions.spec.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js index 2f0fd673c..9c91df5d6 100644 --- a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js +++ b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('ticket moveExpeditions()', () => { +fdescribe('ticket moveExpeditions()', () => { const ctx = beforeAll.getCtx(); it('should move the selected expeditions to new ticket', async() => { @@ -17,14 +17,13 @@ describe('ticket moveExpeditions()', () => { agencyModeId: 1, routeId: null, expeditionIds: [1, 2] - }; - + const newestTicketIdInFixtures = await models.Ticket.findOne({ + order: 'id DESC' + }, options); const ticket = await models.Expedition.moveExpeditions(ctx, options); - const newestTicketIdInFixtures = 27; - - expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures); + expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures.id); await tx.rollback(); } catch (e) { @@ -49,11 +48,12 @@ describe('ticket moveExpeditions()', () => { expeditionIds: [1, 2] }; + const newestTicketIdInFixtures = await models.Ticket.findOne({ + order: 'id DESC' + }, options); const ticket = await models.Expedition.moveExpeditions(myCtx, options); - const newestTicketIdInFixtures = 27; - - expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures); + expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures.id); const updatedTicket = await models.Ticket.findById(ticket.id, null, options); const packedState = await models.State.findOne({where: {code: 'PACKED'}}, options); From b3dcaf4cf549fc9cdbf0f99ca1611c49e816ccd7 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 14 Nov 2024 10:53:13 +0100 Subject: [PATCH 5/5] feat: refs #8151 Requested changes --- .../back/methods/expedition/specs/moveExpeditions.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js index 9c91df5d6..0edae9c6a 100644 --- a/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js +++ b/modules/ticket/back/methods/expedition/specs/moveExpeditions.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -fdescribe('ticket moveExpeditions()', () => { +describe('ticket moveExpeditions()', () => { const ctx = beforeAll.getCtx(); it('should move the selected expeditions to new ticket', async() => {