refs #4797 minor fixes
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alexandre Riera 2023-05-10 14:10:00 +02:00
parent 5171e63188
commit f9e7963e74
2 changed files with 18 additions and 34 deletions

View File

@ -21,7 +21,7 @@ module.exports = Self => {
}); });
Self.getList = async(id, options) => { Self.getList = async(id, options) => {
const notifications = []; const notifications = new Map();
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
@ -29,11 +29,6 @@ module.exports = Self => {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const activeNotifications = await models.NotificationSubscription.find({
include: {relation: 'notification'},
where: {userFk: id}
}, myOptions);
const roles = await models.RoleMapping.find({ const roles = await models.RoleMapping.find({
fields: ['roleId'], fields: ['roleId'],
where: {principalId: id} where: {principalId: id}
@ -43,38 +38,29 @@ module.exports = Self => {
include: {relation: 'notification'}, include: {relation: 'notification'},
where: { where: {
roleFk: { roleFk: {
inq: roles.map(role => { inq: roles.map(role => role.roleId),
return role.roleId;
}),
}, },
} }
}, myOptions); }, myOptions);
for (subscription of activeNotifications) { for (acl of availableNotifications) {
notifications.push({ notifications.set(acl.notificationFk, {
id: subscription.id, id: null,
notificationFk: subscription.notificationFk, notificationFk: acl.notificationFk,
name: subscription.notification().name, name: acl.notification().name,
description: subscription.notification().description, description: acl.notification().description,
active: true active: false,
}); });
} }
for (acl of availableNotifications) { const activeNotifications = await models.NotificationSubscription.find({
const activeNotif = notifications.find( include: {relation: 'notification'},
notif => notif.notificationFk === acl.notificationFk where: {userFk: id}
); }, myOptions);
if (!activeNotif) {
notifications.push({
id: null,
notificationFk: acl.notificationFk,
name: acl.notification().name,
description: acl.notification().description,
active: false,
});
}
}
return notifications; for (subscription of activeNotifications)
notifications.get(subscription.notificationFk).active = true;
return [...notifications.values()];
}; };
}; };

View File

@ -30,9 +30,7 @@ module.exports = Self => {
const worker = await models.Worker.findById(workerId, {fields: ['id', 'bossFk']}); const worker = await models.Worker.findById(workerId, {fields: ['id', 'bossFk']});
const notificationsAvailables = await models.NotificationSubscription.getList(workerId); const notificationsAvailables = await models.NotificationSubscription.getList(workerId);
const hasAcl = notificationsAvailables.some(function(available) { const hasAcl = notificationsAvailables.some(available => available.notificationFk === notificationFk);
return available.notificationFk === notificationFk;
});
if (!hasAcl || (userId != worker.id && userId != worker.bossFk)) if (!hasAcl || (userId != worker.id && userId != worker.bossFk))
throw new UserError('The notification subscription of this worker cant be modified'); throw new UserError('The notification subscription of this worker cant be modified');