openaire-library/services/userProfile.service.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

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<UserProfile> = new AdvancedAsyncSubject<UserProfile>();
constructor(private http: HttpClient) {
this.updateUserProfile();
}
clearSubscriptions() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
public get user(): UserProfile {
return this.getUserProfileSubject.getValue();
}
public getUserProfile(): Observable<UserProfile> {
return this.getUserProfileSubject.asObservable();
}
public setUserProfile(userProfile:UserProfile) {
this.getUserProfileSubject.next(userProfile);
}
public updateUserProfile(resolve: Function = null) {
this.subscription = this.http.get<UserProfile>(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<UserProfile> {
return this.http.post<UserProfile>(properties.monitorServiceAPIURL + 'user/save', new UserProfile(true), CustomOptions.registryOptions(),);
}
undoConsentInUserProfile(properties:EnvProperties): Observable<UserProfile> {
return this.http.post<UserProfile>(properties.monitorServiceAPIURL + 'user/save', new UserProfile(false), CustomOptions.registryOptions(),);
}
}