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 {
|
2020-02-20 14:15:50 +00:00
|
|
|
await Self.rawSql(`
|
|
|
|
CREATE EVENT zoneClosure_doRecalc
|
|
|
|
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 15 SECOND
|
|
|
|
DO CALL zoneClosure_recalc;
|
|
|
|
`);
|
2020-02-06 15:04:23 +00:00
|
|
|
} catch (err) {
|
2020-02-20 14:15:50 +00:00
|
|
|
if (err.code != 'ER_EVENT_ALREADY_EXISTS') throw err;
|
2020-02-06 15:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|