[angular-16-irish-monitor | DONE | ADDED] User profile service

This commit is contained in:
argirok 2023-12-19 11:53:34 +02:00
parent 2a64a0c951
commit 677372a332
1 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,70 @@
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(),);
}
}