#7127 modify days when adding lines to a claim #3195
|
@ -1931,9 +1931,9 @@ INSERT INTO `vn`.`claimEnd`(`id`, `saleFk`, `claimFk`, `workerFk`, `claimDestina
|
||||||
(1, 31, 4, 21, 2),
|
(1, 31, 4, 21, 2),
|
||||||
(2, 32, 3, 21, 3);
|
(2, 32, 3, 21, 3);
|
||||||
|
|
||||||
INSERT INTO `vn`.`claimConfig`(`id`, `maxResponsibility`, `monthsToRefund`, `minShipped`)
|
INSERT INTO `vn`.`claimConfig`(`id`, `maxResponsibility`, `monthsToRefund`, `minShipped`,`daysToClaim`)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 5, 4, '2016-10-01');
|
(1, 5, 4, '2016-10-01', 7);
|
||||||
|
|
||||||
INSERT INTO `vn`.`claimRatio`(`clientFk`, `yearSale`, `claimAmount`, `claimingRate`, `priceIncreasing`, `packingRate`)
|
INSERT INTO `vn`.`claimRatio`(`clientFk`, `yearSale`, `claimAmount`, `claimingRate`, `priceIncreasing`, `packingRate`)
|
||||||
VALUES
|
VALUES
|
||||||
|
|
|
@ -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';
|
|
@ -4,7 +4,7 @@ const LoopBackContext = require('loopback-context');
|
||||||
describe('ClaimBeginning model()', () => {
|
describe('ClaimBeginning model()', () => {
|
||||||
const claimFk = 1;
|
const claimFk = 1;
|
||||||
const activeCtx = {
|
const activeCtx = {
|
||||||
accessToken: {userId: 18},
|
accessToken: {userId: 72},
|
||||||
headers: {origin: 'localhost:5000'},
|
headers: {origin: 'localhost:5000'},
|
||||||
__: () => {}
|
__: () => {}
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,22 +3,18 @@ const LoopBackContext = require('loopback-context');
|
||||||
|
|
||||||
describe('Claim createFromSales()', () => {
|
describe('Claim createFromSales()', () => {
|
||||||
const ticketId = 23;
|
const ticketId = 23;
|
||||||
const newSale = [{
|
const newSale = [{id: 31, instance: 0, quantity: 10}];
|
||||||
id: 31,
|
let activeCtx;
|
||||||
instance: 0,
|
let ctx;
|
||||||
quantity: 10
|
|
||||||
}];
|
beforeEach(() => {
|
||||||
const activeCtx = {
|
activeCtx = {
|
||||||
accessToken: {userId: 1},
|
accessToken: {userId: 72},
|
||||||
headers: {origin: 'localhost:5000'},
|
headers: {origin: 'localhost:5000'},
|
||||||
__: () => {}
|
__: () => {}
|
||||||
};
|
};
|
||||||
|
ctx = {req: activeCtx};
|
||||||
|
|
||||||
const ctx = {
|
|
||||||
req: activeCtx
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||||
active: activeCtx
|
active: activeCtx
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
const LoopBackContext = require('loopback-context');
|
const LoopBackContext = require('loopback-context');
|
||||||
|
const moment = require('moment');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/claim-beginning/importToNewRefundTicket')(Self);
|
require('../methods/claim-beginning/importToNewRefundTicket')(Self);
|
||||||
|
@ -13,8 +14,31 @@ module.exports = Self => {
|
||||||
const options = ctx.options;
|
const options = ctx.options;
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const saleFk = ctx?.currentInstance?.saleFk || ctx?.instance?.saleFk;
|
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);
|
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) {
|
if (ctx.isNewInstance) {
|
||||||
const claim = await models.Claim.findById(ctx.instance.claimFk, {fields: ['ticketFk']}, options);
|
const claim = await models.Claim.findById(ctx.instance.claimFk, {fields: ['ticketFk']}, options);
|
||||||
if (sale.ticketFk != claim.ticketFk)
|
if (sale.ticketFk != claim.ticketFk)
|
||||||
|
|
|
@ -30,7 +30,6 @@ module.exports = Self => {
|
||||||
SELECT
|
SELECT
|
||||||
s.id AS saleFk,
|
s.id AS saleFk,
|
||||||
t.id AS ticketFk,
|
t.id AS ticketFk,
|
||||||
t.landed,
|
|
||||||
s.concept,
|
s.concept,
|
||||||
s.itemFk,
|
s.itemFk,
|
||||||
s.quantity,
|
s.quantity,
|
||||||
|
@ -41,11 +40,10 @@ module.exports = Self => {
|
||||||
INNER JOIN vn.sale s ON s.ticketFk = t.id
|
INNER JOIN vn.sale s ON s.ticketFk = t.id
|
||||||
LEFT JOIN vn.claimBeginning cb ON cb.saleFk = s.id
|
LEFT JOIN vn.claimBeginning cb ON cb.saleFk = s.id
|
||||||
|
|
||||||
WHERE (t.landed) >= TIMESTAMPADD(DAY, -7, ?)
|
WHERE t.id = ?
|
||||||
AND t.id = ? AND cb.id IS NULL
|
AND cb.id IS NULL`;
|
||||||
ORDER BY t.landed DESC, t.id DESC`;
|
|
||||||
|
|
||||||
const claimableSales = await Self.rawSql(query, [date, ticketFk], myOptions);
|
const claimableSales = await Self.rawSql(query, [ticketFk], myOptions);
|
||||||
|
|
||||||
return claimableSales;
|
return claimableSales;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
esto seria incorrecto.
Los permisos se consultan mirando los acls
ejmplo
d1659ce04c/modules/claim/back/methods/claim/createFromSales.js (L64)