7299_testToMaster #2411

Merged
alexm merged 342 commits from 7299_testToMaster into master 2024-05-07 05:30:42 +00:00
5 changed files with 44 additions and 70 deletions
Showing only changes of commit 4b4aaa9e10 - Show all commits

View File

@ -67,7 +67,7 @@ module.exports = Self => {
continue; continue;
} }
const newParams = Object.assign({}, queueParams, sendParams, {queueCreated: queue.created}); const newParams = Object.assign({}, queueParams, sendParams);
const email = new Email(queueName, newParams); const email = new Email(queueName, newParams);
if (process.env.NODE_ENV != 'test') if (process.env.NODE_ENV != 'test')

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Operator', () => { fdescribe('Operator', () => {
const authorFk = 9; const authorFk = 9;
const sectorId = 1; const sectorId = 1;
const labeler = 1; const labeler = 1;
@ -53,6 +53,7 @@ describe('Operator', () => {
try { try {
const options = {transaction: tx, accessToken: {userId: authorFk}}; const options = {transaction: tx, accessToken: {userId: authorFk}};
await models.NotificationQueue.destroyAll({notificationFk: notificationName}, options);
const notificationQueue = await createOperator(2, options); const notificationQueue = await createOperator(2, options);
expect(notificationQueue).toEqual(null); expect(notificationQueue).toEqual(null);
@ -70,25 +71,12 @@ describe('Operator', () => {
try { try {
const options = {transaction: tx, accessToken: {userId: authorFk}}; const options = {transaction: tx, accessToken: {userId: authorFk}};
await models.NotificationQueue.create({ const notifiation = await models.Notification.findOne({where: {name: notificationName}}, options);
authorFk: 1, await notifiation.updateAttributes({delay: null}, options);
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 1}),
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 notificationQueue = await createOperator(labeler, options);
const params = JSON.parse(notificationQueue.params);
expect(notificationQueue.notificationFk).toEqual(notificationName); expect(notificationQueue.notificationFk).toEqual(notificationName);
expect(notificationQueue.authorFk).toEqual(authorFk);
expect(params.labelerId).toEqual(1);
expect(params.sectorId).toEqual(1);
expect(params.workerId).toEqual(9);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
@ -96,23 +84,12 @@ describe('Operator', () => {
} }
}); });
it('should not sent notification when is already notified by another worker', async() => { fit('should not sent notification when is already notified by another worker', async() => {
await models.NotificationQueue.create({ await models.Operator.updateAll({id: 1}, {labelerFk: labeler, sectorFk: sectorId}, null);
authorFk: 2, await models.Operator.updateAll({id: 1}, {labelerFk: labeler, sectorFk: sectorId}, null);
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 2}),
created: '2001-01-01 12:30:00',
});
await models.NotificationQueue.create({ const lastNotification = await models.NotificationQueue.find({order: 'id DESC', limit: 2});
authorFk: 1, console.log('lastNotification: ', lastNotification);
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}); await models.NotificationQueue.destroyAll({notificationFk: notificationName});
@ -183,6 +160,7 @@ describe('Operator', () => {
await models.Notification.send(); await models.Notification.send();
const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'}); const lastNotification = await models.NotificationQueue.findOne({order: 'id DESC'});
console.log('lastNotification: ', lastNotification);
await models.NotificationQueue.destroyAll({notificationFk: notificationName}); await models.NotificationQueue.destroyAll({notificationFk: notificationName});

View File

@ -1,20 +1,44 @@
module.exports = Self => { module.exports = Self => {
Self.observe('after save', async ctx => { Self.observe('after save', async ctx => {
console.log('entra en after save');
const instance = ctx.data || ctx.instance; const instance = ctx.data || ctx.instance;
const models = Self.app.models; const models = Self.app.models;
const options = ctx.options; const options = ctx.options;
const notification = 'backup-printer-selected'; const notificationName = 'backup-printer-selected';
const {userId} = ctx.options.accessToken; const userId = ctx.options.accessToken?.userId;
if (!instance?.sectorFk || !instance?.labelerFk) return; if (!instance?.sectorFk || !instance?.labelerFk) return;
console.log('instance.sectorFk: ', instance.sectorFk);
const sector = await models.Sector.findById(instance.sectorFk, { const sector = await models.Sector.findById(instance.sectorFk, {
fields: ['backupPrinterFk'] fields: ['backupPrinterFk']
}, options); }, options);
console.log('sector.backupPrinterFk == instance.labelerFk: ', sector.backupPrinterFk == instance.labelerFk);
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) { if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
await models.NotificationQueue.create({ console.log('entra');
notificationFk: notification, const {labelerFk, sectorFk} = instance;
const [{delay}] = await models.Notification.find({where: {name: notificationName}}, options);
if (delay) {
const now = Date.vnNow();
const filter = {where: {created: {between: [now - (delay * 1000), now]}}};
const notifications = await models.NotificationQueue.find(filter, options);
console.log('notifications: ', notifications);
const criteria = {labelerId: labelerFk, sectorId: sectorFk};
const filteredNotifications = notifications.filter(notification => {
const paramsObj = JSON.parse(notification.params);
console.log('paramsObj: ', paramsObj);
return Object.keys(criteria).every(key => criteria[key] === paramsObj[key]);
});
console.log('filteredNotifications.length: ', filteredNotifications.length);
if (filteredNotifications.length > 1)
throw new Error('Previous notification sended with the same parameters');
}
const created = await models.NotificationQueue.create({
notificationFk: notificationName,
authorFk: userId, authorFk: userId,
params: JSON.stringify( params: JSON.stringify(
{ {
@ -23,7 +47,9 @@ module.exports = Self => {
'workerId': userId 'workerId': userId
} }
) )
}, options); });
console.log('created: ', created);
} }
}); });
}; };

View File

@ -1,18 +1,9 @@
const Component = require(`vn-print/core/component`); const Component = require(`vn-print/core/component`);
const emailBody = new Component('email-body'); const emailBody = new Component('email-body');
const name = 'backup-printer-selected';
module.exports = { module.exports = {
name: name, name: 'backup-printer-selected',
async serverPrefetch() { async serverPrefetch() {
const notifications = await this.rawSqlFromDef(
'previousNotifications',
[name, this.queueCreated, this.queueCreated]
);
if (checkDuplicates(notifications, this.labelerId, this.sectorId))
throw new Error('Previous notification sended with the same parameters');
this.sector = await this.findOneFromDef('sector', [this.sectorId]); this.sector = await this.findOneFromDef('sector', [this.sectorId]);
if (!this.sector) if (!this.sector)
throw new Error('Something went wrong'); throw new Error('Something went wrong');
@ -36,21 +27,7 @@ module.exports = {
workerId: { workerId: {
type: Number, type: Number,
required: true required: true
},
queueCreated: {
type: Date,
required: true
} }
} }
}; };
function checkDuplicates(notifications, labelerFk, printerFk) {
const criteria = {labelerId: labelerFk, sectorId: printerFk};
const filteredNotifications = notifications.filter(notification => {
const paramsObj = JSON.parse(notification.params);
return Object.keys(criteria).every(key => criteria[key] === paramsObj[key]);
});
return filteredNotifications.length > 1;
}

View File

@ -1,7 +0,0 @@
SELECT nq.params, created, status
FROM util.notificationQueue nq
JOIN util.notification n ON n.name = nq.notificationFk
WHERE n.name = ?
AND nq.created BETWEEN ? - INTERVAL IFNULL(n.delay, 0) SECOND AND ?
AND nq.status <> 'error'
ORDER BY created