fix: refs #6005 move logic to hook
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Pablo Natek 2024-04-09 09:30:28 +02:00
parent 4b4aaa9e10
commit 638b715ee5
3 changed files with 406 additions and 488 deletions

View File

@ -346,5 +346,6 @@
"CountryFK cannot be empty": "El país no puede estar vacío",
"Cmr file does not exist": "El archivo del cmr no existe",
"You are not allowed to modify the alias": "No estás autorizado a modificar el alias",
"The address of the customer must have information about Incoterms and Customs Agent": "El consignatario del cliente debe tener informado Incoterms y Agente de aduanas"
"The address of the customer must have information about Incoterms and Customs Agent": "El consignatario del cliente debe tener informado Incoterms y Agente de aduanas",
"PrinterNotInSameSector": "PrinterNotInSameSector"
}

View File

@ -1,38 +1,24 @@
const models = require('vn-loopback/server/server').models;
fdescribe('Operator', () => {
describe('Operator', () => {
const authorFk = 9;
const sectorId = 1;
const labeler = 1;
const notificationName = 'backup-printer-selected';
const operator = {
workerFk: 1,
trainFk: 1,
itemPackingTypeFk: 'H',
warehouseFk: 1,
sectorFk: sectorId
};
const errorStatus = 'error';
const sentStatus = 'sent';
async function createOperator(labelerFk, options) {
operator.labelerFk = labelerFk;
await models.Operator.create(operator, options);
return models.NotificationQueue.findOne({
where: {
notificationFk: notificationName,
authorFk: authorFk,
},
order: 'created DESC',
}, options);
beforeEach(async() => {
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
});
async function updateOperatorAndFindNotification(labelerFk = labeler) {
await models.Operator.updateAll({id: authorFk}, {workerFk: authorFk, labelerFk: labelerFk, sectorFk: sectorId});
return models.NotificationQueue.findOne({order: 'id DESC'});
}
it('should create notification when configured a backup printer in the sector', async() => {
const tx = await models.Operator.beginTransaction({});
try {
const options = {transaction: tx, accessToken: {userId: authorFk}};
const notificationQueue = await createOperator(labeler, options);
const notificationQueue = await updateOperatorAndFindNotification();
const params = JSON.parse(notificationQueue.params);
expect(notificationQueue.notificationFk).toEqual(notificationName);
@ -40,60 +26,36 @@ fdescribe('Operator', () => {
expect(params.labelerId).toEqual(1);
expect(params.sectorId).toEqual(1);
expect(params.workerId).toEqual(9);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should not create notification when configured a non backup printer in the sector', async() => {
const tx = await models.Operator.beginTransaction({});
const notificationQueue = await updateOperatorAndFindNotification(2);
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);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(notificationQueue?.notificationFk).not.toEqual(notificationName);
});
it('should create notification when delay is null', async() => {
const tx = await models.Operator.beginTransaction({});
const notification = await models.Notification.findOne({where: {name: notificationName}});
const {delay} = notification;
const lastNotification = await updateOperatorAndFindNotification();
await notification.updateAttributes({delay});
try {
const options = {transaction: tx, accessToken: {userId: authorFk}};
const notifiation = await models.Notification.findOne({where: {name: notificationName}}, options);
await notifiation.updateAttributes({delay: null}, options);
const notificationQueue = await createOperator(labeler, options);
expect(notificationQueue.notificationFk).toEqual(notificationName);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(lastNotification.notificationFk).toEqual(notificationName);
});
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);
const lastNotification = await models.NotificationQueue.find({order: 'id DESC', limit: 2});
console.log('lastNotification: ', lastNotification);
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual(errorStatus);
it('should not sent notification when is already notified by another worker', async() => {
try {
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: sentStatus
});
await models.Operator.updateAll({id: 1}, {labelerFk: labeler, sectorFk: sectorId});
} catch (e) {
expect(e.message).toEqual('Previous notification sended with the same parameters');
}
});
it('should send a notification when the previous one is on errorStatus status', async() => {
@ -104,20 +66,9 @@ fdescribe('Operator', () => {
created: '2001-01-01 12:30:00',
status: errorStatus
});
const lastNotification = await updateOperatorAndFindNotification();
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');
expect(lastNotification.notificationFk).toEqual(notificationName);
});
it('should send a notification when the previous one has distinct params', async() => {
@ -126,44 +77,11 @@ fdescribe('Operator', () => {
notificationFk: notificationName,
params: JSON.stringify({'labelerId': labeler, 'sectorId': 2, 'workerId': 1}),
created: '2001-01-01 12:30:00',
status: sentStatus
});
const lastNotification = await updateOperatorAndFindNotification();
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'});
console.log('lastNotification: ', lastNotification);
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
expect(lastNotification.status).toEqual('error');
expect(lastNotification.notificationFk).toEqual(notificationName);
});
});

View File

@ -1,43 +1,43 @@
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 notificationName = 'backup-printer-selected';
const userId = ctx.options.accessToken?.userId;
const userId = ctx.options.accessToken?.userId || instance.workerFk;
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) {
console.log('entra');
const {labelerFk, sectorFk} = instance;
const [{delay}] = await models.Notification.find({where: {name: notificationName}});
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 now = Date.vnNow() - (delay * 1000) + (3600 * 1000);
const notifications = await models.NotificationQueue.find(
{where:
{created: {gte: now},
notificationFk: notificationName,
status: 'sent'
}
});
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]);
return Object.keys(criteria).every(key => criteria[key] === paramsObj?.[key]);
});
console.log('filteredNotifications.length: ', filteredNotifications.length);
if (filteredNotifications.length > 1)
if (filteredNotifications.length >= 1)
throw new Error('Previous notification sended with the same parameters');
}
const created = await models.NotificationQueue.create({
await models.NotificationQueue.create({
notificationFk: notificationName,
authorFk: userId,
params: JSON.stringify(
@ -48,7 +48,6 @@ module.exports = Self => {
}
)
});
console.log('created: ', created);
}
});
};