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";
|
2021-02-01 11:14:08 +01:00
|
|
|
import {StringUtils} from "../../utils/string-utils.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-09-17 19:13:35 +02:00
|
|
|
/* let source = new EventSource(properties.monitorServiceAPIURL + "/stakeholder/events", {withCredentials: true});
|
|
|
|
source.addEventListener('message', message => {
|
|
|
|
console.log(message.data);
|
|
|
|
});*/
|
2020-06-03 15:43:14 +02:00
|
|
|
}
|
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 => {
|
2021-02-01 11:14:08 +01:00
|
|
|
return this.formalize(this.checkIsUpload(stakeholder));
|
2020-10-30 12:33:00 +01:00
|
|
|
})).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);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-02-04 14:36:29 +01:00
|
|
|
getStakeholders(url: string, type: string = null, defaultId: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
|
|
|
|
return this.http.get<Stakeholder[]>(url + '/stakeholder' + ((type) ? ('?type=' + type) : '') + ((!type && defaultId)? ('?defaultId=' + defaultId):''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
|
2021-02-01 11:14:08 +01:00
|
|
|
return this.formalize(this.checkIsUpload(stakeholders));
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
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 => {
|
2021-02-01 11:14:08 +01:00
|
|
|
return this.formalize(this.checkIsUpload(stakeholders));
|
2020-11-02 15:45:20 +01:00
|
|
|
}));
|
|
|
|
}
|
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 => {
|
2021-02-01 11:14:08 +01:00
|
|
|
return this.formalize(this.checkIsUpload(stakeholders));
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
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 => {
|
2021-02-01 11:14:08 +01:00
|
|
|
return this.formalize(this.checkIsUpload(stakeholder));
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
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-02-01 11:14:08 +01:00
|
|
|
if(path.length === 0) {
|
|
|
|
return this.formalize(this.checkIsUpload(element));
|
|
|
|
} else {
|
2021-02-04 14:36:29 +01:00
|
|
|
return this.formalize(element);
|
2021-02-01 11:14:08 +01:00
|
|
|
}
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
2021-07-16 14:02:33 +02:00
|
|
|
saveBulkElements(url: string, indicators, path: string[] = []): Observable<any> {
|
|
|
|
path = HelperFunctions.encodeArray(path);
|
|
|
|
return this.http.post<any>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
|
|
|
|
'/save-bulk', indicators, CustomOptions.registryOptions()).pipe(map(element => {
|
|
|
|
if(path.length === 0) {
|
|
|
|
return this.formalize(this.checkIsUpload(element));
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2021-02-01 11:14:08 +01:00
|
|
|
private checkIsUpload(response: Stakeholder | Stakeholder[]): any | any[] {
|
|
|
|
if(Array.isArray(response)) {
|
|
|
|
response.forEach(value => {
|
2021-02-04 14:36:29 +01:00
|
|
|
value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
|
2021-02-01 11:14:08 +01:00
|
|
|
});
|
|
|
|
} else {
|
2021-02-04 14:36:29 +01:00
|
|
|
response.isUpload = response.logoUrl && !StringUtils.isValidUrl(response.logoUrl);
|
2021-02-01 11:14:08 +01:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
private formalize(element: any) {
|
|
|
|
return HelperFunctions.copy(element);
|
|
|
|
}
|
|
|
|
}
|