All files / src/app/core/models/authorization authorization.mapper.ts

100% Statements 13/13
100% Branches 3/3
100% Functions 6/6
100% Lines 11/11

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   5x 4x 3x   1x 2x   11x 2x 11x     1x        
import { Injectable } from '@angular/core';
 
import { AuthorizationData } from './authorization.interface';
import { Authorization } from './authorization.model';
 
@Injectable({ providedIn: 'root' })
export class AuthorizationMapper {
  fromData(authorizationData: AuthorizationData): Authorization {
    if (authorizationData) {
      if (!authorizationData.userRoles?.length) {
        return { permissionIDs: [], roles: [] };
      }
      return {
        roles: authorizationData.userRoles.map(role => ({ displayName: role.roleDisplayName, roleId: role.roleID })),
        permissionIDs: authorizationData.userRoles
          .map(role => role.permissions.map(p => p.permissionID))
          .reduce((acc, val) => [...acc, ...val], [])
          .filter((v, i, a) => a.indexOf(v) === i),
      };
    } else {
      throw new Error(`authorization data is required`);
    }
  }
}