Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {Injectable} from "@angular/core"; |
| 2 | import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from "@angular/router"; |
| 3 | import {NgRedux} from "@angular-redux/store"; |
| 4 | import {Observable} from "rxjs"; |
| 5 | import {HttpClient} from "@angular/common/http"; |
| 6 | import {AppState} from "../../../shared/store/reducers"; |
| 7 | import {updateDrawingBoardStatus} from "../../../shared/storeUtil/utils/global/global.actions"; |
| 8 | import {DrawingBoardModes} from "../../service-planning/drawing-board.modes"; |
| 9 | import {of} from "rxjs"; |
| 10 | |
| 11 | @Injectable() |
| 12 | export 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 | |
| 54 | export class DrawingBoardPermissions { |
| 55 | isEditPermitted : boolean |
| 56 | } |