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 isEditable = await Self.app.models.Claim.isEditable(httpCtx, ctx.where.id); if (!isEditable) throw new UserError(`The current claim can't be modified`); } };