import {filter, mergeMap} from 'rxjs/operators'; import { Injectable } from '@angular/core'; import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router'; import {Observable} from 'rxjs'; import {Session} from '../../login/utils/helper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {CommunityService} from '../community/community.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import {ConnectHelper} from '../connectHelper'; @Injectable() export class ConnectSubscriberGuard implements CanActivate { constructor(private router: Router, private communityService: CommunityService, private propertiesService: EnvironmentSpecificService) {} check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; if (!Session.isLoggedIn()) { errorCode = LoginErrorCodes.NOT_LOGIN; this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path, communityId:community}}); return false; } else { const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => { let communityDomain = null; //if(!community){ communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain); //} if(communityDomain) { community = communityDomain; } return this.communityService.isSubscribedToCommunity( community, Session.getUserEmail(), properties["adminToolsAPIURL"]) })); obs.pipe(filter(enabled => !enabled)) .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { 'errorCode': errorCode, 'redirectUrl': path, communityId:community } })); return obs; } } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { return this.check(route.queryParams['communityId'], state.url); } }