From 9113f2e3e568b2d4b5e9751535c659ace503d81b Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 13 Nov 2024 08:30:09 +0100 Subject: [PATCH] feat: refs #7127 modify days when adding lines to a claim --- db/dump/fixtures.before.sql | 4 ++-- .../11334-grayRoebelini/00-firstScript.sql | 2 ++ .../specs/claim-beginning.spec.js | 2 +- .../claim/specs/createFromSales.spec.js | 24 ++++++++----------- modules/claim/back/models/claim-beginning.js | 24 +++++++++++++++++++ .../methods/sale/getClaimableFromTicket.js | 8 +++---- 6 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 db/versions/11334-grayRoebelini/00-firstScript.sql diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 8544686e8..4c13b37cc 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -1931,9 +1931,9 @@ INSERT INTO `vn`.`claimEnd`(`id`, `saleFk`, `claimFk`, `workerFk`, `claimDestina (1, 31, 4, 21, 2), (2, 32, 3, 21, 3); -INSERT INTO `vn`.`claimConfig`(`id`, `maxResponsibility`, `monthsToRefund`, `minShipped`) +INSERT INTO `vn`.`claimConfig`(`id`, `maxResponsibility`, `monthsToRefund`, `minShipped`,`daysToClaim`) VALUES - (1, 5, 4, '2016-10-01'); + (1, 5, 4, '2016-10-01', 7); INSERT INTO `vn`.`claimRatio`(`clientFk`, `yearSale`, `claimAmount`, `claimingRate`, `priceIncreasing`, `packingRate`) VALUES diff --git a/db/versions/11334-grayRoebelini/00-firstScript.sql b/db/versions/11334-grayRoebelini/00-firstScript.sql new file mode 100644 index 000000000..5eb107694 --- /dev/null +++ b/db/versions/11334-grayRoebelini/00-firstScript.sql @@ -0,0 +1,2 @@ +-- Place your SQL code here +ALTER TABLE vn.claimConfig ADD IF NOT EXISTS daysToClaim int(11) NOT NULL DEFAULT 7 COMMENT 'Dias para reclamar'; diff --git a/modules/claim/back/methods/claim-beginning/specs/claim-beginning.spec.js b/modules/claim/back/methods/claim-beginning/specs/claim-beginning.spec.js index b7974ad23..8b56f3a1c 100644 --- a/modules/claim/back/methods/claim-beginning/specs/claim-beginning.spec.js +++ b/modules/claim/back/methods/claim-beginning/specs/claim-beginning.spec.js @@ -4,7 +4,7 @@ const LoopBackContext = require('loopback-context'); describe('ClaimBeginning model()', () => { const claimFk = 1; const activeCtx = { - accessToken: {userId: 18}, + accessToken: {userId: 72}, headers: {origin: 'localhost:5000'}, __: () => {} }; diff --git a/modules/claim/back/methods/claim/specs/createFromSales.spec.js b/modules/claim/back/methods/claim/specs/createFromSales.spec.js index 25414d1db..75caf278e 100644 --- a/modules/claim/back/methods/claim/specs/createFromSales.spec.js +++ b/modules/claim/back/methods/claim/specs/createFromSales.spec.js @@ -3,22 +3,18 @@ const LoopBackContext = require('loopback-context'); describe('Claim createFromSales()', () => { const ticketId = 23; - const newSale = [{ - id: 31, - instance: 0, - quantity: 10 - }]; - const activeCtx = { - accessToken: {userId: 1}, - headers: {origin: 'localhost:5000'}, - __: () => {} - }; - - const ctx = { - req: activeCtx - }; + const newSale = [{id: 31, instance: 0, quantity: 10}]; + let activeCtx; + let ctx; beforeEach(() => { + activeCtx = { + accessToken: {userId: 72}, + headers: {origin: 'localhost:5000'}, + __: () => {} + }; + ctx = {req: activeCtx}; + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); diff --git a/modules/claim/back/models/claim-beginning.js b/modules/claim/back/models/claim-beginning.js index 3dc9261c3..40d66c33e 100644 --- a/modules/claim/back/models/claim-beginning.js +++ b/modules/claim/back/models/claim-beginning.js @@ -1,6 +1,7 @@ const UserError = require('vn-loopback/util/user-error'); const LoopBackContext = require('loopback-context'); +const moment = require('moment'); module.exports = Self => { require('../methods/claim-beginning/importToNewRefundTicket')(Self); @@ -13,8 +14,31 @@ module.exports = Self => { const options = ctx.options; const models = Self.app.models; const saleFk = ctx?.currentInstance?.saleFk || ctx?.instance?.saleFk; + const loopBackContext = LoopBackContext.getCurrentContext(); + const accessToken = loopBackContext.active.accessToken; + const user = await models.VnUser.findById(accessToken.userId); + const role = await models.VnRole.findById(user.roleFk); const sale = await models.Sale.findById(saleFk, {fields: ['ticketFk', 'quantity']}, options); + if (role.name !== 'salesPerson' && role.name !== 'claimManager') + throw new UserError(`You don't have permission to modify this claim`); + + if (role.name === 'salesPerson') { + const query = ` + SELECT daysToClaim + FROM vn.claimConfig`; + const res = await Self.rawSql(query); + const daysToClaim = res[0]?.daysToClaim; + + const claim = await models.Claim.findById(ctx?.currentInstance?.claimFk, {fields: ['created']}, options); + const claimDate = moment.utc(claim.created); + const currentDate = moment.utc(); + const daysSinceSale = currentDate.diff(claimDate, 'days'); + + if (daysSinceSale > daysToClaim) + throw new UserError(`You can't modify this claim because the deadline has already passed`); + } + if (ctx.isNewInstance) { const claim = await models.Claim.findById(ctx.instance.claimFk, {fields: ['ticketFk']}, options); if (sale.ticketFk != claim.ticketFk) diff --git a/modules/ticket/back/methods/sale/getClaimableFromTicket.js b/modules/ticket/back/methods/sale/getClaimableFromTicket.js index c51781f59..cb10bdb0e 100644 --- a/modules/ticket/back/methods/sale/getClaimableFromTicket.js +++ b/modules/ticket/back/methods/sale/getClaimableFromTicket.js @@ -30,7 +30,6 @@ module.exports = Self => { SELECT s.id AS saleFk, t.id AS ticketFk, - t.landed, s.concept, s.itemFk, s.quantity, @@ -41,11 +40,10 @@ module.exports = Self => { INNER JOIN vn.sale s ON s.ticketFk = t.id LEFT JOIN vn.claimBeginning cb ON cb.saleFk = s.id - WHERE (t.landed) >= TIMESTAMPADD(DAY, -7, ?) - AND t.id = ? AND cb.id IS NULL - ORDER BY t.landed DESC, t.id DESC`; + WHERE t.id = ? + AND cb.id IS NULL`; - const claimableSales = await Self.rawSql(query, [date, ticketFk], myOptions); + const claimableSales = await Self.rawSql(query, [ticketFk], myOptions); return claimableSales; };