const LoopBackContext = require('loopback-context'); module.exports = Self => { // Methods require('./ticket-methods')(Self); Self.observe('before save', async function(ctx) { const loopBackContext = LoopBackContext.getCurrentContext(); const httpCtx = loopBackContext.active; if (ctx.isNewInstance) return; let changes = ctx.data || ctx.instance; if (changes.routeFk === null && ctx.currentInstance.routeFk != null) { let instance = JSON.parse(JSON.stringify(ctx.currentInstance)); let userId = httpCtx.accessToken.userId; let logRecord = { originFk: ctx.currentInstance.routeFk, userFk: userId, action: 'delete', changedModel: 'Route', oldInstance: {ticket: instance.id}, newInstance: null }; await Self.app.models.RouteLog.create(logRecord); } }); };