Untitled

mail@pastecode.io avatar
unknown
typescript
7 months ago
1.9 kB
2
Indexable
Never
import { Observable } from 'rxjs';

export function canActivate(
    route, // ActivatedRouteSnapshot
    state, // RouterStateSnapshot
): Observable<boolean | UrlTree> {
    return new Observable((observer) => {
        // Goes to the Workspace or Contacts page.
        if (
            (state.url.indexOf('/lk/workspace') === 0 ||
                state.url.indexOf('/lk/contacts') === 0) &&
            this.accountRightsService.filtersWorkspace // Has read access to workspace filters.
        ) {
            // If the user has access to all devices and cases in the database.
            if (this.accountService.accessToAllImagesAndCases) {
                // Check if filters are ready.
                this.filtersHttpService.filtersWorkspaceReady().subscribe((ready) => {
                    if (ready.ready) {
                        // Loading into cache.
                        this.treeCacheService.loadWorkspace().subscribe(() => {
                            // Cache loaded.
                            observer.next(true);
                            observer.complete();
                        });
                    } else {
                        // Cache not loaded.
                        if (this.accountRightsService.viewCase) {
                            observer.next(this.router.createUrlTree(['/lk/cases']));
                            observer.complete();
                        } else {
                            observer.next(this.router.createUrlTree(['/lk/account']));
                            observer.complete();
                        }
                    }
                });
            } else {
                observer.next(true);
                observer.complete();
            }
        } else {
            // other pages
            observer.next(true);
            observer.complete();
        }
    });
}
Leave a Comment