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

32 lines
1.0 KiB
JavaScript

const UserError = require('vn-loopback/util/user-error');
const LoopBackContext = require('loopback-context');
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 => {
if (ctx.isNewInstance) return;
await claimIsEditable(ctx);
});
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 claimBeginning = await Self.findById(ctx.where.id);
const isEditable = await Self.app.models.Claim.isEditable(httpCtx, claimBeginning.claimFk);
if (!isEditable)
throw new UserError(`The current claim can't be modified`);
}
};