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

46 lines
1.7 KiB
JavaScript
Raw Normal View History

module.exports = function(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 notification = 'backup-printer-selected';
2023-06-22 06:56:27 +00:00
if (!instance?.sectorFk || !instance?.labelerFk) return;
2023-05-29 10:55:20 +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 {userId} = ctx.options.accessToken;
const {delay} = await models.Notification.findOne({
where: {name: notification}
}, options);
const hasNotified = await models.NotificationQueue.findOne({
where: {
notificationFk: notification,
and: [
{params: {like: '%\"labelerId\":' + instance.labelerFk + '%'}},
{params: {like: '%\"sectorId\":' + instance.sectorFk + '%'}}
]
},
order: 'created DESC',
}, options);
if (hasNotified?.created - Date.now() > delay || !hasNotified?.created || !delay) {
await models.NotificationQueue.create({
notificationFk: notification,
authorFk: userId,
params: JSON.stringify(
{
'labelerId': instance.labelerFk,
'sectorId': instance.sectorFk,
'workerId': userId
}
)
}, options);
}
}
});
};