From 1b4ce1a0b5780d0454d0e52a15c4786877643450 Mon Sep 17 00:00:00 2001 From: pablone Date: Thu, 23 Nov 2023 08:22:24 +0100 Subject: [PATCH] feat(noSpam): refs #6005 check if exists a previous notification --- back/models/notification.json | 3 ++ .../00-sectorBackUpLabelerFk.sql | 6 ++- db/dump/fixtures.sql | 20 +++++----- .../methods/operator/spec/operator.spec.js | 28 +++++++++++++ modules/worker/back/models/operator.js | 39 +++++++++++++------ 5 files changed, 75 insertions(+), 21 deletions(-) rename db/changes/{234601 => 234801}/00-sectorBackUpLabelerFk.sql (85%) diff --git a/back/models/notification.json b/back/models/notification.json index 56f66bf1d..07702d99d 100644 --- a/back/models/notification.json +++ b/back/models/notification.json @@ -18,6 +18,9 @@ }, "description": { "type": "string" + }, + "delay": { + "type": "number" } }, "relations": { diff --git a/db/changes/234601/00-sectorBackUpLabelerFk.sql b/db/changes/234801/00-sectorBackUpLabelerFk.sql similarity index 85% rename from db/changes/234601/00-sectorBackUpLabelerFk.sql rename to db/changes/234801/00-sectorBackUpLabelerFk.sql index ac512493c..bf7126693 100644 --- a/db/changes/234601/00-sectorBackUpLabelerFk.sql +++ b/db/changes/234801/00-sectorBackUpLabelerFk.sql @@ -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; diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index b492a7aed..870f56a30 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -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 diff --git a/modules/worker/back/methods/operator/spec/operator.spec.js b/modules/worker/back/methods/operator/spec/operator.spec.js index 35aecf538..1d4a9a84d 100644 --- a/modules/worker/back/methods/operator/spec/operator.spec.js +++ b/modules/worker/back/methods/operator/spec/operator.spec.js @@ -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; + } + }); }); diff --git a/modules/worker/back/models/operator.js b/modules/worker/back/models/operator.js index f51a6431c..75ee07821 100644 --- a/modules/worker/back/models/operator.js +++ b/modules/worker/back/models/operator.js @@ -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); + } } }); };