const app = require('vn-loopback/server/server'); module.exports = Self => { app.on('started', function() { let models = ['Zone', 'ZoneEvent', 'ZoneExclusion']; for (let modelName of models) { let Model = app.models[modelName]; Model.observe('after save', doCalc); Model.observe('after delete', doCalc); } async function doCalc(ctx) { try { await Self.create({zoneFk: ctx.instance.zoneFk || ctx.instance.id}); } catch (err) { if (err.code != 'ER_DUP_ENTRY') throw err; } } }); };