import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { Observable, throwError, of, EMPTY, forkJoin } from 'rxjs'; import { map, tap, mergeMap, catchError, take, exhaustMap, concatMap, switchMap, withLatestFrom } from 'rxjs/operators'; import { Store, select } from '@ngrx/store'; import { ProtocolService } from '@ucap-webmessenger/protocol'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { PublicApiService, VersionInfo2Response } from '@ucap-webmessenger/api-public'; import { LoginInfo, KEY_LOGIN_INFO, EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '../types'; import { InnerProtocolService } from '@ucap-webmessenger/protocol-inner'; import { AuthenticationProtocolService, SSOMode, LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import * as AuthenticationStore from '@app/store/account/authentication'; import { ConnResponse } from 'projects/ucap-webmessenger-protocol-inner/src/lib/models/conn'; import { PiService } from '@ucap-webmessenger/pi'; import { NGXLogger } from 'ngx-logger'; import { QueryProtocolService } from '@ucap-webmessenger/protocol-query'; import { OptionProtocolService } from '@ucap-webmessenger/protocol-option'; import * as AppStore from '@app/store'; import * as VersionInfoStore from '@app/store/setting/version-info'; import * as OptionStore from '@app/store/setting/option'; import * as QueryStore from '@app/store/setting/query'; import * as SyncStore from '@app/store/messenger/sync'; @Injectable() export class AppMessengerResolver implements Resolve { constructor( private store: Store, private sessionStorageService: SessionStorageService, private publicApiService: PublicApiService, private piService: PiService, private protocolService: ProtocolService, private queryProtocolService: QueryProtocolService, private optionProtocolService: OptionProtocolService, private authenticationProtocolService: AuthenticationProtocolService, private innerProtocolService: InnerProtocolService, private logger: NGXLogger ) {} resolve( activatedRouteSnapshot: ActivatedRouteSnapshot, routerStateSnapshot: RouterStateSnapshot ): void | Observable | Promise { return new Promise((resolve, reject) => { let versionInfo2Res: VersionInfo2Response; let connRres: ConnResponse; let loginRes: LoginResponse; const loginInfo = this.sessionStorageService.get( KEY_LOGIN_INFO ); const environmentsInfo = this.sessionStorageService.get( KEY_ENVIRONMENTS_INFO ); this.publicApiService .versionInfo2({ deviceType: environmentsInfo.deviceType, companyGroupType: loginInfo.companyGroupType, companyCode: loginInfo.companyCode, loginId: loginInfo.loginId }) .pipe( take(1), switchMap(res => { versionInfo2Res = res; return this.protocolService.connect(versionInfo2Res.serverIp); }), switchMap(() => this.innerProtocolService.conn({})), switchMap(res => { connRres = res; return this.authenticationProtocolService.login({ loginId: loginInfo.loginId, loginPw: loginInfo.loginPw, deviceType: environmentsInfo.deviceType, deviceId: ' ', token: '', localeCode: loginInfo.localeCode, pushId: ' ', companyCode: loginInfo.companyCode, passwordEncodingType: 1, clientVersion: '', reconnect: false, ip: 'localhost', hostName: '', ssoMode: SSOMode.AUTH, userSpecificInformation: 'PRO_000482', productId: 'PRO_000482', productName: 'EZMessenger' }); }), switchMap(res => forkJoin([ this.queryProtocolService.auth({ deviceType: environmentsInfo.deviceType }), this.optionProtocolService.regView({}) ]) ), map(([authRes, regViewRes]) => { this.store.dispatch( OptionStore.regViewSuccess({ res: regViewRes }) ); this.store.dispatch(QueryStore.authSuccess({ res: authRes })); }), withLatestFrom( this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.buddy2SyncDate) ), this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.group2SyncDate) ), this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.roomSyncDate) ) ), map(([_, buddy2SyncDate, group2SyncDate, roomSyncDate]) => { this.store.dispatch(SyncStore.buddy2({ syncDate: buddy2SyncDate })); this.store.dispatch(SyncStore.group2({ syncDate: group2SyncDate })); this.store.dispatch( SyncStore.room({ syncDate: roomSyncDate, localeCode: loginInfo.localeCode }) ); }), catchError(err => { return throwError(err); }) ) .subscribe( () => { this.store.dispatch( AuthenticationStore.loginSuccess({ loginInfo: loginRes }) ); resolve(); }, err => { reject(err); } ); }); } }