import { Injectable } from '@angular/core' ; import { IUser } from '@gauzy/contracts' ; import { Router } from '@angular/router' ; import { NgxPermissionsService } from 'ngx-permissions' ; import { UsersService } from '../../@core/services' ; import { AuthStrategy } from '../../@core/auth/auth-strategy.service' ; import { Store } from '../../@core/services/store.service' ; @ Injectable ( { providedIn : 'root' } ) export class AppInitService { user : IUser ; constructor ( private readonly usersService , private readonly authStrategy , private readonly router , private readonly store , private readonly ngxPermissionsService ) { } async init ( ) { try { const id = this . store . userId ; if ( id ) { this . user = await this . usersService . getMe ( [ 'employee' , 'role' , 'role.rolePermissions' , 'tenant' , 'tenant.featureOrganizations' , 'tenant.featureOrganizations.feature' ] ) ; this . authStrategy . electronAuthentication ( { user : this . user , token : this . store . token } ) ; if ( ! this . user ?. tenantId ) { this . router . navigate ( [ '/onboarding/tenant' ] ) ; return ; } this . store . user = this . user ; const { tenant } = this . user ; this . store . featureTenant = tenant . featureOrganizations . filter ( ( item ) => ! item . organizationId ) ; this . store . userRolePermissions = this . user . role . rolePermissions . filter ( ( permission ) => permission . enabled ) ; const permissions = this . store . userRolePermissions . map ( ( { permission } ) => permission ) ; this . ngxPermissionsService . loadPermissions ( permissions ) ; } } catch ( error ) { console . log ( error ) ; } } }