import { Component , OnDestroy , OnInit } from '@angular/core' ; import { ActivatedRoute , Params , Router } from '@angular/router' ; import { IUser , RolesEnum } from '@gauzy/contracts' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { TranslateService } from '@ngx-translate/core' ; import { firstValueFrom } from 'rxjs' ; import { filter , tap } from 'rxjs/operators' ; import { AuthService , UserIdService , UsersService } from '../../../../@core/services' ; import { TranslationBaseComponent } from '../../../../@shared/language-base' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ngx-edit-user-data' , templateUrl : './edit-user-data.component.html' } ) export class EditUserDataComponent extends TranslationBaseComponent implements OnInit , OnDestroy { params : Params ; user : IUser ; constructor ( private readonly route , public readonly translateService , private readonly router , private readonly userIdService , private readonly usersService , private readonly authService ) { super ( translateService ) ; } ngOnInit ( ) { this . route . params . pipe ( filter ( ( params ) => ! ! params ) , tap ( ( params ) => this . params = params ) , tap ( ( params ) => this . userIdService . userId = params . id ) , tap ( ( ) => this . getUserProfile ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } goBack ( ) { this . router . navigate ( [ '/pages/users' ] ) ; } private async getUserProfile ( ) { const { id } = this . params ; const user = await this . usersService . getUserById ( id , [ 'role' , 'tags' ] ) ; if ( user . role . name === RolesEnum . SUPER_ADMIN ) { /**
             * Redirect If Edit Super Admin Without Permission
             */ const hasSuperAdminRole = await firstValueFrom ( this . authService . hasRole ( [ RolesEnum . SUPER_ADMIN ] ) ) ; if ( ! hasSuperAdminRole ) { this . router . navigate ( [ '/pages/users' ] ) ; return ; } } this . user = user ; } ngOnDestroy ( ) { } }