All files / src/app/core/services/authorization authorization.service.ts

88.23% Statements 15/17
75% Branches 3/4
80% Functions 4/5
92.85% Lines 13/14

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 31 32 33 34 3529x 29x 29x     29x     29x     29x   7x 7x       3x 2x   1x       1x           1x      
import { Injectable } from '@angular/core';
import { throwError } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { AuthorizationData } from 'ish-core/models/authorization/authorization.interface';
import { AuthorizationMapper } from 'ish-core/models/authorization/authorization.mapper';
import { Customer } from 'ish-core/models/customer/customer.model';
import { User } from 'ish-core/models/user/user.model';
import { ApiService } from 'ish-core/services/api/api.service';
 
@Injectable({ providedIn: 'root' })
export class AuthorizationService {
  constructor(
    private apiService: ApiService,
    private authorizationMapper: AuthorizationMapper
  ) {}
 
  getRolesAndPermissions(customer: Customer, user: User) {
    if (!customer?.customerNo) {
      return throwError(() => new Error('getRolesAndPermissions() called without customer.customerNo'));
    }
    Iif (!user?.login) {
      return throwError(() => new Error('getRolesAndPermissions() called without user.login'));
    }
 
    return this.apiService
      .get<AuthorizationData>(
        `customers/${this.apiService.encodeResourceId(customer.customerNo)}/users/${this.apiService.encodeResourceId(
          user.login
        )}/roles`
      )
      .pipe(map(data => this.authorizationMapper.fromData(data)));
  }
}