Updated user service to use different endpoint
This commit is contained in:
@@ -3,11 +3,13 @@ import { Injectable } from '@angular/core';
|
||||
import { SELECTED_ORGANIZATION_NUMBER_KEY } from '@msfa-constants/local-storage-keys';
|
||||
import { UnsubscribeDirective } from '@msfa-directives/unsubscribe.directive';
|
||||
import { environment } from '@msfa-environment';
|
||||
import { EmployeeResponse } from '@msfa-models/api/employee.response.model';
|
||||
import { OrganizationResponse } from '@msfa-models/api/organization.response.model';
|
||||
import { UserInfoResponse } from '@msfa-models/api/user-info.response.model';
|
||||
import { Employee, mapResponseToEmployee } from '@msfa-models/employee.model';
|
||||
import { mapResponseToOrganization, Organization } from '@msfa-models/organization.model';
|
||||
import { Role } from '@msfa-models/role.model';
|
||||
import { mapResponseToUserInfo, UserInfo } from '@msfa-models/user-info.model';
|
||||
import { User } from '@msfa-models/user.model';
|
||||
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
|
||||
import { filter, map, switchMap } from 'rxjs/operators';
|
||||
import { AuthenticationService } from './authentication.service';
|
||||
@@ -16,12 +18,16 @@ import { AuthenticationService } from './authentication.service';
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class UserService extends UnsubscribeDirective {
|
||||
private _apiBaseUrl = `${environment.api.url}/auth`;
|
||||
private _apiAuthUrl = `${environment.api.url}/auth`;
|
||||
private _apiUserUrl = `${environment.api.url}/users/currentUser`;
|
||||
|
||||
private _isLoggedIn$: Observable<boolean> = this.authenticationService.isLoggedIn$;
|
||||
private _organizations$ = new BehaviorSubject<Organization[]>(null);
|
||||
public organizations$: Observable<Organization[]> = this._organizations$.asObservable();
|
||||
private _user$ = new BehaviorSubject<User>(null);
|
||||
public user$: Observable<User> = this._user$.asObservable();
|
||||
private _user$ = new BehaviorSubject<Employee>(null);
|
||||
public user$: Observable<Employee> = this._user$.asObservable();
|
||||
private _userRoles$ = new BehaviorSubject<Role[]>(null);
|
||||
public userRoles$: Observable<Role[]> = this._userRoles$.asObservable();
|
||||
private _selectedOrganizationNumber$ = new BehaviorSubject<string>(null);
|
||||
|
||||
constructor(private httpClient: HttpClient, private authenticationService: AuthenticationService) {
|
||||
@@ -39,28 +45,37 @@ export class UserService extends UnsubscribeDirective {
|
||||
combineLatest([this._isLoggedIn$, this.selectedOrganization$])
|
||||
.pipe(
|
||||
filter(([loggedIn, selectedOrganization]) => !!(loggedIn && selectedOrganization)),
|
||||
switchMap(() => this._fetchUserInfo$())
|
||||
switchMap(() => combineLatest([this._fetchUserInfo$(), this._fetchCurrentUser$()]))
|
||||
)
|
||||
.subscribe(userInfo => {
|
||||
this._user$.next({ ...userInfo, organizations: this._organizations$.value });
|
||||
.subscribe(([userInfo, currentUser]) => {
|
||||
this._userRoles$.next(userInfo.roles);
|
||||
this._user$.next(currentUser);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private _fetchOrganizations$(): Observable<Organization[]> {
|
||||
return this.httpClient.get<{ data: OrganizationResponse[] }>(`${this._apiBaseUrl}/organizations`).pipe(
|
||||
return this.httpClient.get<{ data: OrganizationResponse[] }>(`${this._apiAuthUrl}/organizations`).pipe(
|
||||
filter(response => !!response?.data),
|
||||
map(({ data }) => data.map(organization => mapResponseToOrganization(organization)))
|
||||
);
|
||||
}
|
||||
|
||||
private _fetchUserInfo$(): Observable<UserInfo> {
|
||||
return this.httpClient.get<{ data: UserInfoResponse }>(`${this._apiBaseUrl}/userinfo`).pipe(
|
||||
return this.httpClient.get<{ data: UserInfoResponse }>(`${this._apiAuthUrl}/userinfo`).pipe(
|
||||
filter(response => !!response?.data),
|
||||
map(({ data }) => mapResponseToUserInfo(data))
|
||||
);
|
||||
}
|
||||
|
||||
private _fetchCurrentUser$(): Observable<Employee> {
|
||||
console.log('fetching current user');
|
||||
return this.httpClient.get<{ data: EmployeeResponse }>(`${this._apiUserUrl}`).pipe(
|
||||
filter(response => !!response?.data),
|
||||
map(({ data }) => mapResponseToEmployee(data))
|
||||
);
|
||||
}
|
||||
|
||||
private get _selectedOrganizationNumber(): string | null {
|
||||
return localStorage.getItem(SELECTED_ORGANIZATION_NUMBER_KEY);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RoleEnum } from '@msfa-enums/role.enum';
|
||||
import { environment } from '@msfa-environment';
|
||||
import { mapResponseToRoles, Role } from '@msfa-models/role.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RoleService {
|
||||
private readonly _apiBaseUrl = `${environment.api.url}/auth/userinfo`;
|
||||
public get allRoles(): Role[] {
|
||||
return mapResponseToRoles(Object.keys(RoleEnum) as RoleEnum[]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user