2023-11-02 15:25:25 +00:00
|
|
|
module.exports = Self => {
|
2023-11-09 06:56:35 +00:00
|
|
|
Self.observe('after save', async ctx => {
|
2023-06-22 06:56:27 +00:00
|
|
|
const instance = ctx.data || ctx.instance;
|
2023-05-10 11:22:53 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const options = ctx.options;
|
2024-04-05 06:04:53 +00:00
|
|
|
const notificationName = 'backup-printer-selected';
|
2024-04-09 07:30:28 +00:00
|
|
|
const userId = ctx.options.accessToken?.userId || instance.workerFk;
|
2023-05-10 11:22:53 +00:00
|
|
|
|
2023-06-22 06:56:27 +00:00
|
|
|
if (!instance?.sectorFk || !instance?.labelerFk) return;
|
2024-04-09 07:30:28 +00:00
|
|
|
|
2023-05-10 11:22:53 +00:00
|
|
|
const sector = await models.Sector.findById(instance.sectorFk, {
|
2023-11-09 06:56:35 +00:00
|
|
|
fields: ['backupPrinterFk']
|
2023-05-10 11:22:53 +00:00
|
|
|
}, options);
|
2023-05-29 10:55:20 +00:00
|
|
|
|
2023-11-09 06:56:35 +00:00
|
|
|
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
|
2024-04-05 06:04:53 +00:00
|
|
|
const {labelerFk, sectorFk} = instance;
|
|
|
|
|
2024-04-09 11:34:33 +00:00
|
|
|
const {backupPrinterNotificationDelay} = await models.ProductionConfig.findOne();
|
|
|
|
if (backupPrinterNotificationDelay) {
|
2024-04-09 07:30:28 +00:00
|
|
|
const notifications = await models.NotificationQueue.find(
|
2024-05-03 10:08:22 +00:00
|
|
|
{where: {created: {gte: new Date(Date.vnNow() - (backupPrinterNotificationDelay * 1000))},
|
2024-04-09 11:34:33 +00:00
|
|
|
notificationFk: notificationName,
|
2024-06-03 13:01:06 +00:00
|
|
|
status: {neq: 'error'}
|
2024-04-09 11:34:33 +00:00
|
|
|
}
|
2024-04-09 07:30:28 +00:00
|
|
|
});
|
2024-04-05 06:04:53 +00:00
|
|
|
|
|
|
|
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-05 06:04:53 +00:00
|
|
|
});
|
|
|
|
|
2024-05-03 10:33:59 +00:00
|
|
|
if (filteredNotifications.length >= 1) return;
|
2024-04-05 06:04:53 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 07:30:28 +00:00
|
|
|
await models.NotificationQueue.create({
|
2024-04-05 06:04:53 +00:00
|
|
|
notificationFk: notificationName,
|
2023-12-19 09:51:18 +00:00
|
|
|
authorFk: userId,
|
|
|
|
params: JSON.stringify(
|
|
|
|
{
|
|
|
|
'labelerId': instance.labelerFk,
|
|
|
|
'sectorId': instance.sectorFk,
|
|
|
|
'workerId': userId
|
|
|
|
}
|
|
|
|
)
|
2024-04-05 06:04:53 +00:00
|
|
|
});
|
2023-05-10 11:22:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2024-04-05 06:04:53 +00:00
|
|
|
|