feat(noSpam): refs #6005 check if exists a previous notification

This commit is contained in:
Pablo Natek 2023-11-23 08:22:24 +01:00
parent 570ce97cc3
commit 1b4ce1a0b5
5 changed files with 75 additions and 21 deletions

View File

@ -18,6 +18,9 @@
},
"description": {
"type": "string"
},
"delay": {
"type": "number"
}
},
"relations": {

View File

@ -1,3 +1,6 @@
ALTER TABLE `util`.`notification` ADD delay INT NULL
COMMENT 'Minimum Milliseconds Interval to Prevent Spam from Same-Type Notifications';
ALTER TABLE `vn`.`sector` CHANGE `mainPrinterFk` `backupPrinterFk` tinyint(3) unsigned DEFAULT NULL NULL;
ALTER TABLE `util`.`notificationSubscription` DROP FOREIGN KEY `notificationSubscription_ibfk_1`;
@ -16,4 +19,5 @@ DELETE FROM `util`.`notification`
INSERT INTO `util`.`notification`
SET `id` = 15,
`name` = 'backup-printer-selected',
`description` = 'The worker has selected the backup printer for their sector';
`description` = 'The worker has selected the backup printer for their sector',
`delay` = 600000;

View File

@ -2782,17 +2782,19 @@ INSERT INTO `vn`.`packingSiteConfig` (`shinobiUrl`, `shinobiToken`, `shinobiGrou
('', 'SHINNOBI_TOKEN', 'GROUP_TOKEN', 6000);
INSERT INTO `util`.`notificationConfig`
SET `cleanDays` = 90;
INSERT INTO `util`.`notification` (`id`, `name`, `description`)
INSERT IGNORE INTO `util`.`notification` (`id`, `name`, `description`, `delay`)
VALUES
(1, 'print-email', 'notification fixture one'),
(2, 'invoice-electronic', 'A electronic invoice has been generated'),
(4, 'supplier-pay-method-update', 'A supplier pay method has been updated'),
(5, 'modified-entry', 'An entry has been modified'),
(6, 'book-entry-deleted', 'accounting entries deleted');
(1, 'print-email', 'notification fixture one', NULL),
(2, 'invoice-electronic', 'A electronic invoice has been generated', NULL),
(3, 'backup-printer-selected', 'A printer distinct than main has been configured', 600000),
(4, 'supplier-pay-method-update', 'A supplier pay method has been updated', NULL),
(5, 'modified-entry', 'An entry has been modified', NULL),
(6, 'book-entry-deleted', 'accounting entries deleted', NULL);
INSERT IGNORE INTO `util`.`notification` (`id`, `name`, `description`)
VALUES (3, 'backup-printer-selected', 'A printer distinct than main has been configured');
UPDATE `util`.`notification`
SET `id` = 3
WHERE `name` = 'backup-printer-selected';
INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
VALUES

View File

@ -58,4 +58,32 @@ describe('Operator', () => {
throw e;
}
});
it('should not create notification when is already notified', async() => {
const tx = await models.Operator.beginTransaction({});
try {
const options = {transaction: tx, accessToken: {userId: authorFk}};
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': 10, 'sectorId': 10, 'workerId': 10}),
created: Date.vnNow(),
}, options);
const notificationQueue = await createOperator(1, options);
const params = JSON.parse(notificationQueue.params);
expect(notificationQueue.notificationFk).toEqual(notificationName);
expect(notificationQueue.authorFk).toEqual(1);
expect(params.labelerId).toEqual(10);
expect(params.sectorId).toEqual(10);
expect(params.workerId).toEqual(10);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -3,6 +3,7 @@ module.exports = function(Self) {
const instance = ctx.data || ctx.instance;
const models = Self.app.models;
const options = ctx.options;
const notification = 'backup-printer-selected';
if (!instance?.sectorFk || !instance?.labelerFk) return;
@ -12,17 +13,33 @@ module.exports = function(Self) {
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
const {userId} = ctx.options.accessToken;
await models.NotificationQueue.create({
notificationFk: 'backup-printer-selected',
authorFk: userId,
params: JSON.stringify(
{
'labelerId': instance.labelerFk,
'sectorId': instance.sectorFk,
'workerId': userId
}
)
}, options);
const {delay} = await models.Notification.findOne({
where: {name: notification}
});
const hasNotified = await models.NotificationQueue.findOne({
where: {
notificationFk: notification,
and: [
{params: {like: '%\"labelerId\":' + instance.labelerFk + '%'}},
{params: {like: '%\"sectorId\":' + instance.sectorFk + '%'}}
]
},
order: 'CREATED DESC',
});
if (hasNotified?.created - Date.now() > delay || !hasNotified?.created) {
await models.NotificationQueue.create({
notificationFk: notification,
authorFk: userId,
params: JSON.stringify(
{
'labelerId': instance.labelerFk,
'sectorId': instance.sectorFk,
'workerId': userId
}
)
}, options);
}
}
});
};