import {Injectable} from "@angular/core"; import {HttpClient} from "@angular/common/http"; import {BehaviorSubject, from, Observable, Subscriber} from "rxjs"; import {Indicator, Section, Stakeholder, StakeholderInfo, Visibility} from "../entities/stakeholder"; import {HelperFunctions} from "../../utils/HelperFunctions.class"; import {map} from "rxjs/operators"; import {ActivatedRoute} from "@angular/router"; import {properties} from "../../../../environments/environment"; import {CustomOptions} from "../../services/servicesUtils/customOptions.class"; let maps: string[] = ['parameters', 'filters']; export interface Reorder { action: 'moved' | 'added' | 'removed', target: string, ids: string[]; } @Injectable({ providedIn: "root" }) export class StakeholderService { private stakeholderSubject: BehaviorSubject = null; private promise: Promise; private sub; constructor(private http: HttpClient, private route: ActivatedRoute) { this.stakeholderSubject = new BehaviorSubject(null); } ngOnDestroy() { this.clearSubscriptions(); } clearSubscriptions(){ if (this.sub instanceof Subscriber) { this.sub.unsubscribe(); } } getStakeholder(alias:string): Observable { if(!this.stakeholderSubject.value || this.stakeholderSubject.value.alias !== alias) { this.promise = new Promise((resolve, reject) => { this.sub = this.http.get(properties.monitorServiceAPIURL + '/stakeholder/' + encodeURIComponent(alias), CustomOptions.registryOptions()).pipe(map(stakeholder => { return this.formalize(stakeholder); })).subscribe(stakeholder => { this.stakeholderSubject.next(stakeholder); resolve(); }, error => { this.stakeholderSubject.next(null); reject(); }); }); } return from(this.getStakeholderAsync()); } async getStakeholderAsync() { await this.promise; this.clearSubscriptions(); return this.stakeholderSubject.getValue(); } getAlias(url: string): Observable { return this.http.get(url + '/stakeholder/alias', CustomOptions.registryOptions()).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } getStakeholders(url: string, type: string = null): Observable<(Stakeholder & StakeholderInfo)[]> { return this.http.get(url + '/stakeholder' + ((type)?('?type=' + type):''), CustomOptions.registryOptions()).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } getMyStakeholders(url: string, type: string = null): Observable { return this.http.get(url + '/my-stakeholder' + ((type)?('?type=' + type):''), CustomOptions.registryOptions()).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } getDefaultStakeholders(url: string, type: string = null): Observable { return this.http.get(url + '/stakeholder/default' + ((type)?('?type=' + type):''), CustomOptions.registryOptions()).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } buildStakeholder(url: string, stakeholder: Stakeholder): Observable { return this.http.post(url + '/build-stakeholder', stakeholder, CustomOptions.registryOptions()).pipe(map(stakeholder => { return this.formalize(stakeholder); })); } changeVisibility(url: string, path: string[], visibility:Visibility): Observable { return this.http.post(url + '/' + path.join('/') + '/change-visibility'+'?visibility=' + visibility, null, CustomOptions.registryOptions()); } saveElement(url: string, element: any, path: string[] = []): Observable { path = HelperFunctions.encodeArray(path); return this.http.post(url + ((path.length > 0)?'/':'') + path.join('/') + '/save', element, CustomOptions.registryOptions()).pipe(map(element => { return this.formalize(element); })); } saveSection(url: string, element: any, path: string[] = [], index: number = -1): Observable
{ path = HelperFunctions.encodeArray(path); return this.http.post
(url + ((path.length > 0)?'/':'') + path.join('/') + '/save/' + index, element, CustomOptions.registryOptions()).pipe(map(element => { return this.formalize(element); })); } deleteElement(url: string, path: string[], childrenAction: string = null): Observable { path = HelperFunctions.encodeArray(path); let params: string = ""; if(childrenAction) { params = "?children="+childrenAction; } return this.http.delete(url + '/' + path.join('/') + '/delete'+params, CustomOptions.registryOptions()); } reorderIndicators(url: string, path: string[], reorder: Reorder, type: string = 'chart'): Observable { path = HelperFunctions.encodeArray(path); return this.http.post(url + '/' + path.join('/') + '/' + type + '/reorder', reorder, CustomOptions.registryOptions()).pipe(map(indicators => { return this.formalize(indicators); })); } getStakeholderAsObservable(): Observable { return this.stakeholderSubject.asObservable(); } get stakeholder(): Stakeholder { return this.stakeholderSubject.getValue(); } setStakeholder(stakeholder: Stakeholder) { this.stakeholderSubject.next(stakeholder); } private formalize(element: any) { return HelperFunctions.copy(element); } }