import { IWCToken } from './../../shared/modal/common'; import { Component, OnInit } from '@angular/core'; import { Router, ActivatedRoute } from "@angular/router"; import { ERROR_LIST } from '../../shared/error.constants'; import { SharedService } from './../../shared/shared.service'; import { UserService } from './../../services/user.service'; import { User } from './../../shared/modal/user'; import { environment } from '../../../environments/environment'; import { OktaAuthService } from '@okta/okta-angular'; import UserUtils from 'app/shared/user.utils'; declare var $: any; @Component({ selector: 'prohibited-cmp', moduleId: module.id, templateUrl: './prohibited.component.html', styleUrls: ['./prohibited.component.css'] }) export class ProhibitedComponent implements OnInit { private COMPONENT_NAME: string = 'ProhibitedComponent()'; test: Date = new Date(); errorCode:string; errorMsg = ''; storeId: number; inputData: any; public constructor(private route: ActivatedRoute, private sharedService: SharedService, public oktaAuth: OktaAuthService, public router: Router, private userService: UserService) { this.route.queryParams.subscribe(params => { this.errorCode = params["errorCode"]; if(params["inputData"]){ this.inputData = params["inputData"]; } }); } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated. * @return {void} */ ngOnInit(): void { this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; console.log('Prohibited Component Store ID -->>',this.storeId); }); } get errorMessage(): string{ if(this.errorCode){ const filterErrors = ERROR_LIST.filter((item) => { return item.key == this.errorCode; }); if(filterErrors && filterErrors.length > 0) { return filterErrors[0].value; } } return null; } openMenuItems(): void { if ($('.dropdown-header.settings').hasClass('open')) { $('.dropdown-header.settings').removeClass('open'); } if ($('.dropdown-header.address').hasClass('open')) { $('.dropdown-header.address').removeClass('open'); } } logout() { const METHOD_NAME: string = 'logout()'; console.log('NavbarComponent.notifications()>>Start.'); this.errorMsg = ''; this.sharedService.showSpinner(true); this.sharedService.clearLocalStorage(); this.sharedService.clearSessionStorage(true); this.redirectLoginPage(); } async redirectLoginPage(): Promise { if(environment.oktaEnable) { await this.oktaAuth.logout(); this.oktaAuth.loginRedirect('/login') }else{ this.router.navigate(['login']); } } async relogin() : Promise{ const METHOD_NAME: string = 'relogin()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Agent relogin in'); console.log('ProhibitedComponent.relogin()>>Start.'); this.sharedService.showSpinner(true); this.userService.login(this.storeId, this.inputData).subscribe( data => { this.sharedService.showSpinner(false); console.log('ProhibitedComponent.relogin()>>login success..'); let wcToken:IWCToken = Object.assign({}, data); wcToken.logonId = this.inputData.loginId; wcToken.password = this.inputData.loginPassword; this.profile( wcToken ); }, error => { this.sharedService.showSpinner(false); console.error('error=>' + error); // WCS Login Failed. this.errorCode = '_ERR_WCS_LOGIN'; //this.router.navigate(['login/prohibited']); }); console.log('ProhibitedComponent.relogin()>>End.'); } async profile(wcToken: IWCToken): Promise { const METHOD_NAME: string = 'profile()'; console.log('ProhibitedComponent.profile()>>Start.'); this.sharedService.showSpinner(true); this.userService.getProfile(this.storeId, wcToken.userId).subscribe( data => { this.sharedService.showSpinner(false); console.log('ProhibitedComponent.login()>>User prfile success..'); let user:User = Object.assign(wcToken, data); UserUtils.updateFavorites(user); UserUtils.setPermissionsForAgent(user); if (user.channels && user.channels.length > 0) { if(UserUtils.hasAccessStore(this.storeId, user)){ UserUtils.activeSelectedStore(this.storeId, user); this.userService.setUser(user); this.router.navigate(['home/search']); //this.navigateErrorPage('_ERR_CHANNEL_ACCESS_DENIED');//added for testing }else{ let accStroreId = UserUtils.getAccessStore(user); if(accStroreId && accStroreId > 0) { this.sharedService.updateStoreId(accStroreId); this.relogin(); //reloging in } else { this.errorCode='_ERR_CHANNEL_ACCESS_DENIED'; } } } else { this.errorCode= '_ERR_CHANNEL_ACCESS_DENIED'; } }, error => { this.sharedService.showSpinner(false); console.error('error=>' + error); this.navigateErrorPage(error); }); console.log('ProhibitedComponent.profile()>>End.'); } navigateErrorPage(errors: any): void{ let errorCode: string = '_ERR_GENERIC'; if(errors && typeof (errors) === 'string') { const filterErrors = ERROR_LIST.filter((item) => { return errors.indexOf(item.key) !== -1 }); if(filterErrors && filterErrors.length > 0) { errorCode = filterErrors[0].key; } } } }