107 lines
3.8 KiB
JavaScript
107 lines
3.8 KiB
JavaScript
|
|
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);
|
|
|
|
Self.validatesUniquenessOf('saleFk', {
|
|
message: `A claim with that sale already exists`
|
|
});
|
|
|
|
Self.observe('before save', async ctx => {
|
|
const options = ctx.options;
|
|
const models = Self.app.models;
|
|
const saleFk = ctx?.currentInstance?.saleFk || ctx?.instance?.saleFk;
|
|
const claimFk = ctx?.instance?.claimFk || ctx?.currentInstance?.claimFk;
|
|
const myOptions = {};
|
|
const accessToken = ctx?.options?.accessToken || LoopBackContext.getCurrentContext().active.accessToken;
|
|
const ctxToken = {req: {accessToken}};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const sale = await models.Sale.findById(saleFk, {fields: ['ticketFk', 'quantity']}, options);
|
|
|
|
const canCreateClaimAfterDeadline = models.ACL.checkAccessAcl(
|
|
ctxToken,
|
|
'Claim',
|
|
'createAfterDeadline',
|
|
myOptions
|
|
);
|
|
|
|
const canUpdateClaim = models.ACL.checkAccessAcl(
|
|
ctxToken,
|
|
'Claim',
|
|
'updateClaim',
|
|
myOptions
|
|
);
|
|
|
|
if (!canUpdateClaim && !canCreateClaimAfterDeadline)
|
|
throw new UserError(`You don't have permission to modify this claim`);
|
|
|
|
if (canUpdateClaim) {
|
|
const query = `
|
|
SELECT daysToClaim
|
|
FROM vn.claimConfig`;
|
|
const res = await Self.rawSql(query);
|
|
const daysToClaim = res[0]?.daysToClaim;
|
|
|
|
const claim = await models.Claim.findById(claimFk, {fields: ['created']}, options);
|
|
const claimDate = moment.utc(claim.created);
|
|
const currentDate = moment.utc();
|
|
const daysSinceSale = currentDate.diff(claimDate, 'days');
|
|
|
|
if (daysSinceSale > daysToClaim && !canCreateClaimAfterDeadline)
|
|
throw new UserError(`You can't modify this claim because the deadline has already passed`);
|
|
}
|
|
|
|
if (ctx.isNewInstance) {
|
|
const claim = await models.Claim.findById(claimFk, {fields: ['ticketFk']}, options);
|
|
if (sale.ticketFk != claim.ticketFk)
|
|
throw new UserError(`Cannot create a new claimBeginning from a different ticket`);
|
|
}
|
|
|
|
await claimIsEditable(ctx);
|
|
|
|
if (sale?.quantity && ctx.data?.quantity && ctx.data.quantity > sale?.quantity)
|
|
throw new UserError('The quantity claimed cannot be greater than the quantity of the line');
|
|
});
|
|
|
|
Self.observe('before delete', async ctx => {
|
|
if (ctx.isNewInstance) return;
|
|
await claimIsEditable(ctx);
|
|
});
|
|
|
|
async function claimIsEditable(ctx) {
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
const httpCtx = {req: loopBackContext.active};
|
|
const models = Self.app.models;
|
|
const myOptions = {};
|
|
|
|
if (ctx.options && ctx.options.transaction)
|
|
myOptions.transaction = ctx.options.transaction;
|
|
|
|
const claimBeginning = ctx.instance ?? await Self.findById(ctx?.where?.id);
|
|
|
|
const filter = {
|
|
where: {id: claimBeginning.claimFk},
|
|
include: [
|
|
{
|
|
relation: 'claimState',
|
|
scope: {
|
|
fields: ['id', 'code', 'description']
|
|
}
|
|
}
|
|
]
|
|
};
|
|
|
|
const [claim] = await models.Claim.find(filter, myOptions);
|
|
const isEditable = await models.ClaimState.isEditable(httpCtx, claim.claimState().id);
|
|
|
|
if (!isEditable)
|
|
throw new UserError(`The current claim can't be modified`);
|
|
}
|
|
};
|