salix/back/models/notificationQueue.js

30 lines
1.0 KiB
JavaScript

module.exports = Self => {
Self.observe('before save', async ctx => {
const instance = ctx.data || ctx.instance;
const {notificationFk} = instance;
if (!(notificationFk === 'backup-printer-selected')) return;
const {models} = Self.app;
const params = JSON.parse(instance.params);
const options = ctx.options;
const {delay} = await models.Notification.findOne({
where: {name: notificationFk}
}, options);
const hasNotified = await models.NotificationQueue.findOne({
where: {
notificationFk: notificationFk,
and: [
{params: {like: '%\"labelerId\":' + params.labelerId + '%'}},
{params: {like: '%\"sectorId\":' + params.sectorId + '%'}}
]
},
order: 'created DESC',
}, options);
if (hasNotified?.created - Date.now() > delay || !hasNotified?.created || !delay) return;
throw new Error('Is already Notified');
});
};