salix/modules/claim/back/models/claim-beginning.js

89 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-06-16 05:47:46 +00:00
const UserError = require('vn-loopback/util/user-error');
const LoopBackContext = require('loopback-context');
const moment = require('moment');
2020-06-16 05:47:46 +00:00
2018-08-30 07:19:09 +00:00
module.exports = Self => {
require('../methods/claim-beginning/importToNewRefundTicket')(Self);
2018-08-30 07:19:09 +00:00
Self.validatesUniquenessOf('saleFk', {
message: `A claim with that sale already exists`
});
2020-06-16 10:00:38 +00:00
2020-06-16 05:47:46 +00:00
Self.observe('before save', async ctx => {
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)
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');
2020-06-16 05:47:46 +00:00
});
2020-06-16 10:00:38 +00:00
2020-06-16 05:47:46 +00:00
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};
2023-02-07 14:40:03 +00:00
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);
2023-03-07 09:20:10 +00:00
2023-02-07 14:40:03 +00:00
const filter = {
2023-03-07 09:20:10 +00:00
where: {id: claimBeginning.claimFk},
include: [
{
relation: 'claimState',
scope: {
fields: ['id', 'code', 'description']
}
}
]
};
2023-02-07 14:40:03 +00:00
const [claim] = await models.Claim.find(filter, myOptions);
2023-03-08 06:45:22 +00:00
const isEditable = await models.ClaimState.isEditable(httpCtx, claim.claimState().id);
2020-06-16 05:47:46 +00:00
if (!isEditable)
throw new UserError(`The current claim can't be modified`);
}
2018-08-30 07:19:09 +00:00
};