From 83dcfc68d87c8d2f6d8b7cec3367e25b2630955d Mon Sep 17 00:00:00 2001 From: anishfyle Date: Thu, 19 Dec 2024 12:12:23 +0530 Subject: [PATCH] feat: Travelperk Guard --- .../core/guard/travelperk-token.guard.spec.ts | 17 +++++++ src/app/core/guard/travelperk-token.guard.ts | 50 +++++++++++++++++++ .../travelperk-onboarding-routing.module.ts | 10 ++-- .../travelperk/travelperk-routing.module.ts | 4 +- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/app/core/guard/travelperk-token.guard.spec.ts create mode 100644 src/app/core/guard/travelperk-token.guard.ts diff --git a/src/app/core/guard/travelperk-token.guard.spec.ts b/src/app/core/guard/travelperk-token.guard.spec.ts new file mode 100644 index 000000000..0e65ae592 --- /dev/null +++ b/src/app/core/guard/travelperk-token.guard.spec.ts @@ -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(); + }); +}); diff --git a/src/app/core/guard/travelperk-token.guard.ts b/src/app/core/guard/travelperk-token.guard.ts new file mode 100644 index 000000000..59a2931e8 --- /dev/null +++ b/src/app/core/guard/travelperk-token.guard.ts @@ -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 { + 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); + }) + ); + } +} \ No newline at end of file diff --git a/src/app/integrations/travelperk/travelperk-onboarding/travelperk-onboarding-routing.module.ts b/src/app/integrations/travelperk/travelperk-onboarding/travelperk-onboarding-routing.module.ts index b15d87bea..df433db1d 100644 --- a/src/app/integrations/travelperk/travelperk-onboarding/travelperk-onboarding-routing.module.ts +++ b/src/app/integrations/travelperk/travelperk-onboarding/travelperk-onboarding-routing.module.ts @@ -5,6 +5,7 @@ import { TravelperkOnboardingAdvancedSettingsComponent } from './travelperk-onbo import { TravelperkOnboardingComponent } from './travelperk-onboarding.component'; import { TravelperkOnboardingLandingComponent } from './travelperk-onboarding-landing/travelperk-onboarding-landing.component'; import { TravelperkOnboardingDoneComponent } from './travelperk-onboarding-done/travelperk-onboarding-done.component'; +import { TravelperkTokenGuard } from 'src/app/core/guard/travelperk-token.guard'; const routes: Routes = [ { @@ -17,15 +18,18 @@ const routes: Routes = [ }, { path: 'payment_profile_settings', - component: TravelperkOnboardingPaymentProfileSettingsComponent + component: TravelperkOnboardingPaymentProfileSettingsComponent, + canActivate: [TravelperkTokenGuard] }, { path: 'advanced_settings', - component: TravelperkOnboardingAdvancedSettingsComponent + component: TravelperkOnboardingAdvancedSettingsComponent, + canActivate: [TravelperkTokenGuard] }, { path: 'done', - component: TravelperkOnboardingDoneComponent + component: TravelperkOnboardingDoneComponent, + canActivate: [TravelperkTokenGuard] } ] } diff --git a/src/app/integrations/travelperk/travelperk-routing.module.ts b/src/app/integrations/travelperk/travelperk-routing.module.ts index 0f4fcc4c3..d01999d9e 100644 --- a/src/app/integrations/travelperk/travelperk-routing.module.ts +++ b/src/app/integrations/travelperk/travelperk-routing.module.ts @@ -1,6 +1,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { TravelperkComponent } from './travelperk.component'; +import { TravelperkTokenGuard } from 'src/app/core/guard/travelperk-token.guard'; const routes: Routes = [ { @@ -13,7 +14,8 @@ const routes: Routes = [ }, { path: 'main', - loadChildren: () => import('./travelperk-main/travelperk-main.module').then(m => m.TravelperkMainModule) + loadChildren: () => import('./travelperk-main/travelperk-main.module').then(m => m.TravelperkMainModule), + canActivate: [TravelperkTokenGuard] } ] }