salix/modules/worker/back/models/operator.js

55 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-11-02 15:25:25 +00:00
module.exports = Self => {
Self.observe('after save', async ctx => {
2023-06-22 06:56:27 +00:00
const instance = ctx.data || ctx.instance;
const models = Self.app.models;
const options = ctx.options;
const notificationName = 'backup-printer-selected';
2024-04-09 07:30:28 +00:00
const userId = ctx.options.accessToken?.userId || instance.workerFk;
2023-06-22 06:56:27 +00:00
if (!instance?.sectorFk || !instance?.labelerFk) return;
2024-04-09 07:30:28 +00:00
const sector = await models.Sector.findById(instance.sectorFk, {
fields: ['backupPrinterFk']
}, options);
2023-05-29 10:55:20 +00:00
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
const {labelerFk, sectorFk} = instance;
2024-04-09 07:30:28 +00:00
const [{delay}] = await models.Notification.find({where: {name: notificationName}});
if (delay) {
2024-04-09 07:30:28 +00:00
const now = Date.vnNow() - (delay * 1000) + (3600 * 1000);
const notifications = await models.NotificationQueue.find(
{where:
{created: {gte: now},
notificationFk: notificationName,
status: 'sent'
}
});
const criteria = {labelerId: labelerFk, sectorId: sectorFk};
const filteredNotifications = notifications.filter(notification => {
const paramsObj = JSON.parse(notification.params);
2024-04-09 07:30:28 +00:00
return Object.keys(criteria).every(key => criteria[key] === paramsObj?.[key]);
});
2024-04-09 07:30:28 +00:00
if (filteredNotifications.length >= 1)
throw new Error('Previous notification sended with the same parameters');
}
2024-04-09 07:30:28 +00:00
await models.NotificationQueue.create({
notificationFk: notificationName,
authorFk: userId,
params: JSON.stringify(
{
'labelerId': instance.labelerFk,
'sectorId': instance.sectorFk,
'workerId': userId
}
)
});
}
});
};