2020-06-16 05:47:46 +00:00
|
|
|
|
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
2018-08-30 07:19:09 +00:00
|
|
|
module.exports = Self => {
|
2018-09-25 12:39:08 +00:00
|
|
|
require('../methods/claim-beginning/importToNewRefundTicket')(Self);
|
2018-10-02 07:52:38 +00:00
|
|
|
|
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 => {
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
await claimIsEditable(ctx);
|
|
|
|
});
|
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};
|
|
|
|
const isEditable = await Self.app.models.Claim.isEditable(httpCtx, ctx.where.id);
|
|
|
|
|
|
|
|
if (!isEditable)
|
|
|
|
throw new UserError(`The current claim can't be modified`);
|
|
|
|
}
|
2018-08-30 07:19:09 +00:00
|
|
|
};
|