blob: 4a305b1931a105bc39c5ce39cb0aeebb18ffea2b [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Injectable} from "@angular/core";
2import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from "@angular/router";
3import {NgRedux} from "@angular-redux/store";
4import {Observable} from "rxjs";
5import {HttpClient} from "@angular/common/http";
6import {AppState} from "../../../shared/store/reducers";
7import {updateDrawingBoardStatus} from "../../../shared/storeUtil/utils/global/global.actions";
8import {DrawingBoardModes} from "../../service-planning/drawing-board.modes";
9import {of} from "rxjs";
10
11@Injectable()
12export class DrawingBoardGuard implements CanActivate {
13 constructor(private store: NgRedux<AppState>, private _http: HttpClient, private _router : Router) { }
14 canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
15 let url : string = `/vid/roles/service_permissions?subscriberId=${next.queryParams['subscriberId']}&serviceType=${next.queryParams['serviceType']}`;
16 const viewMode = state.url.includes(DrawingBoardModes.RETRY_EDIT)? DrawingBoardModes.RETRY: DrawingBoardModes.VIEW;
17 return this._http.get(url)
18 .map((result : DrawingBoardPermissions) => {
19 if(!result.isEditPermitted){
20 this.navigateToNewViewEdit(next.queryParams, viewMode);
21 return false;
22 }else {
23 return true;
24 }
25 }).catch(err => {
26 this.navigateToNewViewEdit(next.queryParams, viewMode);
27 return of(false);
28 });
29 }
30
31 navigateToNewViewEdit(queryParams: any, mode: DrawingBoardModes): void{
32 this.store.dispatch(updateDrawingBoardStatus(mode));
33 const viewEditUrlTree = this.getNewViewEditUrlTree(queryParams, mode);
34 this._router.navigateByUrl(viewEditUrlTree);
35 window.parent.location.assign('../../serviceModels.htm#'+viewEditUrlTree.toString());
36 }
37
38 getNewViewEditUrlTree(queryParams: any, mode: DrawingBoardModes) {
39 return this._router.createUrlTree(
40 ['/servicePlanning/' + mode],
41 {
42 queryParams:
43 {
44 serviceModelId: queryParams['serviceModelId'],
45 serviceInstanceId: queryParams['serviceInstanceId'],
46 serviceType : queryParams['serviceType'],
47 subscriberId : queryParams['subscriberId'],
48 jobId : queryParams['jobId'],
49 }
50 });
51 }
52}
53
54export class DrawingBoardPermissions {
55 isEditPermitted : boolean
56}