6005-backupLabeler #2276

Merged
pablone merged 50 commits from 6005-backupLabeler into dev 2024-04-12 07:12:54 +00:00
3 changed files with 129 additions and 28 deletions
Showing only changes of commit 8815f07850 - Show all commits

View File

@ -45,7 +45,6 @@ module.exports = Self => {
}); });
availableNotificationsMap.delete(active.notificationFk); availableNotificationsMap.delete(active.notificationFk);
} }
console.log(activeNotificationsMap);
return { return {
active: [...activeNotificationsMap.entries()], active: [...activeNotificationsMap.entries()],
available: [...availableNotificationsMap.entries()] available: [...availableNotificationsMap.entries()]

View File

@ -1,6 +1,38 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Notification Send()', () => { describe('Notification Send()', () => {
beforeAll(async() => {
await models.NotificationQueue.destroyAll();
await models.NotificationQueue.create(
[
{
id: 1,
params: '{"id": "1"}',
status: 'pending',
created: '2000-12-31T23:00:00.000Z',
notificationFk: 'print-email',
authorFk: 9
},
{
id: 2,
params: '{"id": "2"}',
status: 'pending',
created: '2000-12-31T23:00:00.000Z',
notificationFk: 'print-email',
authorFk: null
},
{
id: 3,
params: null,
status: 'pending',
created: '2000-12-31T23:00:00.000Z',
notificationFk: 'print-email',
authorFk: null
}
]
);
});
it('should send notification', async() => { it('should send notification', async() => {
const statusPending = 'pending'; const statusPending = 'pending';
const tx = await models.NotificationQueue.beginTransaction({}); const tx = await models.NotificationQueue.beginTransaction({});

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
fdescribe('Operator', () => { describe('Operator', () => {
const authorFk = 9; const authorFk = 9;
const sectorId = 1; const sectorId = 1;
const labeler = 1; const labeler = 1;
@ -13,6 +13,8 @@ fdescribe('Operator', () => {
sectorFk: sectorId sectorFk: sectorId
}; };
const errorStatus = 'error';
async function createOperator(labelerFk, options) { async function createOperator(labelerFk, options) {
operator.labelerFk = labelerFk; operator.labelerFk = labelerFk;
await models.Operator.create(operator, options); await models.Operator.create(operator, options);
@ -62,31 +64,6 @@ fdescribe('Operator', () => {
} }
}); });
fit('should not create notification when is already notified by another worker', 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);
await createOperator(labeler, options);
await models.Notification.send(options);
const lastNotification = await models.NotificationQueue.find({order: 'id DESC'}, options);
console.log('lastNotification: ', lastNotification);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should create notification when delay is null', async() => { it('should create notification when delay is null', async() => {
const tx = await models.Operator.beginTransaction({}); const tx = await models.Operator.beginTransaction({});
@ -96,7 +73,7 @@ fdescribe('Operator', () => {
await models.NotificationQueue.create({ await models.NotificationQueue.create({
authorFk: 1, authorFk: 1,
notificationFk: notificationName, notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 10}), params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: Date.vnNow(), created: Date.vnNow(),
}, options); }, options);
@ -118,4 +95,97 @@ fdescribe('Operator', () => {
throw e; throw e;
} }
}); });
it('should not sent notification when is already notified by another worker', 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',
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 12:31:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual(errorStatus);
});
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
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 12:31:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('sent');
});
it('should send a notification when the previous one has distinct params', async() => {
await models.NotificationQueue.create({
authorFk: 2,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': 2, 'workerId': 1}),
created: '2001-01-01 12:30:00',
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 12:31:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('sent');
});
it('should respect de configured delay for the notification', 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',
});
await models.NotificationQueue.create({
authorFk: 1,
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
created: '2001-01-01 13:29:00',
});
await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('error');
});
}); });