2023-01-24 08:20:02 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
2020-02-06 15:04:23 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
app.on('started', function() {
|
|
|
|
let models = ['Zone', 'ZoneEvent', 'ZoneExclusion'];
|
|
|
|
|
|
|
|
for (let modelName of models) {
|
2023-01-24 08:20:02 +00:00
|
|
|
let Model = app.models[modelName];
|
2020-02-06 15:04:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|