6005-backupLabeler #2276

Merged
pablone merged 50 commits from 6005-backupLabeler into dev 2024-04-12 07:12:54 +00:00
5 changed files with 75 additions and 21 deletions
Showing only changes of commit 1b4ce1a0b5 - Show all commits

View File

@ -18,6 +18,9 @@
}, },
"description": { "description": {
"type": "string" "type": "string"
},
"delay": {
"type": "number"
} }
}, },
"relations": { "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 `vn`.`sector` CHANGE `mainPrinterFk` `backupPrinterFk` tinyint(3) unsigned DEFAULT NULL NULL;
ALTER TABLE `util`.`notificationSubscription` DROP FOREIGN KEY `notificationSubscription_ibfk_1`; ALTER TABLE `util`.`notificationSubscription` DROP FOREIGN KEY `notificationSubscription_ibfk_1`;
@ -16,4 +19,5 @@ DELETE FROM `util`.`notification`
INSERT INTO `util`.`notification` INSERT INTO `util`.`notification`
SET `id` = 15, SET `id` = 15,
`name` = 'backup-printer-selected', `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); ('', 'SHINNOBI_TOKEN', 'GROUP_TOKEN', 6000);
INSERT INTO `util`.`notificationConfig` INSERT INTO `util`.`notificationConfig`
SET `cleanDays` = 90; SET `cleanDays` = 90;
INSERT INTO `util`.`notification` (`id`, `name`, `description`) INSERT IGNORE INTO `util`.`notification` (`id`, `name`, `description`, `delay`)
VALUES VALUES
(1, 'print-email', 'notification fixture one'), (1, 'print-email', 'notification fixture one', NULL),
(2, 'invoice-electronic', 'A electronic invoice has been generated'), (2, 'invoice-electronic', 'A electronic invoice has been generated', NULL),
(4, 'supplier-pay-method-update', 'A supplier pay method has been updated'), (3, 'backup-printer-selected', 'A printer distinct than main has been configured', 600000),
(5, 'modified-entry', 'An entry has been modified'), (4, 'supplier-pay-method-update', 'A supplier pay method has been updated', NULL),
(6, 'book-entry-deleted', 'accounting entries deleted'); (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`) UPDATE `util`.`notification`
VALUES (3, 'backup-printer-selected', 'A printer distinct than main has been configured'); SET `id` = 3
WHERE `name` = 'backup-printer-selected';
INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`)
VALUES VALUES

View File

@ -58,4 +58,32 @@ describe('Operator', () => {
throw e; 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 instance = ctx.data || ctx.instance;
const models = Self.app.models; const models = Self.app.models;
const options = ctx.options; const options = ctx.options;
const notification = 'backup-printer-selected';
if (!instance?.sectorFk || !instance?.labelerFk) return; if (!instance?.sectorFk || !instance?.labelerFk) return;
@ -12,17 +13,33 @@ module.exports = function(Self) {
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) { if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
const {userId} = ctx.options.accessToken; const {userId} = ctx.options.accessToken;
await models.NotificationQueue.create({ const {delay} = await models.Notification.findOne({
notificationFk: 'backup-printer-selected', where: {name: notification}
authorFk: userId, });
params: JSON.stringify( const hasNotified = await models.NotificationQueue.findOne({
{ where: {
'labelerId': instance.labelerFk, notificationFk: notification,
'sectorId': instance.sectorFk, and: [
'workerId': userId {params: {like: '%\"labelerId\":' + instance.labelerFk + '%'}},
} {params: {like: '%\"sectorId\":' + instance.sectorFk + '%'}}
) ]
}, options); },
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);
}
} }
}); });
}; };