-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { CanActivateFn } from '@angular/router'; | ||
|
||
import { travelperkTokenGuard } from './travelperk-token.guard'; | ||
|
||
describe('travelperkTokenGuard', () => { | ||
const executeGuard: CanActivateFn = (...guardParameters) => | ||
TestBed.runInInjectionContext(() => travelperkTokenGuard(...guardParameters)); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(executeGuard).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { Observable, catchError, map, throwError } from 'rxjs'; | ||
import { WorkspaceService } from '../services/common/workspace.service'; | ||
import { TravelperkService } from '../services/travelperk/travelperk.service'; | ||
import { globalCacheBusterNotifier } from 'ts-cacheable'; | ||
import { IntegrationsToastService } from '../services/common/integrations-toast.service'; | ||
import { TravelPerkOnboardingState, ToastSeverity } from '../models/enum/enum.model'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TravelperkTokenGuard { | ||
constructor( | ||
private travelperkService: TravelperkService, | ||
private router: Router, | ||
private toastService: IntegrationsToastService, | ||
private workspaceService: WorkspaceService | ||
) { } | ||
|
||
canActivate( | ||
route: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot | ||
): Observable<boolean> { | ||
const workspaceId = this.workspaceService.getWorkspaceId(); | ||
|
||
if (!workspaceId) { | ||
this.router.navigateByUrl('workspaces'); | ||
return throwError(() => new Error('Workspace not found')); | ||
} | ||
|
||
return this.travelperkService.getTravelperkData().pipe( | ||
map(() => true), | ||
catchError(error => { | ||
if (error.status === 400) { | ||
globalCacheBusterNotifier.next(); | ||
this.toastService.displayToastMessage(ToastSeverity.ERROR, 'Oops! Your TravelPerk connection expired, please connect again'); | ||
|
||
const onboardingState = this.workspaceService.getOnboardingState(); | ||
if (onboardingState !== TravelPerkOnboardingState.COMPLETE) { | ||
this.router.navigateByUrl('integrations/travelperk/onboarding/landing'); | ||
} else { | ||
this.router.navigateByUrl('integrations/travelperk/onboarding/landing'); | ||
} | ||
} | ||
return throwError(() => error); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters