import { Injectable } from '@angular/core'; import {ActivatedRoute, Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/filter'; import { ConfigurationService } from '../utils/configuration/configuration.service'; import{EnvProperties} from '../utils/properties/env-properties'; @Injectable() export class IsRouteEnabled implements CanActivate { constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { let customRedirect = route.data['redirect']; let api = route.data['api']; let community = route.data['community'] if(!community){ community = (route.queryParams["communityId"])?route.queryParams["communityId"]:route.queryParams["community"]; } if(community){ let isEnabled = this.config.isPageEnabled(api, community,"/"+state.url.split("?")[0].substring(1)); let redirect = !!customRedirect ? customRedirect : '/error'; isEnabled.filter(enabled => !enabled) .subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } })); return isEnabled; } return true; } }