diff --git a/modules/worker/back/methods/operator/spec/operator.spec.js b/modules/worker/back/methods/operator/spec/operator.spec.js index 1d4a9a84d..6a8b02e04 100644 --- a/modules/worker/back/methods/operator/spec/operator.spec.js +++ b/modules/worker/back/methods/operator/spec/operator.spec.js @@ -3,6 +3,7 @@ const models = require('vn-loopback/server/server').models; describe('Operator', () => { const authorFk = 9; const sectorId = 1; + const labeler = 1; const notificationName = 'backup-printer-selected'; const operator = { workerFk: 1, @@ -17,8 +18,10 @@ describe('Operator', () => { await models.Operator.create(operator, options); return models.NotificationQueue.findOne({ where: { - notificationFk: notificationName - } + notificationFk: notificationName, + authorFk: authorFk, + }, + order: 'created DESC', }, options); } @@ -27,7 +30,7 @@ describe('Operator', () => { try { const options = {transaction: tx, accessToken: {userId: authorFk}}; - const notificationQueue = await createOperator(1, options); + const notificationQueue = await createOperator(labeler, options); const params = JSON.parse(notificationQueue.params); expect(notificationQueue.notificationFk).toEqual(notificationName); @@ -59,7 +62,7 @@ describe('Operator', () => { } }); - it('should not create notification when is already notified', async() => { + it('should not create notification when is already notified by another worker', async() => { const tx = await models.Operator.beginTransaction({}); try { @@ -67,18 +70,45 @@ describe('Operator', () => { await models.NotificationQueue.create({ authorFk: 1, notificationFk: notificationName, - params: JSON.stringify({'labelerId': 10, 'sectorId': 10, 'workerId': 10}), + params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 10}), created: Date.vnNow(), }, options); - const notificationQueue = await createOperator(1, options); + const notificationQueue = await createOperator(labeler, options); + + expect(notificationQueue).toEqual(null); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should create notification when delay is null', 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': labeler, 'sectorId': sectorId, 'workerId': 10}), + created: Date.vnNow(), + }, options); + + const notification = await models.Notification.findOne({where: {name: notificationName}}, options); + await notification.updateAttributes({delay: null}, options); + + const notificationQueue = await createOperator(labeler, 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); + expect(notificationQueue.authorFk).toEqual(authorFk); + expect(params.labelerId).toEqual(1); + expect(params.sectorId).toEqual(1); + expect(params.workerId).toEqual(9); await tx.rollback(); } catch (e) { diff --git a/modules/worker/back/models/operator.js b/modules/worker/back/models/operator.js index 75ee07821..51fd0bfa1 100644 --- a/modules/worker/back/models/operator.js +++ b/modules/worker/back/models/operator.js @@ -15,7 +15,7 @@ module.exports = function(Self) { const {userId} = ctx.options.accessToken; const {delay} = await models.Notification.findOne({ where: {name: notification} - }); + }, options); const hasNotified = await models.NotificationQueue.findOne({ where: { notificationFk: notification, @@ -24,10 +24,10 @@ module.exports = function(Self) { {params: {like: '%\"sectorId\":' + instance.sectorFk + '%'}} ] }, - order: 'CREATED DESC', - }); + order: 'created DESC', + }, options); - if (hasNotified?.created - Date.now() > delay || !hasNotified?.created) { + if (hasNotified?.created - Date.now() > delay || !hasNotified?.created || !delay) { await models.NotificationQueue.create({ notificationFk: notification, authorFk: userId,