const LoopBackContext = require('loopback-context'); module.exports = function(Self) { Self.observe('before save', async function(ctx) { 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); } }); };