All files / src/app/core/utils/role-toggle role-toggle.service.ts

100% Statements 15/15
87.5% Branches 7/8
100% Functions 7/7
100% Lines 12/12

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 25 26 27 28 29 30 318x 8x   8x   8x 8x   8x   148x 187x       8x       4x       4x     4x        
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { getUserRoles } from 'ish-core/store/customer/authorization';
import { whenTruthy } from 'ish-core/utils/operators';
 
export function checkRole(roleIds: string[], roleId: string | string[]): boolean {
  // eslint-disable-next-line no-param-reassign
  roleId = typeof roleId === 'string' ? [roleId] : typeof roleId === 'undefined' ? [] : roleId;
  return roleId.some(id => roleIds.includes(id));
}
 
@Injectable({ providedIn: 'root' })
export class RoleToggleService {
  private roleIds$: Observable<string[]>;
 
  constructor(store: Store) {
    this.roleIds$ = store.pipe(select(getUserRoles)).pipe(map(roles => roles.map(role => role.roleId)));
  }
 
  hasRole(roleId: string | string[]): Observable<boolean> {
    return this.roleIds$.pipe(
      // wait for user roles to be loaded
      whenTruthy(),
      map(roleIds => checkRole(roleIds, roleId))
    );
  }
}