refs #4797 refactor: getList & notificationSubscription method
This commit is contained in:
parent
8c5e40c27f
commit
0de5bc5109
|
@ -10,5 +10,8 @@
|
||||||
"eslint.format.enable": true,
|
"eslint.format.enable": true,
|
||||||
"[javascript]": {
|
"[javascript]": {
|
||||||
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
|
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
|
||||||
}
|
},
|
||||||
|
"cSpell.words": [
|
||||||
|
"salix"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,55 +21,34 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.getList = async(id, options) => {
|
Self.getList = async(id, options) => {
|
||||||
const notifications = new Map();
|
const activeNotificationsMap = new Map();
|
||||||
const models = Self.app.models;
|
|
||||||
|
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
|
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
const roles = await models.RoleMapping.find({
|
const availableNotificationsMap = await Self.getAvailable(id, myOptions);
|
||||||
fields: ['roleId'],
|
const activeNotifications = await Self.app.models.NotificationSubscription.find({
|
||||||
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({
|
|
||||||
fields: ['id', 'notificationFk'],
|
fields: ['id', 'notificationFk'],
|
||||||
include: {relation: 'notification'},
|
include: {relation: 'notification'},
|
||||||
where: {userFk: id}
|
where: {userFk: id}
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
for (acl of availableNotifications) {
|
for (active of activeNotifications) {
|
||||||
notifications.set(acl.notificationFk, {
|
activeNotificationsMap.set(active.notificationFk, {
|
||||||
id: null,
|
id: active.id,
|
||||||
notificationFk: acl.notificationFk,
|
notificationFk: active.notificationFk,
|
||||||
name: acl.notification().name,
|
name: active.notification().name,
|
||||||
description: acl.notification().description,
|
description: active.notification().description,
|
||||||
active: false,
|
active: true
|
||||||
});
|
});
|
||||||
|
availableNotificationsMap.delete(active.notificationFk);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (subscription of activeNotifications) {
|
return {
|
||||||
notifications.set(subscription.notificationFk, {
|
active: [...activeNotificationsMap.entries()],
|
||||||
id: subscription.id,
|
available: [...availableNotificationsMap.entries()]
|
||||||
notificationFk: subscription.notificationFk,
|
};
|
||||||
name: subscription.notification().name,
|
|
||||||
description: subscription.notification().description,
|
|
||||||
active: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return [...notifications.values()];
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -29,10 +29,46 @@ 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 available = await Self.getAvailable(workerId);
|
||||||
const hasAcl = notificationsAvailables.some(available => available.notificationFk === notificationFk);
|
const hasAcl = available.has(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');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue