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