fix: refs #6005 move logic to hook
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
This commit is contained in:
parent
4b4aaa9e10
commit
638b715ee5
|
@ -346,5 +346,6 @@
|
||||||
"CountryFK cannot be empty": "El país no puede estar vacío",
|
"CountryFK cannot be empty": "El país no puede estar vacío",
|
||||||
"Cmr file does not exist": "El archivo del cmr no existe",
|
"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",
|
"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"
|
||||||
}
|
}
|
|
@ -1,38 +1,24 @@
|
||||||
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;
|
||||||
const notificationName = 'backup-printer-selected';
|
const notificationName = 'backup-printer-selected';
|
||||||
const operator = {
|
|
||||||
workerFk: 1,
|
|
||||||
trainFk: 1,
|
|
||||||
itemPackingTypeFk: 'H',
|
|
||||||
warehouseFk: 1,
|
|
||||||
sectorFk: sectorId
|
|
||||||
};
|
|
||||||
|
|
||||||
const errorStatus = 'error';
|
const errorStatus = 'error';
|
||||||
|
const sentStatus = 'sent';
|
||||||
|
|
||||||
async function createOperator(labelerFk, options) {
|
beforeEach(async() => {
|
||||||
operator.labelerFk = labelerFk;
|
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
|
||||||
await models.Operator.create(operator, options);
|
});
|
||||||
return models.NotificationQueue.findOne({
|
|
||||||
where: {
|
async function updateOperatorAndFindNotification(labelerFk = labeler) {
|
||||||
notificationFk: notificationName,
|
await models.Operator.updateAll({id: authorFk}, {workerFk: authorFk, labelerFk: labelerFk, sectorFk: sectorId});
|
||||||
authorFk: authorFk,
|
return models.NotificationQueue.findOne({order: 'id DESC'});
|
||||||
},
|
|
||||||
order: 'created DESC',
|
|
||||||
}, options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
it('should create notification when configured a backup printer in the sector', async() => {
|
it('should create notification when configured a backup printer in the sector', async() => {
|
||||||
const tx = await models.Operator.beginTransaction({});
|
const notificationQueue = await updateOperatorAndFindNotification();
|
||||||
|
|
||||||
try {
|
|
||||||
const options = {transaction: tx, accessToken: {userId: authorFk}};
|
|
||||||
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);
|
||||||
|
@ -40,60 +26,36 @@ fdescribe('Operator', () => {
|
||||||
expect(params.labelerId).toEqual(1);
|
expect(params.labelerId).toEqual(1);
|
||||||
expect(params.sectorId).toEqual(1);
|
expect(params.sectorId).toEqual(1);
|
||||||
expect(params.workerId).toEqual(9);
|
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() => {
|
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 {
|
expect(notificationQueue?.notificationFk).not.toEqual(notificationName);
|
||||||
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;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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 notification = await models.Notification.findOne({where: {name: notificationName}});
|
||||||
|
const {delay} = notification;
|
||||||
|
const lastNotification = await updateOperatorAndFindNotification();
|
||||||
|
await notification.updateAttributes({delay});
|
||||||
|
|
||||||
try {
|
expect(lastNotification.notificationFk).toEqual(notificationName);
|
||||||
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;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
fit('should not sent notification when is already notified by another worker', async() => {
|
it('should not sent notification when is already notified by another worker', async() => {
|
||||||
await models.Operator.updateAll({id: 1}, {labelerFk: labeler, sectorFk: sectorId}, null);
|
try {
|
||||||
await models.Operator.updateAll({id: 1}, {labelerFk: labeler, sectorFk: sectorId}, null);
|
await models.NotificationQueue.create({
|
||||||
|
authorFk: 2,
|
||||||
const lastNotification = await models.NotificationQueue.find({order: 'id DESC', limit: 2});
|
notificationFk: notificationName,
|
||||||
console.log('lastNotification: ', lastNotification);
|
params: JSON.stringify({'labelerId': labeler, 'sectorId': sectorId, 'workerId': 2}),
|
||||||
|
created: '2001-01-01 12:30:00',
|
||||||
await models.NotificationQueue.destroyAll({notificationFk: notificationName});
|
status: sentStatus
|
||||||
|
});
|
||||||
expect(lastNotification.status).toEqual(errorStatus);
|
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() => {
|
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',
|
created: '2001-01-01 12:30:00',
|
||||||
status: errorStatus
|
status: errorStatus
|
||||||
});
|
});
|
||||||
|
const lastNotification = await updateOperatorAndFindNotification();
|
||||||
|
|
||||||
await models.NotificationQueue.create({
|
expect(lastNotification.notificationFk).toEqual(notificationName);
|
||||||
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() => {
|
it('should send a notification when the previous one has distinct params', async() => {
|
||||||
|
@ -126,44 +77,11 @@ fdescribe('Operator', () => {
|
||||||
notificationFk: notificationName,
|
notificationFk: notificationName,
|
||||||
params: JSON.stringify({'labelerId': labeler, 'sectorId': 2, 'workerId': 1}),
|
params: JSON.stringify({'labelerId': labeler, 'sectorId': 2, 'workerId': 1}),
|
||||||
created: '2001-01-01 12:30:00',
|
created: '2001-01-01 12:30:00',
|
||||||
|
status: sentStatus
|
||||||
});
|
});
|
||||||
|
const lastNotification = await updateOperatorAndFindNotification();
|
||||||
|
|
||||||
await models.NotificationQueue.create({
|
expect(lastNotification.notificationFk).toEqual(notificationName);
|
||||||
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');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,43 +1,43 @@
|
||||||
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 notificationName = 'backup-printer-selected';
|
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;
|
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) {
|
||||||
console.log('entra');
|
|
||||||
const {labelerFk, sectorFk} = instance;
|
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) {
|
if (delay) {
|
||||||
const now = Date.vnNow();
|
const now = Date.vnNow() - (delay * 1000) + (3600 * 1000);
|
||||||
const filter = {where: {created: {between: [now - (delay * 1000), now]}}};
|
const notifications = await models.NotificationQueue.find(
|
||||||
const notifications = await models.NotificationQueue.find(filter, options);
|
{where:
|
||||||
console.log('notifications: ', notifications);
|
|
||||||
|
{created: {gte: now},
|
||||||
|
notificationFk: notificationName,
|
||||||
|
status: 'sent'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const criteria = {labelerId: labelerFk, sectorId: sectorFk};
|
const criteria = {labelerId: labelerFk, sectorId: sectorFk};
|
||||||
const filteredNotifications = notifications.filter(notification => {
|
const filteredNotifications = notifications.filter(notification => {
|
||||||
const paramsObj = JSON.parse(notification.params);
|
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');
|
throw new Error('Previous notification sended with the same parameters');
|
||||||
}
|
}
|
||||||
|
|
||||||
const created = await models.NotificationQueue.create({
|
await models.NotificationQueue.create({
|
||||||
notificationFk: notificationName,
|
notificationFk: notificationName,
|
||||||
authorFk: userId,
|
authorFk: userId,
|
||||||
params: JSON.stringify(
|
params: JSON.stringify(
|
||||||
|
@ -48,7 +48,6 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
console.log('created: ', created);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue