diff --git a/modules/claim/back/methods/claim/createFromSales.js b/modules/claim/back/methods/claim/createFromSales.js index cdbce865b..ba7bda71d 100644 --- a/modules/claim/back/methods/claim/createFromSales.js +++ b/modules/claim/back/methods/claim/createFromSales.js @@ -59,11 +59,12 @@ module.exports = Self => { const landedPlusWeek = new Date(ticket.landed); landedPlusWeek.setDate(landedPlusWeek.getDate() + 7); + const hasClaimManagerRole = await models.Account.hasRole(userId, 'claimManager', myOptions); const isClaimable = landedPlusWeek >= new Date(); if (ticket.isDeleted) throw new UserError(`You can't create a claim for a removed ticket`); - if (!isClaimable) + if (!isClaimable && !hasClaimManagerRole) throw new UserError(`You can't create a claim from a ticket delivered more than seven days ago`); const newClaim = await Self.create({ diff --git a/modules/claim/back/methods/claim/specs/createFromSales.spec.js b/modules/claim/back/methods/claim/specs/createFromSales.spec.js index 8a48b8613..097dcc0d9 100644 --- a/modules/claim/back/methods/claim/specs/createFromSales.spec.js +++ b/modules/claim/back/methods/claim/specs/createFromSales.spec.js @@ -46,9 +46,40 @@ describe('Claim createFromSales()', () => { } }); + it('should be able to create a claim for a ticket delivered more than seven days ago as claimManager', async() => { + const tx = await models.Claim.beginTransaction({}); + const claimManagerId = 72; + activeCtx.accessToken.userId = claimManagerId; + + try { + const options = {transaction: tx}; + + const todayMinusEightDays = new Date(); + todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8); + + const ticket = await models.Ticket.findById(ticketId, options); + await ticket.updateAttribute('landed', todayMinusEightDays, options); + + const claim = await models.Claim.createFromSales(ctx, ticketId, newSale, options); + + expect(claim.ticketFk).toEqual(ticketId); + + const claimBeginning = await models.ClaimBeginning.findOne({where: {claimFk: claim.id}}, options); + + expect(claimBeginning.saleFk).toEqual(newSale[0].id); + expect(claimBeginning.quantity).toEqual(newSale[0].quantity); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + 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({}); + activeCtx.accessToken.userId = 1; let error; try { diff --git a/modules/ticket/front/sale/index.js b/modules/ticket/front/sale/index.js index 9d0f71eb8..54167ce96 100644 --- a/modules/ticket/front/sale/index.js +++ b/modules/ticket/front/sale/index.js @@ -40,7 +40,9 @@ class Controller extends Section { const landedPlusWeek = new Date(this.ticket.landed); landedPlusWeek.setDate(landedPlusWeek.getDate() + 7); - return landedPlusWeek >= new Date(); + const hasClaimManagerRole = this.aclService.hasAny(['claimManager']); + + return landedPlusWeek >= new Date() || hasClaimManagerRole; } return false; }