const LoopBackContext = require('loopback-context'); const UserError = require('vn-loopback/util/user-error'); module.exports = function(Self) { Self.observe('before save', async function(ctx) { let isStowaway = await Self.app.models.Ticket.canBeStowawayed(ctx.instance.id); if (!isStowaway) throw new UserError(`Can't create stowaway for this ticket`); if (ctx.isNewInstance) { let where = { code: 'BOARDING' }; let state = await Self.app.models.State.findOne({where}); let params = {ticketFk: ctx.instance.id, stateFk: state.id}; const loopBackContext = LoopBackContext.getCurrentContext(); let httpCtx = {req: loopBackContext.active}; await Self.app.models.TicketTracking.changeState(httpCtx, params); } }); };