import {Injectable} from "@angular/core"; import {HttpClient} from "@angular/common/http"; import {Observable} from "rxjs"; import {EnvProperties} from "../utils/properties/env-properties"; import {CustomOptions} from "./servicesUtils/customOptions.class"; import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject"; import {properties} from "../../../environments/environment"; import {map} from "rxjs/operators"; export class UserProfile{ id:string; aaiId:string; consent:boolean = false; constructor(consent:boolean) { this.consent = consent; } } @Injectable({ providedIn: "root" }) export class UserProfileService { private subscription; private readonly getUserProfileSubject: AdvancedAsyncSubject = new AdvancedAsyncSubject(); constructor(private http: HttpClient) { this.updateUserProfile(); } clearSubscriptions() { if (this.subscription) { this.subscription.unsubscribe(); } } public get user(): UserProfile { return this.getUserProfileSubject.getValue(); } public getUserProfile(): Observable { return this.getUserProfileSubject.asObservable(); } public setUserProfile(userProfile:UserProfile) { this.getUserProfileSubject.next(userProfile); } public updateUserProfile(resolve: Function = null) { this.subscription = this.http.get(properties.monitorServiceAPIURL + 'user', CustomOptions.registryOptions()).pipe(map(userProfile => { return userProfile; })).subscribe(user => { this.getUserProfileSubject.next(user); if (resolve) { resolve(); } }, error => { this.getUserProfileSubject.next(null); if (resolve) { resolve(); } }); } saveConsentInUserProfile(properties:EnvProperties): Observable { return this.http.post(properties.monitorServiceAPIURL + 'user/save', new UserProfile(true), CustomOptions.registryOptions(),); } undoConsentInUserProfile(properties:EnvProperties): Observable { return this.http.post(properties.monitorServiceAPIURL + 'user/save', new UserProfile(false), CustomOptions.registryOptions(),); } }