All files / projects/organization-management/src/app/guards fetch-users.guard.ts

100% Statements 10/10
66.66% Branches 2/3
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2526x   26x   26x   26x         26x 3x   3x     3x 3x     3x      
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
 
import { getUserCount, loadUsers } from '../store/users';
 
/**
 * Fetch users for user management page
 */
export function fetchUsersGuard(route: ActivatedRouteSnapshot): boolean | Observable<boolean> {
  const store = inject(Store);
 
  return store.pipe(
    select(getUserCount),
    tap(count => {
      if (count <= 1 || !route.data.onlyInitialUsers) {
        store.dispatch(loadUsers());
      }
    }),
    map(() => true)
  );
}