const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.observe('before save', async ctx => { let changes = ctx.currentInstance || ctx.instance; if (changes) { let ticketId = changes.ticketFk; let ticket = await Self.app.models.Ticket.findById(ticketId); if (ticket.refFk) throw new UserError('You cannot add or modify services to an invoiced ticket'); } }); };