From 57f6c6d9a1b4f5214c3bb1b3d5f612f335d137e2 Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 31 Jul 2024 10:43:55 +0200 Subject: [PATCH 01/10] chore: refs #7663 WIP setWeight --- .../11178-yellowCamellia/00-aclSetWeight.sql | 9 +++ loopback/locale/es.json | 3 +- .../ticket/back/methods/ticket/setWeight.js | 75 +++++++++++++++++++ modules/ticket/back/models/ticket-methods.js | 1 + modules/ticket/front/summary/locale/es.yml | 3 +- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 db/versions/11178-yellowCamellia/00-aclSetWeight.sql create mode 100644 modules/ticket/back/methods/ticket/setWeight.js diff --git a/db/versions/11178-yellowCamellia/00-aclSetWeight.sql b/db/versions/11178-yellowCamellia/00-aclSetWeight.sql new file mode 100644 index 000000000..d70287738 --- /dev/null +++ b/db/versions/11178-yellowCamellia/00-aclSetWeight.sql @@ -0,0 +1,9 @@ +-- Place your SQL code here +INSERT INTO salix.ACL + SET model = 'Ticket', + property = 'setWeight', + accessType = 'WRITE', + permission = 'ALLOW', + principalType = 'ROLE', + principalId = 'salesPerson'; + \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index acc3d69f6..eb7a2aaf2 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -368,5 +368,6 @@ "Payment method is required": "El método de pago es obligatorio", "Cannot send mail": "Não é possível enviar o email", "CONSTRAINT `supplierAccountTooShort` failed for `vn`.`supplier`": "La cuenta debe tener exactamente 10 dígitos", - "The sale not exists in the item shelving": "La venta no existe en la estantería del artículo" + "The sale not exists in the item shelving": "La venta no existe en la estantería del artículo", + "Weight already set": "El peso ya está establecido" } \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/setWeight.js b/modules/ticket/back/methods/ticket/setWeight.js new file mode 100644 index 000000000..f2a65b1d6 --- /dev/null +++ b/modules/ticket/back/methods/ticket/setWeight.js @@ -0,0 +1,75 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('setWeight', { + description: 'Sets weight of a ticket', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'The ticket id', + http: {source: 'path'} + }, { + arg: 'weight', + type: 'number', + required: true, + description: 'The weight value', + }], + http: { + path: `/:id/setWeight`, + verb: 'POST' + } + }); + + Self.setWeight = async(ctx, ticketId, weight, invoiceable, options) => { + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + const myOptions = {userId}; + let tx; + + if (typeof options == 'object') Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + if (ticket.weight) throw new UserError('Weight already set'); + + const canEdit = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'updateAttributes'); + const ticket = await Self.findById(ticketId, null, myOptions); + const client = await models.Client.findById(ticket.clientFk, { + include: {relation: 'salesPersonUser'}}, + myOptions); + + if (!canEdit) { + const salesPersonUser = client.salesPersonUser(); + const workerDepartments = await models.WorkerDepartment.find({ + include: {relation: 'department'}, + where: {workerFk: {inq: [userId, salesPersonUser.id]}} + }, myOptions); + + if (workerDepartments[0].departmentFk != workerDepartments[1].departmentFk) + throw new UserError('You don\'t have enough privileges'); + } + + await ticket.updateAttribute('weight', weight, myOptions); + + const packedState = await models.State.findOne({where: {code: 'PACKED'}}, myOptions); + const ticketState = await models.TicketState.findOne({where: {ticketFk: ticketId}}, myOptions); + + const [{taxArea}] = await Self.rawSql('SELECT clientTaxArea(?,?) taxArea', + [ticket.clientFk, ticket.warehouseFk], myOptions); + + if (ticketState.alertLevel >= packedState.alertLevel && taxArea == 'WORLD' && client.hasDailyInvoice) + await Self.invoiceTicketsAndPdf(ctx, [ticketId], null, myOptions); + + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 5582dde5c..457454627 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -47,4 +47,5 @@ module.exports = function(Self) { require('../methods/ticket/docuwareDownload')(Self); require('../methods/ticket/myLastModified')(Self); require('../methods/ticket/clone')(Self); + require('../methods/ticket/setWeight')(Self); }; diff --git a/modules/ticket/front/summary/locale/es.yml b/modules/ticket/front/summary/locale/es.yml index d1e6dba58..9de7ccbf4 100644 --- a/modules/ticket/front/summary/locale/es.yml +++ b/modules/ticket/front/summary/locale/es.yml @@ -3,4 +3,5 @@ Address mobile: Móv. consignatario Client phone: Tel. cliente Client mobile: Móv. cliente Go to the ticket: Ir al ticket -Change state: Cambiar estado \ No newline at end of file +Change state: Cambiar estado +Weight: Peso \ No newline at end of file From 52d62710e6347cc2b8e8d6ab8fb49effaf663757 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 13 Aug 2024 13:49:00 +0200 Subject: [PATCH 02/10] chore: refs #7663 fix logic --- .../ticket/back/methods/ticket/setWeight.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/ticket/back/methods/ticket/setWeight.js b/modules/ticket/back/methods/ticket/setWeight.js index f2a65b1d6..4f9e1883e 100644 --- a/modules/ticket/back/methods/ticket/setWeight.js +++ b/modules/ticket/back/methods/ticket/setWeight.js @@ -16,13 +16,17 @@ module.exports = Self => { required: true, description: 'The weight value', }], + returns: { + type: 'boolean', + root: true + }, http: { path: `/:id/setWeight`, verb: 'POST' } }); - Self.setWeight = async(ctx, ticketId, weight, invoiceable, options) => { + Self.setWeight = async(ctx, ticketId, weight, options) => { const models = Self.app.models; const userId = ctx.req.accessToken.userId; const myOptions = {userId}; @@ -36,10 +40,10 @@ module.exports = Self => { } try { + const ticket = await Self.findById(ticketId, null, myOptions); if (ticket.weight) throw new UserError('Weight already set'); const canEdit = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'updateAttributes'); - const ticket = await Self.findById(ticketId, null, myOptions); const client = await models.Client.findById(ticket.clientFk, { include: {relation: 'salesPersonUser'}}, myOptions); @@ -51,7 +55,10 @@ module.exports = Self => { where: {workerFk: {inq: [userId, salesPersonUser.id]}} }, myOptions); - if (workerDepartments[0].departmentFk != workerDepartments[1].departmentFk) + if ( + workerDepartments.length == 2 && + workerDepartments[0].departmentFk != workerDepartments[1].departmentFk + ) throw new UserError('You don\'t have enough privileges'); } @@ -63,10 +70,12 @@ module.exports = Self => { const [{taxArea}] = await Self.rawSql('SELECT clientTaxArea(?,?) taxArea', [ticket.clientFk, ticket.warehouseFk], myOptions); - if (ticketState.alertLevel >= packedState.alertLevel && taxArea == 'WORLD' && client.hasDailyInvoice) + if (ticketState.alertLevel >= packedState.alertLevel && taxArea == 'WORLD' && client.hasDailyInvoice) { await Self.invoiceTicketsAndPdf(ctx, [ticketId], null, myOptions); - + return true; + } if (tx) await tx.commit(); + return false; } catch (e) { if (tx) await tx.rollback(); throw e; From 6c9676ce9c90f145b638d3ec293e6d44d274d2db Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 13 Aug 2024 14:52:26 +0200 Subject: [PATCH 03/10] chore: refs #7663 fix logic --- modules/ticket/back/methods/ticket/setWeight.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/ticket/back/methods/ticket/setWeight.js b/modules/ticket/back/methods/ticket/setWeight.js index 4f9e1883e..91ecef634 100644 --- a/modules/ticket/back/methods/ticket/setWeight.js +++ b/modules/ticket/back/methods/ticket/setWeight.js @@ -70,12 +70,13 @@ module.exports = Self => { const [{taxArea}] = await Self.rawSql('SELECT clientTaxArea(?,?) taxArea', [ticket.clientFk, ticket.warehouseFk], myOptions); - if (ticketState.alertLevel >= packedState.alertLevel && taxArea == 'WORLD' && client.hasDailyInvoice) { - await Self.invoiceTicketsAndPdf(ctx, [ticketId], null, myOptions); - return true; - } + const isInvoiceable = ticketState.alertLevel >= packedState.alertLevel && + taxArea == 'WORLD' && client.hasDailyInvoice; + if (tx) await tx.commit(); - return false; + if (isInvoiceable) await Self.invoiceTicketsAndPdf(ctx, [ticketId]); + + return isInvoiceable; } catch (e) { if (tx) await tx.rollback(); throw e; From 4c5f5c8324c8424d3bbb001fae7c7c4538694ca3 Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 2 Sep 2024 17:08:23 +0200 Subject: [PATCH 04/10] fix: refs #7663 conflicts --- modules/ticket/back/models/ticket-methods.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index cb2baf01f..12161d5f5 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -46,6 +46,5 @@ module.exports = function(Self) { require('../methods/ticket/invoiceTicketsAndPdf')(Self); require('../methods/ticket/docuwareDownload')(Self); require('../methods/ticket/myLastModified')(Self); - require('../methods/ticket/clone')(Self); require('../methods/ticket/setWeight')(Self); }; From 9797d5e219b1c3d1a1a9a57f50163f1b7ded9087 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 3 Sep 2024 17:33:44 +0200 Subject: [PATCH 05/10] feat: refs #7663 return created invoice ids --- modules/ticket/back/methods/ticket/setWeight.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ticket/back/methods/ticket/setWeight.js b/modules/ticket/back/methods/ticket/setWeight.js index 91ecef634..d71f2c0e5 100644 --- a/modules/ticket/back/methods/ticket/setWeight.js +++ b/modules/ticket/back/methods/ticket/setWeight.js @@ -74,9 +74,10 @@ module.exports = Self => { taxArea == 'WORLD' && client.hasDailyInvoice; if (tx) await tx.commit(); - if (isInvoiceable) await Self.invoiceTicketsAndPdf(ctx, [ticketId]); + let invoiceIds = []; + if (isInvoiceable) invoiceIds = [...await Self.invoiceTicketsAndPdf(ctx, [ticketId])]; - return isInvoiceable; + return invoiceIds; } catch (e) { if (tx) await tx.rollback(); throw e; From df5961a2a2bb196ca9e794c15fd73d80f5ce2973 Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 4 Sep 2024 11:41:47 +0200 Subject: [PATCH 06/10] feat: refs #7663 wip test --- .../methods/ticket/specs/setWeight.spec.js | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 modules/ticket/back/methods/ticket/specs/setWeight.spec.js diff --git a/modules/ticket/back/methods/ticket/specs/setWeight.spec.js b/modules/ticket/back/methods/ticket/specs/setWeight.spec.js new file mode 100644 index 000000000..0d48d4adf --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/setWeight.spec.js @@ -0,0 +1,82 @@ +const {models} = require('vn-loopback/server/server'); + +fdescribe('ticket setWeight()', () => { + const ctx = beforeAll.getCtx(); + beforeAll.mockLoopBackContext(); + let opts; + let tx; + const administrativeId = 5; + + beforeEach(async() => { + opts = {transaction: tx}; + tx = await models.Ticket.beginTransaction({}); + opts.transaction = tx; + }); + + afterEach(async() => await tx.rollback()); + + xit('should throw an error if the weight is already set', async() => { + try { + const ticketId = 1; + const weight = 10; + + await models.Ticket.setWeight(ctx, ticketId, weight, opts); + } catch (e) { + expect(e.message).toEqual('Weight already set'); + } + }); + + xit('should set the weight of a ticket', async() => { + const ticketId = 31; + const weight = 15; + + await models.Ticket.setWeight(ctx, ticketId, weight, opts); + + const ticket = await models.Ticket.findById(ticketId, null, opts); + + expect(ticket.weight).toEqual(weight); + }); + + it('should throw an error if the user does not have enough privileges', async() => { + ctx.req.accessToken.userId = administrativeId; + try { + const ticketId = 10; + const weight = 20; + + await models.Ticket.setWeight(ctx, ticketId, weight, opts); + } catch (e) { + expect(e.message).toEqual('You don\'t have enough privilegs'); + } + }); + + // it('should commit the transaction and return invoice ids if the ticket is invoiceable', async() => { + // const tx = await models.Ticket.beginTransaction({}); + + // try { + // const opts = {transaction: tx}; + + // const ticketId = 4; + // const weight = 25; + + // // Mock the necessary methods and data + // jest.spyOn(models.ACL, 'checkAccessAcl').mockResolvedValue(true); + // jest.spyOn(models.Client, 'findById').mockResolvedValue({ + // hasDailyInvoice: true, + // salesPersonUser: () => ({id: 1}) + // }); + // jest.spyOn(models.State, 'findOne').mockResolvedValue({alertLevel: 2}); + // jest.spyOn(models.TicketState, 'findOne').mockResolvedValue({alertLevel: 3}); + // jest.spyOn(models.Ticket, 'rawSql').mockResolvedValue([{taxArea: 'WORLD'}]); + // jest.spyOn(models.Ticket, 'invoiceTicketsAndPdf').mockResolvedValue([1001]); + + // const invoiceIds = await models.Ticket.setWeight(ctx, ticketId, weight, opts); + + // expect(invoiceIds).toEqual([1001]); + + // await tx.rollback(); + // } catch (e) { + // await tx.rollback(); + // throw e; + // } + // }); +}); From 998d3865a357be22c63c3c9759c65fc45133048e Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 5 Sep 2024 08:56:15 +0200 Subject: [PATCH 07/10] chore: refs #7663 fix test --- .../methods/ticket/specs/setWeight.spec.js | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/modules/ticket/back/methods/ticket/specs/setWeight.spec.js b/modules/ticket/back/methods/ticket/specs/setWeight.spec.js index 0d48d4adf..c26ae7aaf 100644 --- a/modules/ticket/back/methods/ticket/specs/setWeight.spec.js +++ b/modules/ticket/back/methods/ticket/specs/setWeight.spec.js @@ -1,21 +1,23 @@ const {models} = require('vn-loopback/server/server'); -fdescribe('ticket setWeight()', () => { +describe('ticket setWeight()', () => { const ctx = beforeAll.getCtx(); beforeAll.mockLoopBackContext(); let opts; let tx; + const employeeId = 1; const administrativeId = 5; beforeEach(async() => { opts = {transaction: tx}; tx = await models.Ticket.beginTransaction({}); opts.transaction = tx; + ctx.req.accessToken.userId = administrativeId; }); afterEach(async() => await tx.rollback()); - xit('should throw an error if the weight is already set', async() => { + it('should throw an error if the weight is already set', async() => { try { const ticketId = 1; const weight = 10; @@ -26,7 +28,7 @@ fdescribe('ticket setWeight()', () => { } }); - xit('should set the weight of a ticket', async() => { + it('should set the weight of a ticket', async() => { const ticketId = 31; const weight = 15; @@ -38,45 +40,31 @@ fdescribe('ticket setWeight()', () => { }); it('should throw an error if the user does not have enough privileges', async() => { - ctx.req.accessToken.userId = administrativeId; + ctx.req.accessToken.userId = employeeId; try { const ticketId = 10; const weight = 20; await models.Ticket.setWeight(ctx, ticketId, weight, opts); } catch (e) { - expect(e.message).toEqual('You don\'t have enough privilegs'); + expect(e.message).toEqual('You don\'t have enough privileges'); } }); - // it('should commit the transaction and return invoice ids if the ticket is invoiceable', async() => { - // const tx = await models.Ticket.beginTransaction({}); + it('should call invoiceTicketsAndPdf if the ticket is invoiceable', async() => { + const ticketId = 10; + const weight = 25; - // try { - // const opts = {transaction: tx}; + spyOn(models.Client, 'findById').and.returnValue({ + hasDailyInvoice: true, + salesPersonUser: () => ({id: 1}) + }); + spyOn(models.TicketState, 'findOne').and.returnValue({alertLevel: 3}); + spyOn(models.Ticket, 'rawSql').and.returnValue([{taxArea: 'WORLD'}]); + spyOn(models.Ticket, 'invoiceTicketsAndPdf').and.returnValue([10]); - // const ticketId = 4; - // const weight = 25; + const invoiceIds = await models.Ticket.setWeight(ctx, ticketId, weight, opts); - // // Mock the necessary methods and data - // jest.spyOn(models.ACL, 'checkAccessAcl').mockResolvedValue(true); - // jest.spyOn(models.Client, 'findById').mockResolvedValue({ - // hasDailyInvoice: true, - // salesPersonUser: () => ({id: 1}) - // }); - // jest.spyOn(models.State, 'findOne').mockResolvedValue({alertLevel: 2}); - // jest.spyOn(models.TicketState, 'findOne').mockResolvedValue({alertLevel: 3}); - // jest.spyOn(models.Ticket, 'rawSql').mockResolvedValue([{taxArea: 'WORLD'}]); - // jest.spyOn(models.Ticket, 'invoiceTicketsAndPdf').mockResolvedValue([1001]); - - // const invoiceIds = await models.Ticket.setWeight(ctx, ticketId, weight, opts); - - // expect(invoiceIds).toEqual([1001]); - - // await tx.rollback(); - // } catch (e) { - // await tx.rollback(); - // throw e; - // } - // }); + expect(invoiceIds.length).toBeGreaterThan(0); + }); }); From 99ce15863291525340c4c35677776906c033d933 Mon Sep 17 00:00:00 2001 From: jorgep Date: Fri, 6 Sep 2024 11:33:15 +0200 Subject: [PATCH 08/10] chore: refs #7663 return correct type --- modules/ticket/back/methods/ticket/setWeight.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ticket/back/methods/ticket/setWeight.js b/modules/ticket/back/methods/ticket/setWeight.js index d71f2c0e5..47087d384 100644 --- a/modules/ticket/back/methods/ticket/setWeight.js +++ b/modules/ticket/back/methods/ticket/setWeight.js @@ -17,7 +17,7 @@ module.exports = Self => { description: 'The weight value', }], returns: { - type: 'boolean', + type: 'Array', root: true }, http: { From 91abf2eefe7a68473b19fbf16794200fe37a03ea Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 10 Sep 2024 09:50:02 +0200 Subject: [PATCH 09/10] refactor: refs #7663 accurate error --- loopback/locale/es.json | 3 ++- modules/ticket/back/methods/ticket/setWeight.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index e9e9cd212..064ea4a95 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -373,5 +373,6 @@ "Too many records": "Demasiados registros", "Original invoice not found": "Factura original no encontrada", "The entry has no lines or does not exist": "La entrada no tiene lineas o no existe", - "Weight already set": "El peso ya está establecido" + "Weight already set": "El peso ya está establecido", + "This ticket is not allocated to your department": "Este ticket no está asignado a tu departamento" } \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket/setWeight.js b/modules/ticket/back/methods/ticket/setWeight.js index 47087d384..b3b505832 100644 --- a/modules/ticket/back/methods/ticket/setWeight.js +++ b/modules/ticket/back/methods/ticket/setWeight.js @@ -59,7 +59,7 @@ module.exports = Self => { workerDepartments.length == 2 && workerDepartments[0].departmentFk != workerDepartments[1].departmentFk ) - throw new UserError('You don\'t have enough privileges'); + throw new UserError('This ticket is not allocated to your department'); } await ticket.updateAttribute('weight', weight, myOptions); From 2ee0c93e246335e157141af1bb49f38a3ecfbe2d Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 11 Sep 2024 09:35:26 +0200 Subject: [PATCH 10/10] chore: refs #7663 fix test --- modules/ticket/back/methods/ticket/specs/setWeight.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ticket/back/methods/ticket/specs/setWeight.spec.js b/modules/ticket/back/methods/ticket/specs/setWeight.spec.js index c26ae7aaf..f6ef1f8e7 100644 --- a/modules/ticket/back/methods/ticket/specs/setWeight.spec.js +++ b/modules/ticket/back/methods/ticket/specs/setWeight.spec.js @@ -39,7 +39,7 @@ describe('ticket setWeight()', () => { expect(ticket.weight).toEqual(weight); }); - it('should throw an error if the user does not have enough privileges', async() => { + it('should throw an error if the user is not allocated to the same department', async() => { ctx.req.accessToken.userId = employeeId; try { const ticketId = 10; @@ -47,7 +47,7 @@ describe('ticket setWeight()', () => { await models.Ticket.setWeight(ctx, ticketId, weight, opts); } catch (e) { - expect(e.message).toEqual('You don\'t have enough privileges'); + expect(e.message).toEqual('This ticket is not allocated to your department'); } });