feat(operator.spec): refs #6005 add spec for delay and fix spec for spam

This commit is contained in:
Pablo Natek 2023-11-23 10:10:09 +01:00
parent a35a790155
commit 754150d17d
2 changed files with 44 additions and 14 deletions

View File

@ -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) {

View File

@ -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,