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

56 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-11-02 15:25:25 +00:00
module.exports = Self => {
Self.observe('after save', async ctx => {
console.log('entra en after save');
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';
const userId = ctx.options.accessToken?.userId;
2023-06-22 06:56:27 +00:00
if (!instance?.sectorFk || !instance?.labelerFk) return;
console.log('instance.sectorFk: ', instance.sectorFk);
const sector = await models.Sector.findById(instance.sectorFk, {
fields: ['backupPrinterFk']
}, options);
2023-05-29 10:55:20 +00:00
console.log('sector.backupPrinterFk == instance.labelerFk: ', sector.backupPrinterFk == instance.labelerFk);
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
console.log('entra');
const {labelerFk, sectorFk} = instance;
const [{delay}] = await models.Notification.find({where: {name: notificationName}}, options);
if (delay) {
const now = Date.vnNow();
const filter = {where: {created: {between: [now - (delay * 1000), now]}}};
const notifications = await models.NotificationQueue.find(filter, options);
console.log('notifications: ', notifications);
const criteria = {labelerId: labelerFk, sectorId: sectorFk};
const filteredNotifications = notifications.filter(notification => {
const paramsObj = JSON.parse(notification.params);
console.log('paramsObj: ', paramsObj);
return Object.keys(criteria).every(key => criteria[key] === paramsObj[key]);
});
console.log('filteredNotifications.length: ', filteredNotifications.length);
if (filteredNotifications.length > 1)
throw new Error('Previous notification sended with the same parameters');
}
const created = await models.NotificationQueue.create({
notificationFk: notificationName,
authorFk: userId,
params: JSON.stringify(
{
'labelerId': instance.labelerFk,
'sectorId': instance.sectorFk,
'workerId': userId
}
)
});
console.log('created: ', created);
}
});
};