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 = 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: 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'], 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 (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()]; }; };