salix/modules/agency/back/models/zone-calc-ticket.js

25 lines
657 B
JavaScript
Raw Normal View History

2020-02-06 15:04:23 +00:00
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;
}
}
});
};