From da037c5235c956e924bb57fdfd2254f28e04bdbf Mon Sep 17 00:00:00 2001 From: pablone Date: Tue, 9 Apr 2024 13:34:33 +0200 Subject: [PATCH] refactor: refs #6005 delay on productionConfig --- back/model-config.json | 3 +++ back/models/production-config.json | 19 +++++++++++++++ db/dump/fixtures.after.sql | 10 -------- db/dump/fixtures.before.sql | 22 +++++++++--------- .../10895-pinkArborvitae/02-thirdScript.sql | 2 +- .../03-insertBackUpNotification.vn.sql | 4 ++-- .../methods/operator/spec/operator.spec.js | 23 ++++--------------- modules/worker/back/models/operator.js | 15 +++++------- 8 files changed, 47 insertions(+), 51 deletions(-) create mode 100644 back/models/production-config.json diff --git a/back/model-config.json b/back/model-config.json index f48ec11e6..ebcdb7bce 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -174,5 +174,8 @@ }, "WorkerActivityType": { "dataSource": "vn" + }, + "ProductionConfig": { + "dataSource": "vn" } } \ No newline at end of file diff --git a/back/models/production-config.json b/back/models/production-config.json new file mode 100644 index 000000000..3800dbbf2 --- /dev/null +++ b/back/models/production-config.json @@ -0,0 +1,19 @@ +{ + "name": "ProductionConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "productionConfig" + } + }, + "properties": { + "id": { + "type": "number", + "required": true, + "id": true + }, + "backupPrinterNotificationDelay": { + "type": "string" + } + } +} diff --git a/db/dump/fixtures.after.sql b/db/dump/fixtures.after.sql index 896642005..fade82c3e 100644 --- a/db/dump/fixtures.after.sql +++ b/db/dump/fixtures.after.sql @@ -153,16 +153,6 @@ INSERT INTO `vn`.`occupationCode` (`code`, `name`) ('b', 'Representantes de comercio'), ('c', 'Personal de oficios en trabajos de construcción en general, y en instalac.,edificios y obras'); -INSERT INTO `vn2008`.`payroll_employee` (`CodTrabajador`,`codempresa`) - VALUES - (36,20), - (43,20), - (76,20), - (1106,20), - (1107,20), - (1108,20), - (1109,20), - (1110,20); INSERT INTO `vn`.`trainingCourseType` (`id`, `name`) VALUES diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 8c2f0a5d4..25a2ee8fb 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -2805,16 +2805,16 @@ INSERT INTO `util`.`notificationConfig` SET `id` = 1, `cleanDays` = 90; TRUNCATE `util`.`notification`; -INSERT INTO `util`.`notification` (`id`, `name`, `description`, `delay`) +INSERT INTO `util`.`notification` (`id`, `name`, `description`) VALUES - (1, 'print-email', 'notification fixture one', NULL), - (2, 'invoice-electronic', 'A electronic invoice has been generated', NULL), - (3, 'not-main-printer-configured', 'A printer distinct than main has been configured', NULL), - (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), - (7, 'zone-included','An email to notify zoneCollisions'); - (8, 'backup-printer-selected','A backup printer has been selected', 3600), + (1, 'print-email', 'notification fixture one'), + (2, 'invoice-electronic', 'A electronic invoice has been generated'), + (3, 'not-main-printer-configured', 'A printer distinct than main has been configured'), + (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'), + (7, 'zone-included','An email to notify zoneCollisions'), + (8, 'backup-printer-selected','A backup printer has been selected'); TRUNCATE `util`.`notificationAcl`; INSERT INTO `util`.`notificationAcl` (`notificationFk`, `roleFk`) @@ -2854,9 +2854,9 @@ INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) VALUES (1, 9); -INSERT INTO `vn`.`productionConfig` (`isPreviousPreparationRequired`, `ticketPrintedMax`, `ticketTrolleyMax`, `rookieDays`, `notBuyingMonths`, `id`, `isZoneClosedByExpeditionActivated`, `maxNotReadyCollections`, `minTicketsToCloseZone`, `movingTicketDelRoute`, `defaultZone`, `defautlAgencyMode`, `hasUniqueCollectionTime`, `maxCollectionWithoutUser`, `pendingCollectionsOrder`, `pendingCollectionsAge`) +INSERT INTO `vn`.`productionConfig` (`isPreviousPreparationRequired`, `ticketPrintedMax`, `ticketTrolleyMax`, `rookieDays`, `notBuyingMonths`, `id`, `isZoneClosedByExpeditionActivated`, `maxNotReadyCollections`, `minTicketsToCloseZone`, `movingTicketDelRoute`, `defaultZone`, `defautlAgencyMode`, `hasUniqueCollectionTime`, `maxCollectionWithoutUser`, `pendingCollectionsOrder`, `pendingCollectionsAge`, `backupPrinterNotificationDelay`) VALUES - (0, 8, 80, 0, 0, 1, 0, 15, 25, -1, 697, 1328, 0, 1, 8, 6); + (0, 8, 80, 0, 0, 1, 0, 15, 25, -1, 697, 1328, 0, 1, 8, 6, 3600); INSERT INTO `vn`.`collection` (`id`, `created`, `workerFk`, `stateFk`, `itemPackingTypeFk`, `saleTotalCount`, `salePickedCount`, `trainFk`, `sectorFk`, `wagons`) VALUES diff --git a/db/versions/10895-pinkArborvitae/02-thirdScript.sql b/db/versions/10895-pinkArborvitae/02-thirdScript.sql index 21e97580f..142ec06b1 100644 --- a/db/versions/10895-pinkArborvitae/02-thirdScript.sql +++ b/db/versions/10895-pinkArborvitae/02-thirdScript.sql @@ -1,5 +1,5 @@ -ALTER TABLE `util`.`notification` ADD IF NOT EXISTS delay int unsigned NULL +ALTER TABLE `vn`.`productionConfig` ADD IF NOT EXISTS backupPrinterNotificationDelay int unsigned NULL COMMENT 'Minimum seconds Interval to Prevent Spam from Same-Type Notifications'; ALTER TABLE vn.sector DROP FOREIGN KEY IF EXISTS sector_FK; diff --git a/db/versions/10895-pinkArborvitae/03-insertBackUpNotification.vn.sql b/db/versions/10895-pinkArborvitae/03-insertBackUpNotification.vn.sql index b6558e6d4..9dc3c0f60 100644 --- a/db/versions/10895-pinkArborvitae/03-insertBackUpNotification.vn.sql +++ b/db/versions/10895-pinkArborvitae/03-insertBackUpNotification.vn.sql @@ -1,5 +1,5 @@ -INSERT IGNORE INTO util.notification (name, description, delay) - VALUES ('backup-printer-selected','A backup printer has been selected', 3600); +INSERT IGNORE INTO util.notification (name, description) + VALUES ('backup-printer-selected','A backup printer has been selected'); INSERT IGNORE INTO util.notificationSubscription (notificationFk, userFk) SELECT id, 10435 diff --git a/modules/worker/back/methods/operator/spec/operator.spec.js b/modules/worker/back/methods/operator/spec/operator.spec.js index 6a4a5cff1..5a3528641 100644 --- a/modules/worker/back/methods/operator/spec/operator.spec.js +++ b/modules/worker/back/methods/operator/spec/operator.spec.js @@ -1,11 +1,10 @@ const models = require('vn-loopback/server/server').models; -describe('Operator', () => { +fdescribe('Operator', () => { const authorFk = 9; const sectorId = 1; const labeler = 1; const notificationName = 'backup-printer-selected'; - const errorStatus = 'error'; const sentStatus = 'sent'; beforeEach(async() => { @@ -35,10 +34,11 @@ describe('Operator', () => { }); it('should create notification when delay is null', async() => { - const notification = await models.Notification.findOne({where: {name: notificationName}}); - const {delay} = notification; + const config = await models.ProductionConfig.findOne(); + const delay = config.backupPrinterNotificationDelay; + await config.updateAttributes({backupPrinterNotificationDelay: null}); const lastNotification = await updateOperatorAndFindNotification(); - await notification.updateAttributes({delay}); + await config.updateAttributes({backupPrinterNotificationDelay: delay}); expect(lastNotification.notificationFk).toEqual(notificationName); }); @@ -58,19 +58,6 @@ describe('Operator', () => { } }); - it('should send a notification when the previous one is on errorStatus status', async() => { - await models.NotificationQueue.create({ - authorFk: 2, - notificationFk: notificationName, - params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 2}), - created: '2001-01-01 12:30:00', - status: errorStatus - }); - const lastNotification = await updateOperatorAndFindNotification(); - - expect(lastNotification.notificationFk).toEqual(notificationName); - }); - it('should send a notification when the previous one has distinct params', async() => { await models.NotificationQueue.create({ authorFk: 2, diff --git a/modules/worker/back/models/operator.js b/modules/worker/back/models/operator.js index d123a1493..1ebc1643c 100644 --- a/modules/worker/back/models/operator.js +++ b/modules/worker/back/models/operator.js @@ -14,17 +14,14 @@ module.exports = Self => { if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) { const {labelerFk, sectorFk} = instance; - const [{delay}] = await models.Notification.find({where: {name: notificationName}}); - if (delay) { - const now = Date.vnNow() - (delay * 1000) + (3600 * 1000); + const {backupPrinterNotificationDelay} = await models.ProductionConfig.findOne(); + if (backupPrinterNotificationDelay) { const notifications = await models.NotificationQueue.find( - {where: - - {created: {gte: now}, - notificationFk: notificationName, - status: 'sent' - } + {where: {created: {gte: Date.vnNow() - (backupPrinterNotificationDelay * 1000) + (3600 * 1000)}, + notificationFk: notificationName, + status: 'sent' + } }); const criteria = {labelerId: labelerFk, sectorId: sectorFk};