From 8c5f0d3019a818c838a566ee7405aad852fa2f4e Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 19 Nov 2021 09:24:31 +0100 Subject: [PATCH 1/3] feat(createClaim): disallow claim creation for a ticket delivered more than seven days ago Refs: 3278 --- loopback/locale/es.json | 3 ++- modules/claim/back/methods/claim/createFromSales.js | 6 ++++++ modules/ticket/front/sale/index.html | 3 ++- modules/ticket/front/sale/index.js | 10 ++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 4a5448d34..e42c13c72 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -212,5 +212,6 @@ "You don't have enough privileges to set this credit amount": "No tienes suficientes privilegios para establecer esta cantidad de crédito", "You can't change the credit set to zero from a manager": "No puedes cambiar el cŕedito establecido a cero por un gerente", "The PDF document does not exists": "El documento PDF no existe. Prueba a regenerarlo desde la opción 'Regenerar PDF factura'", - "The type of business must be filled in basic data": "El tipo de negocio debe estar rellenado en datos básicos" + "The type of business must be filled in basic data": "El tipo de negocio debe estar rellenado en datos básicos", + "You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días" } \ No newline at end of file diff --git a/modules/claim/back/methods/claim/createFromSales.js b/modules/claim/back/methods/claim/createFromSales.js index f22aabbf3..cdbce865b 100644 --- a/modules/claim/back/methods/claim/createFromSales.js +++ b/modules/claim/back/methods/claim/createFromSales.js @@ -57,8 +57,14 @@ module.exports = Self => { } }, myOptions); + const landedPlusWeek = new Date(ticket.landed); + landedPlusWeek.setDate(landedPlusWeek.getDate() + 7); + const isClaimable = landedPlusWeek >= new Date(); + if (ticket.isDeleted) throw new UserError(`You can't create a claim for a removed ticket`); + if (!isClaimable) + throw new UserError(`You can't create a claim from a ticket delivered more than seven days ago`); const newClaim = await Self.create({ ticketFk: ticketId, diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index cbb95d901..0aa41226b 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -469,7 +469,8 @@ + ng-click="$ctrl.createClaim()" + ng-if="$ctrl.isClaimable"> Add claim = new Date(); + } + return false; + } + getSaleTotal(sale) { if (sale.quantity == null || sale.price == null) return null; From 46d7a6e765b8abd44956fffe72f9dadff0725c7f Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 19 Nov 2021 09:24:49 +0100 Subject: [PATCH 2/3] Added unit tests --- .../claim/specs/createFromSales.spec.js | 64 ++++++++++++++----- modules/ticket/front/sale/index.spec.js | 22 ++++++- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/modules/claim/back/methods/claim/specs/createFromSales.spec.js b/modules/claim/back/methods/claim/specs/createFromSales.spec.js index 849ccf8f5..8a48b8613 100644 --- a/modules/claim/back/methods/claim/specs/createFromSales.spec.js +++ b/modules/claim/back/methods/claim/specs/createFromSales.spec.js @@ -1,31 +1,40 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); describe('Claim createFromSales()', () => { - const ticketId = 2; + const ticketId = 16; const newSale = [{ id: 3, instance: 0, quantity: 10 }]; - const ctx = { - req: { - accessToken: {userId: 1}, - headers: {origin: 'localhost:5000'}, - __: () => {} - } + const activeCtx = { + accessToken: {userId: 1}, + headers: {origin: 'localhost:5000'}, + __: () => {} }; + const ctx = { + req: activeCtx + }; + + beforeEach(() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + }); + it('should create a new claim', async() => { - const tx = await app.models.Claim.beginTransaction({}); + const tx = await models.Claim.beginTransaction({}); try { const options = {transaction: tx}; - const claim = await app.models.Claim.createFromSales(ctx, ticketId, newSale, options); + const claim = await models.Claim.createFromSales(ctx, ticketId, newSale, options); expect(claim.ticketFk).toEqual(ticketId); - let claimBeginning = await app.models.ClaimBeginning.findOne({where: {claimFk: claim.id}}, options); + let claimBeginning = await models.ClaimBeginning.findOne({where: {claimFk: claim.id}}, options); expect(claimBeginning.saleFk).toEqual(newSale[0].id); expect(claimBeginning.quantity).toEqual(newSale[0].quantity); @@ -37,17 +46,42 @@ describe('Claim createFromSales()', () => { } }); - it('should not be able to create a claim if exists that sale', async() => { - const tx = await app.models.Claim.beginTransaction({}); + it('should not be able to create a claim for a ticket delivered more than seven days ago', async() => { + const tx = await models.Claim.beginTransaction({}); let error; try { const options = {transaction: tx}; - await app.models.Claim.createFromSales(ctx, ticketId, newSale, options); + const todayMinusEightDays = new Date(); + todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8); - await app.models.Claim.createFromSales(ctx, ticketId, newSale, options); + const ticket = await models.Ticket.findById(ticketId, options); + await ticket.updateAttribute('landed', todayMinusEightDays, options); + + await models.Claim.createFromSales(ctx, ticketId, newSale, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.toString()).toContain(`You can't create a claim from a ticket delivered more than seven days ago`); + }); + + it('should not be able to create a claim if exists that sale', async() => { + const tx = await models.Claim.beginTransaction({}); + + let error; + + try { + const options = {transaction: tx}; + + await models.Claim.createFromSales(ctx, ticketId, newSale, options); + + await models.Claim.createFromSales(ctx, ticketId, newSale, options); await tx.rollback(); } catch (e) { diff --git a/modules/ticket/front/sale/index.spec.js b/modules/ticket/front/sale/index.spec.js index cee1fe5b4..d543c1b81 100644 --- a/modules/ticket/front/sale/index.spec.js +++ b/modules/ticket/front/sale/index.spec.js @@ -15,7 +15,8 @@ describe('Ticket', () => { const ticket = { id: 1, clientFk: 1101, - shipped: 1, + shipped: new Date(), + landed: new Date(), created: new Date(), client: {salesPersonFk: 1}, address: {mobile: 111111111} @@ -74,6 +75,25 @@ describe('Ticket', () => { }); }); + describe('isClaimable() getter', () => { + it('should return true for a ticket delivered less than seven days ago', () => { + const result = controller.isClaimable; + + expect(result).toEqual(true); + }); + + it('should return false for a ticket delivered more than seven days ago', () => { + const ticket = controller.ticket; + const landedMinusEightDays = new Date(ticket.landed); + landedMinusEightDays.setDate(landedMinusEightDays.getDate() - 8); + ticket.landed = landedMinusEightDays; + + const result = controller.isClaimable; + + expect(result).toEqual(false); + }); + }); + describe('getSaleTotal()', () => { it('should return the sale total amount', () => { const sale = { From 60c0943044a658fc16a617610385543828fd2e20 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 19 Nov 2021 09:36:27 +0100 Subject: [PATCH 3/3] Dark theme autofill color --- front/core/components/field/style.scss | 1 + 1 file changed, 1 insertion(+) diff --git a/front/core/components/field/style.scss b/front/core/components/field/style.scss index 399122b1d..91e0c3ec4 100644 --- a/front/core/components/field/style.scss +++ b/front/core/components/field/style.scss @@ -116,6 +116,7 @@ &:active, &:valid { box-shadow: 0 0 0 40px $color-bg-panel inset; + -webkit-text-fill-color: $color-primary-medium } } }