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