refs #4797 refactor: getList & notificationSubscription method

This commit is contained in:
Alex Moreno 2023-11-08 14:56:47 +01:00
parent 8c5e40c27f
commit 0de5bc5109
3 changed files with 57 additions and 39 deletions

View File

@ -10,5 +10,8 @@
"eslint.format.enable": true,
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
},
"cSpell.words": [
"salix"
]
}

View File

@ -21,55 +21,34 @@ module.exports = Self => {
});
Self.getList = async(id, options) => {
const notifications = new Map();
const models = Self.app.models;
const activeNotificationsMap = new Map();
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const roles = await models.RoleMapping.find({
fields: ['roleId'],
where: {principalId: id}
}, myOptions);
const availableNotifications = await models.NotificationAcl.find({
fields: ['notificationFk', 'roleFk'],
include: {relation: 'notification'},
where: {
roleFk: {
inq: roles.map(role => role.roleId),
},
}
}, myOptions);
const activeNotifications = await models.NotificationSubscription.find({
const availableNotificationsMap = await Self.getAvailable(id, myOptions);
const activeNotifications = await Self.app.models.NotificationSubscription.find({
fields: ['id', 'notificationFk'],
include: {relation: 'notification'},
where: {userFk: id}
}, myOptions);
for (acl of availableNotifications) {
notifications.set(acl.notificationFk, {
id: null,
notificationFk: acl.notificationFk,
name: acl.notification().name,
description: acl.notification().description,
active: false,
for (active of activeNotifications) {
activeNotificationsMap.set(active.notificationFk, {
id: active.id,
notificationFk: active.notificationFk,
name: active.notification().name,
description: active.notification().description,
active: true
});
availableNotificationsMap.delete(active.notificationFk);
}
for (subscription of activeNotifications) {
notifications.set(subscription.notificationFk, {
id: subscription.id,
notificationFk: subscription.notificationFk,
name: subscription.notification().name,
description: subscription.notification().description,
active: true,
});
}
return [...notifications.values()];
return {
active: [...activeNotificationsMap.entries()],
available: [...availableNotificationsMap.entries()]
};
};
};

View File

@ -29,10 +29,46 @@ module.exports = Self => {
}
const worker = await models.Worker.findById(workerId, {fields: ['id', 'bossFk']});
const notificationsAvailables = await models.NotificationSubscription.getList(workerId);
const hasAcl = notificationsAvailables.some(available => available.notificationFk === notificationFk);
const available = await Self.getAvailable(workerId);
const hasAcl = available.has(notificationFk);
if (!hasAcl || (userId != worker.id && userId != worker.bossFk))
throw new UserError('The notification subscription of this worker cant be modified');
}
Self.getAvailable = async function(userId, options) {
const availableNotificationsMap = new Map();
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const roles = await models.RoleMapping.find({
fields: ['roleId'],
where: {principalId: userId}
}, myOptions);
const availableNotifications = await models.NotificationAcl.find({
fields: ['notificationFk', 'roleFk'],
include: {relation: 'notification'},
where: {
roleFk: {
inq: roles.map(role => role.roleId),
},
}
}, myOptions);
for (available of availableNotifications) {
availableNotificationsMap.set(available.notificationFk, {
id: null,
notificationFk: available.notificationFk,
name: available.notification().name,
description: available.notification().description,
active: false
});
}
return availableNotificationsMap;
};
};