2023-05-09 09:34:32 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('getList', {
|
|
|
|
description: 'Get list of the available and active notification subscriptions',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
description: 'User to modify',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/getList`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.getList = async(id, options) => {
|
|
|
|
const notifications = [];
|
|
|
|
const models = Self.app.models;
|
|
|
|
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const activeNotifications = await models.NotificationSubscription.find({
|
|
|
|
include: {relation: 'notification'},
|
|
|
|
where: {userFk: id}
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const roles = await models.RoleMapping.find({
|
|
|
|
fields: ['roleId'],
|
|
|
|
where: {principalId: id}
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const availableNotifications = await models.NotificationAcl.find({
|
|
|
|
include: {relation: 'notification'},
|
|
|
|
where: {
|
|
|
|
roleFk: {
|
|
|
|
inq: roles.map(role => {
|
|
|
|
return role.roleId;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
2023-05-09 12:20:20 +00:00
|
|
|
for (subscription of activeNotifications) {
|
2023-05-09 09:34:32 +00:00
|
|
|
notifications.push({
|
|
|
|
id: subscription.id,
|
|
|
|
notificationFk: subscription.notificationFk,
|
|
|
|
name: subscription.notification().name,
|
|
|
|
description: subscription.notification().description,
|
|
|
|
active: true
|
|
|
|
});
|
2023-05-09 12:20:20 +00:00
|
|
|
}
|
2023-05-09 09:34:32 +00:00
|
|
|
|
2023-05-09 12:20:20 +00:00
|
|
|
for (acl of availableNotifications) {
|
2023-05-09 09:34:32 +00:00
|
|
|
const activeNotif = notifications.find(
|
|
|
|
notif => notif.notificationFk === acl.notificationFk
|
|
|
|
);
|
|
|
|
if (!activeNotif) {
|
|
|
|
notifications.push({
|
|
|
|
id: null,
|
|
|
|
notificationFk: acl.notificationFk,
|
|
|
|
name: acl.notification().name,
|
|
|
|
description: acl.notification().description,
|
|
|
|
active: false,
|
|
|
|
});
|
|
|
|
}
|
2023-05-09 12:20:20 +00:00
|
|
|
}
|
2023-05-09 09:34:32 +00:00
|
|
|
|
|
|
|
return notifications;
|
|
|
|
};
|
|
|
|
};
|