From 677372a332bd7d8f923203ea270f5c6f7d875426 Mon Sep 17 00:00:00 2001 From: argirok Date: Tue, 19 Dec 2023 11:53:34 +0200 Subject: [PATCH] [angular-16-irish-monitor | DONE | ADDED] User profile service --- services/userProfile.service.ts | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 services/userProfile.service.ts diff --git a/services/userProfile.service.ts b/services/userProfile.service.ts new file mode 100644 index 00000000..56c13c3e --- /dev/null +++ b/services/userProfile.service.ts @@ -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 = 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(),); + } + +}