7253-devToTest_2418 #2350

Merged
alexm merged 320 commits from 7253-devToTest_2418 into test 2024-04-23 08:02:46 +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;
}
const newParams = Object.assign({}, queueParams, sendParams, {queueCreated: queue.created});
const newParams = Object.assign({}, queueParams, sendParams);
const email = new Email(queueName, newParams);
if (process.env.NODE_ENV != 'test')

View File

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

View File

@ -1,20 +1,44 @@
module.exports = Self => {
Self.observe('after save', async ctx => {
console.log('entra en after save');
const instance = ctx.data || ctx.instance;
const models = Self.app.models;
const options = ctx.options;
const notification = 'backup-printer-selected';
const {userId} = ctx.options.accessToken;
const notificationName = 'backup-printer-selected';
const userId = ctx.options.accessToken?.userId;
if (!instance?.sectorFk || !instance?.labelerFk) return;
console.log('instance.sectorFk: ', instance.sectorFk);
const sector = await models.Sector.findById(instance.sectorFk, {
fields: ['backupPrinterFk']
}, options);
console.log('sector.backupPrinterFk == instance.labelerFk: ', sector.backupPrinterFk == instance.labelerFk);
if (sector.backupPrinterFk && sector.backupPrinterFk == instance.labelerFk) {
await models.NotificationQueue.create({
notificationFk: notification,
console.log('entra');
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,
params: JSON.stringify(
{
@ -23,7 +47,9 @@ module.exports = Self => {
'workerId': userId
}
)
}, options);
});
console.log('created: ', created);
}
});
};

View File

@ -1,18 +1,9 @@
const Component = require(`vn-print/core/component`);
const emailBody = new Component('email-body');
const name = 'backup-printer-selected';
module.exports = {
name: name,
name: 'backup-printer-selected',
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]);
if (!this.sector)
throw new Error('Something went wrong');
@ -36,21 +27,7 @@ module.exports = {
workerId: {
type: Number,
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