import { Component , OnInit } from '@angular/core' ; import { saveAs } from 'file-saver' ; import { Router , UrlSerializer } from '@angular/router' ; import { catchError , concatMap , delay , filter , finalize , switchMap , tap } from 'rxjs/operators' ; import { EMPTY , firstValueFrom , of as observableOf } from 'rxjs' ; import { TranslateService } from '@ngx-translate/core' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import { ITenant , IAuthResponse , IOrganization , IOrganizationCreateInput , IUser , IUserRegistrationInput , PermissionsEnum , BonusTypeEnum , CurrenciesEnum , DefaultValueDateTypeEnum , ImportTypeEnum , IUserOrganization } from '@gauzy/contracts' ; import { environment } from './../../../environments/environment' ; import { Environment } from './../../../environments/model' ; import { ErrorHandlingService , ExportAllService , GauzyCloudService , Store , ToastrService , UsersOrganizationsService } from '../../@core/services' ; import { TranslationBaseComponent } from '../../@shared/language-base/translation-base.component' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ngx-import-export' , templateUrl : './import-export.html' } ) export class ImportExportComponent extends TranslationBaseComponent implements OnInit { user : IUser ; environment : Environment = environment ; permissionsEnum = PermissionsEnum ; loading : boolean ; token : string ; gauzyUser : IUser ; userOrganizations : IUserOrganization [ ] = [ ] ; constructor ( private readonly exportAll , private readonly gauzyCloudService , private readonly userOrganizationService , private readonly router , private readonly store , readonly translateService , private readonly toastrService , private readonly serializer , private readonly _errorHandlingService ) { super ( translateService ) ; } ngOnInit ( ) { this . store . user$ . pipe ( filter ( ( user ) => ! ! user ) , tap ( ( user ) => ( this . user = user ) ) , tap ( ( ) => this . getOrganizations ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } importPage ( ) { this . router . navigate ( [ '/pages/settings/import-export/import' ] ) ; } exportPage ( ) { this . router . navigate ( [ '/pages/settings/import-export/export' ] ) ; } onDownloadTemplates ( ) { this . exportAll . downloadTemplates ( ) . pipe ( untilDestroyed ( this ) ) . subscribe ( ( data ) => saveAs ( data , `archive.zip` ) ) ; } onMigrateIntoCloud ( password ) { this . loading = true ; const { id : sourceId , firstName , lastName , username , thirdPartyId , email , imageUrl , preferredComponentLayout , preferredLanguage , tenant : { id : tenantId , name } } = this . user ; const register = { user : { firstName , lastName , username , thirdPartyId , email , preferredComponentLayout , preferredLanguage , imageUrl } , isImporting : true , sourceId , password } ; try { this . gauzyCloudService . migrateIntoCloud ( register ) . pipe ( catchError ( ( error ) => { this . _errorHandlingService . handleError ( error ) ; return observableOf ( EMPTY ) ; } ) , switchMap ( ( response ) => { if ( response ) { const { token , user } = response ; this . token = token ; this . gauzyUser = user ; return this . gauzyCloudService . migrateTenant ( { name , isImporting : true , sourceId : tenantId , userSourceId : sourceId } , token ) ; } return EMPTY ; } ) , delay ( 1000 ) , concatMap ( async ( tenant ) => { if ( tenant ) { for await ( const { id : userOrganizationSourceId , organization } of this . userOrganizations ) { await firstValueFrom ( this . gauzyCloudService . migrateOrganization ( { ... this . mapOrganization ( organization , tenant , userOrganizationSourceId ) } , this . token ) ) ; } return await firstValueFrom ( observableOf ( tenant ) ) ; } return await firstValueFrom ( observableOf ( EMPTY ) ) ; } ) , tap ( { next : ( x ) => { this . toastrService . success ( 'MENU.IMPORT_EXPORT.MIGRATE_SUCCESSFULLY' , { tenant : name } ) ; const externalUrl = environment . GAUZY_CLOUD_APP ; const tree = this . router . createUrlTree ( [ '/pages/settings/import-export/import' ] , { queryParams : { token : this . token , userId : this . gauzyUser . id , importType : ImportTypeEnum . MERGE } } ) ; let redirect ; if ( externalUrl . indexOf ( '#' ) !== - 1 ) { redirect = externalUrl + '' + this . serializer . serialize ( tree ) ; } else { redirect = externalUrl + '/#' + this . serializer . serialize ( tree ) ; } setTimeout ( ( ) => { this . router . navigate ( [ '/pages/settings/import-export/external-redirect' , { redirect } ] ) ; } , 1500 ) ; } , error : ( err ) => { console . log ( 'tap error' , err ) ; this . _errorHandlingService . handleError ( err ) ; } , } ) , finalize ( ( ) => this . loading = false ) , untilDestroyed ( this ) ) . subscribe ( ) ; } catch ( error ) { this . toastrService . danger ( error ) ; } } async getOrganizations ( ) { const { id : userId , tenantId } = this . user ; const { items = [ ] } = await this . userOrganizationService . getAll ( [ 'organization' ] , { userId , tenantId } ) ; this . userOrganizations = items ; } mapOrganization ( organization , tenant , userOrganizationSourceId ) { const { currency , defaultValueDateType , bonusType , imageUrl , id : sourceId } = organization ; delete organization [ 'id' ] ; return { ... organization , imageUrl , tenant , tenantId : tenant . id , currency : currency as CurrenciesEnum , defaultValueDateType : defaultValueDateType as DefaultValueDateTypeEnum , bonusType : bonusType as BonusTypeEnum , isImporting : true , sourceId , userOrganizationSourceId } ; } }