import { Injectable } from '@angular/core'; import { User } from '@core/typings/client-user.typing'; import { PermissionSet } from '@core/typings/permission.typing'; import { BaseRole, Role, RoleToSave } from '@core/typings/roles.typing'; import { ClientUserService } from '@features/client-user/client-user.service'; import { TypeaheadSelectOption } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; import { AttachYCState, BaseYCService } from '@yourcause/common/state'; import { RolesResources } from './roles.resources'; import { RolesState } from './roles.state'; @AttachYCState(RolesState) @Injectable({ providedIn: 'root' }) export class RolesService extends BaseYCService { constructor ( private logger: LogService, private clientUserService: ClientUserService, private rolesResources: RolesResources, private notifier: NotifierService, private i18n: I18nService ) { super(); } get roles () { return this.get('roles'); } getDetachedPermissionSets () { return this.get('permissionSets').map((set) => ({ ...set, permissions: [ ...set.permissions ] })); } getPermissionSetByType (type: number) { return this.get('permissionSets').find(set => set.type === type); } getPermissionSetOptions () { return this.getDetachedPermissionSets().map>((set) => ({ label: this.i18n.translate( this.getPermissionSetKey(set.type), {}, set.name ), value: set })); } getPermissionSetKey (setType: number): string { return `permissions:lblSet${setType}Name`; } getPermissionKey ( permissionSetType: number, permissionType: number ) { // rigs the i18n test to not detect this return 'permissions' + ':lblPermission' + permissionSetType + permissionType + 'Name'; } attachUsers (roleId: number, users: User[]) { this.set('userRoleMap', { ...this.get('userRoleMap'), [roleId]: users }); } getUsersByRoleId (id: number) { return this.get('userRoleMap')[id]; } getRoleById (id: number) { return this.get('roles').find(role => role.id === id); } getBlankRole (): BaseRole { return { roleName: '', roleDescription: '', allowedPermissions: [], deniedPermissions: [] }; } resetUserMap (roleId: number) { this.set('userRoleMap', { ...this.get('userRoleMap'), [roleId]: undefined }); } getPermissionMap ( permissionSets = this.get('permissionSets') ) { return permissionSets.reduce((fullMap, set) => ({ ...fullMap, [set.type]: set.permissions .reduce((setMap, permission) => ({ ...setMap, [permission.type]: permission }), { name: set.name }) }), {}); } async getRoles (force = false): Promise { if (force || !this.roles) { const roles = await this.rolesResources.getRoles(); this.set('roles', roles); return roles; } return this.roles; } async saveRole (roleToSave: RoleToSave) { try { await this.rolesResources.saveRole(roleToSave); await this.clientUserService.getUser(true); await this.getRoles(true); this.notifier.success(this.i18n.translate( 'common:textSuccessfullySavedRole', {}, 'Successfully saved the role' )); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'common:textErrorSavingRole', {}, 'There was an error saving the role' )); } } async getUsersInRole (roleId: number): Promise { const users = await this.rolesResources.getUsersInRole(roleId); this.attachUsers(roleId, users); return users; } async getPermissions (): Promise { const permissionSets = await this.rolesResources.getPermissions(); this.set('permissionSets', permissionSets); return permissionSets; } }