-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Travelperk Guard #1132
base: master
Are you sure you want to change the base?
feat: Travelperk Guard #1132
Changes from all commits
83dcfc6
25d79bf
32be093
5165d08
edfafdc
7a8a91f
4be6423
39cb013
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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> { | ||
return this.travelperkService.getTravelperkTokenHealth().pipe( | ||
map(() => true), | ||
catchError(error => { | ||
if (error.status === 400) { | ||
globalCacheBusterNotifier.next(); | ||
this.toastService.displayToastMessage(ToastSeverity.ERROR, 'Oops! Your TravelPerk connection expired, please connect again'); | ||
this.router.navigateByUrl('integrations/travelperk/onboarding/landing'); | ||
} | ||
return throwError(() => error); | ||
}) | ||
); | ||
} | ||
Comment on lines
+21
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance error handling to cover all failure scenarios The current implementation only handles 400 status explicitly, but other error scenarios (like network issues, 500s) should also be handled gracefully. canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> {
return this.travelperkService.getTravelperkTokenHealth().pipe(
map(() => true),
catchError(error => {
- if (error.status === 400) {
+ if (error.status === 400 && error.error?.is_expired) {
globalCacheBusterNotifier.next();
this.toastService.displayToastMessage(ToastSeverity.ERROR, 'Oops! Your TravelPerk connection expired, please connect again');
this.router.navigateByUrl('integrations/travelperk/onboarding/landing');
+ } else {
+ this.toastService.displayToastMessage(ToastSeverity.ERROR, 'Failed to verify TravelPerk connection');
}
return throwError(() => error);
})
);
}
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import { catchError, Observable, Subject, throwError } from 'rxjs'; | ||
import { Cacheable, CacheBuster } from 'ts-cacheable'; | ||
import { Travelperk, TravelperkConfiguration, TravelperkDestinationAttribuite } from '../../models/travelperk/travelperk.model'; | ||
import { ApiService } from '../common/api.service'; | ||
|
@@ -30,7 +30,25 @@ export class TravelperkService { | |
} | ||
|
||
getTravelperkData(): Observable<Travelperk> { | ||
return this.apiService.get(`/orgs/${this.orgId}/travelperk/`, {}); | ||
return this.apiService.get(`/orgs/${this.orgId}/travelperk/`, {}).pipe( | ||
catchError(error => { | ||
if (error.status === 400 && error.error?.message?.includes('token expired')) { | ||
error.error.is_expired = true; | ||
} | ||
return throwError(() => error); | ||
}) | ||
); | ||
} | ||
Comment on lines
+33
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Extract error handling logic to a reusable operator The error handling logic is duplicated between private handleTravelperkError = () => <T>(source: Observable<T>): Observable<T> => {
return source.pipe(
catchError(error => {
if (error.status === 400) {
if (error.error?.message?.includes('token expired') || error.error?.is_expired) {
error.error.is_expired = true;
}
}
return throwError(() => error);
})
);
};
// Usage:
getTravelperkData(): Observable<Travelperk> {
return this.apiService.get(`/orgs/${this.orgId}/travelperk/`, {}).pipe(
this.handleTravelperkError()
);
} |
||
|
||
getTravelperkTokenHealth(): Observable<{}> { | ||
return this.apiService.get(`/orgs/${this.orgId}/travelperk/token_health/`, {}).pipe( | ||
catchError(error => { | ||
if (error.status === 400) { | ||
error.error.is_expired = true; | ||
} | ||
return throwError(() => error); | ||
}) | ||
); | ||
anishfyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
connectTravelperk(): Observable<{}>{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add return type for getTravelperkTokenHealth response
The service method's response type is
{}
. Consider creating an interface for better type safety.