import { Injectable } from '@angular/core'; import { BasicUserWorkflowLevel } from '@core/typings/client-user.typing'; import { GrantManagerUser } from '@core/typings/grant-manager.typing'; import { UserRoleExport, UserWorkflowExport } from '@core/typings/user.typing'; import { WorkflowLevelPermissions } from '@core/typings/workflow.typing'; import { RolesService } from '@features/roles/roles.service'; import { FileService, PaginationOptions, TableDataDownloadFormat } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; import { unparse } from 'papaparse'; import { UserResources } from './user.resources'; @Injectable({ providedIn: 'root' }) export class UserExportService { constructor ( private logger: LogService, private userResources: UserResources, private roleService: RolesService, private i18n: I18nService, private notifier: NotifierService, private fileService: FileService ) { } private async getAllUsersWithRoles () { await Promise.all([ this.i18n.namespaceReady('ROLES'), this.roleService.getPermissions() ]); const users = await this.userResources.getUsersPaginatedWithPolicies({ filterColumns: [], orFilterColumns: [], pageNumber: 0, retrieveTotalRecordCount: false, returnAll: true, rowsPerPage: 0, sortColumns: [] }); return users.users.records; } /** * Returns the text for workflow level permissions for things like users export * * @param workflowLevel this will have the details used to determine the permissions for the user/workflow level combo * @returns text that is used in things like csv/excel exports to show the permissions for a given user/workflow level combo */ getWorkflowLevelPermissionForUser ( workflowLevel: BasicUserWorkflowLevel ) { const workflowManager = this.i18n.translate( 'WORKFLOW:textWorkflowManager', {}, 'Workflow manager' ); const workflowOnly = this.i18n.translate( 'WORKFLOW:textWorkflowLevelOnly', {}, 'Workflow level only' ); const workflowPassthrough = this.i18n.translate( 'WORKFLOW:textWorkflowPassthrough', {}, 'Workflow passthrough' ); if (workflowLevel.workflowLevelUserAccessType) { switch (workflowLevel.workflowLevelUserAccessType) { case WorkflowLevelPermissions.MANAGER: return workflowManager; default: case WorkflowLevelPermissions.LEVEL_ONLY: return workflowOnly; case WorkflowLevelPermissions.PASSTHROUGH: return workflowPassthrough; } } else { return workflowLevel.workflow.workflowManager ? workflowManager : workflowOnly; } } async getUserWFLs (): Promise { const users = await this.getAllUsersWithRoles(); await this.i18n.ensureTranslate( 'WORKFLOW:textWorkflowPassthrough', {}, 'Workflow passthrough' ); await this.i18n.ensureTranslate( 'WORKFLOW:textWorkflowLevelOnly', {}, 'Workflow level only' ); await this.i18n.ensureTranslate( 'WORKFLOW:textWorkflowManager', {}, 'Workflow manager' ); return users.reduce((acc, user) => { return [ ...acc, ...user.workFlowLevels.map((workflowLevel) => { const workflowLevelPermission = this.getWorkflowLevelPermissionForUser(workflowLevel); return { 'Employee First Name': user.firstName, 'Employee Last Name': user.lastName, 'Employee Email': user.email, 'Is Employee Active': !user.isDeactivated, Workflow: workflowLevel.workflow.name, 'Workflow Level': workflowLevel.name, 'Workflow Level Permission': workflowLevelPermission }; }) ]; }, []); } async exportUserWFLs (downloadFormat: TableDataDownloadFormat) { try { const userWFLs = await this.getUserWFLs(); let csv: string; if (userWFLs.length === 0) { csv = 'Employee First Name,Employee Last Name,Employee Email,Workflow,Workflow Level,Workflow Level Permission'; } else { csv = unparse(userWFLs); } this.fileService.downloadByFormat(csv, downloadFormat); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'ROLES:textErrorExportingUserWFLs', {}, 'There was an error exporting the user workflow levels' )); } } async exportUsersGMPortal (getActiveUsers: boolean, getInactiveUsers: boolean, downloadFormat: TableDataDownloadFormat) { try { const response = await this.userResources.exportUsersGMPortal(getActiveUsers, getInactiveUsers); const csv = unparse(response); this.fileService.downloadByFormat(csv, downloadFormat); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'USERS:textErrorExportingUsersGMPortal', {}, 'There was an error exporting the users in GM Portal' )); } } async exportGrantManagersAdminPortal (paginationOptions: PaginationOptions, downloadFormat: TableDataDownloadFormat) { try { const response = await this.userResources.exportGrantManagersAdminPortal(paginationOptions); const csv = unparse(response); this.fileService.downloadByFormat(csv, downloadFormat); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'USERS:textErrorExportingUsersAdminPortal', {}, 'There was an error exporting the users in Admin Portal' )); } } async getUserRoles (): Promise { const users = await this.getAllUsersWithRoles(); return users.reduce((acc, user) => { return [ ...acc, ...user.roles.map((role) => { return { 'Employee First Name': user.firstName, 'Employee Last Name': user.lastName, 'Employee Email': user.email, 'Is Employee Active': !user.isDeactivated, SSO: user.isSSO ? this.i18n.translate('common:textYes', {}, 'Yes') : this.i18n.translate('common:textNo', {}, 'No'), Role: role.clientRoleName, Permissions: role.policies.map(policy => { const foundPermissionSet = this.roleService.getDetachedPermissionSets() .find(set => { return set.type === policy.permissionSetType; }); const foundPermission = foundPermissionSet .permissions.find(permission => { return permission.type === policy.permissionType; }); if (foundPermission) { const permissionName = this.i18n.translate( this.roleService.getPermissionKey( policy.permissionSetType, policy.permissionType ), {}, foundPermission.name ); const permissionSetName = this.i18n.translate( this.roleService.getPermissionSetKey( foundPermissionSet.type ), {}, foundPermissionSet.name ); return permissionSetName + ' - ' + permissionName; } return ''; }) .filter(foundPermission => !!foundPermission) .join(', ') }; }) ]; }, []); } async exportUserRoles (downloadFormat: TableDataDownloadFormat) { try { const users = await this.getUserRoles(); let csv: string; if (users.length === 0) { csv = 'Employee First Name,Employee Last Name,Employee Email,SSO,Role,Permissions'; } else { csv = unparse(users); } this.fileService.downloadByFormat(csv, downloadFormat); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'ROLES:textErrorExportingUserRoles', {}, 'There was an error exporting the user roles' )); } } async exportWFLs (downloadFormat: TableDataDownloadFormat) { const response = await this.userResources.exportWFLs(); const csv = unparse(response); this.fileService.downloadByFormat(csv, downloadFormat); } async exportRoles (downloadFormat: TableDataDownloadFormat) { const response = await this.userResources.exportRoles(); const csv = unparse(response); this.fileService.downloadByFormat(csv, downloadFormat); } }