feat(operator.spec): refs #6005 add spec for delay and fix spec for spam
This commit is contained in:
parent
a35a790155
commit
754150d17d
|
@ -3,6 +3,7 @@ const models = require('vn-loopback/server/server').models;
|
||||||
describe('Operator', () => {
|
describe('Operator', () => {
|
||||||
const authorFk = 9;
|
const authorFk = 9;
|
||||||
const sectorId = 1;
|
const sectorId = 1;
|
||||||
|
const labeler = 1;
|
||||||
const notificationName = 'backup-printer-selected';
|
const notificationName = 'backup-printer-selected';
|
||||||
const operator = {
|
const operator = {
|
||||||
workerFk: 1,
|
workerFk: 1,
|
||||||
|
@ -17,8 +18,10 @@ describe('Operator', () => {
|
||||||
await models.Operator.create(operator, options);
|
await models.Operator.create(operator, options);
|
||||||
return models.NotificationQueue.findOne({
|
return models.NotificationQueue.findOne({
|
||||||
where: {
|
where: {
|
||||||
notificationFk: notificationName
|
notificationFk: notificationName,
|
||||||
}
|
authorFk: authorFk,
|
||||||
|
},
|
||||||
|
order: 'created DESC',
|
||||||
}, options);
|
}, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +30,7 @@ describe('Operator', () => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx, accessToken: {userId: authorFk}};
|
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);
|
const params = JSON.parse(notificationQueue.params);
|
||||||
|
|
||||||
expect(notificationQueue.notificationFk).toEqual(notificationName);
|
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({});
|
const tx = await models.Operator.beginTransaction({});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -67,18 +70,45 @@ describe('Operator', () => {
|
||||||
await models.NotificationQueue.create({
|
await models.NotificationQueue.create({
|
||||||
authorFk: 1,
|
authorFk: 1,
|
||||||
notificationFk: notificationName,
|
notificationFk: notificationName,
|
||||||
params: JSON.stringify({'labelerId': 10, 'sectorId': 10, 'workerId': 10}),
|
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 10}),
|
||||||
created: Date.vnNow(),
|
created: Date.vnNow(),
|
||||||
}, options);
|
}, 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);
|
const params = JSON.parse(notificationQueue.params);
|
||||||
|
|
||||||
expect(notificationQueue.notificationFk).toEqual(notificationName);
|
expect(notificationQueue.notificationFk).toEqual(notificationName);
|
||||||
expect(notificationQueue.authorFk).toEqual(1);
|
expect(notificationQueue.authorFk).toEqual(authorFk);
|
||||||
expect(params.labelerId).toEqual(10);
|
expect(params.labelerId).toEqual(1);
|
||||||
expect(params.sectorId).toEqual(10);
|
expect(params.sectorId).toEqual(1);
|
||||||
expect(params.workerId).toEqual(10);
|
expect(params.workerId).toEqual(9);
|
||||||
|
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -15,7 +15,7 @@ module.exports = function(Self) {
|
||||||
const {userId} = ctx.options.accessToken;
|
const {userId} = ctx.options.accessToken;
|
||||||
const {delay} = await models.Notification.findOne({
|
const {delay} = await models.Notification.findOne({
|
||||||
where: {name: notification}
|
where: {name: notification}
|
||||||
});
|
}, options);
|
||||||
const hasNotified = await models.NotificationQueue.findOne({
|
const hasNotified = await models.NotificationQueue.findOne({
|
||||||
where: {
|
where: {
|
||||||
notificationFk: notification,
|
notificationFk: notification,
|
||||||
|
@ -24,10 +24,10 @@ module.exports = function(Self) {
|
||||||
{params: {like: '%\"sectorId\":' + instance.sectorFk + '%'}}
|
{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({
|
await models.NotificationQueue.create({
|
||||||
notificationFk: notification,
|
notificationFk: notification,
|
||||||
authorFk: userId,
|
authorFk: userId,
|
||||||
|
|
Loading…
Reference in New Issue