Skip to content

Commit

Permalink
feat: Travelperk Guard
Browse files Browse the repository at this point in the history
  • Loading branch information
anishfyle committed Dec 19, 2024
1 parent 7e8db09 commit 83dcfc6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/app/core/guard/travelperk-token.guard.spec.ts
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) =>

Check failure on line 7 in src/app/core/guard/travelperk-token.guard.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
TestBed.runInInjectionContext(() => travelperkTokenGuard(...guardParameters));

beforeEach(() => {
TestBed.configureTestingModule({});
});

it('should be created', () => {
expect(executeGuard).toBeTruthy();
});
});
50 changes: 50 additions & 0 deletions src/app/core/guard/travelperk-token.guard.ts
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);
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand All @@ -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]
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/integrations/travelperk/travelperk-routing.module.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand All @@ -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]
}
]
}
Expand Down

0 comments on commit 83dcfc6

Please sign in to comment.