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 {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
|
|
|
|
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 {
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
private stakeholderSubject: BehaviorSubject<Stakeholder> = null;
|
2022-09-23 11:24:15 +02:00
|
|
|
private promise: Promise<void>;
|
2020-11-13 15:56:17 +01:00
|
|
|
private sub;
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2023-01-20 16:31:36 +01:00
|
|
|
constructor(private http: HttpClient) {
|
2020-06-03 15:43:14 +02:00
|
|
|
this.stakeholderSubject = new BehaviorSubject<Stakeholder>(null);
|
|
|
|
}
|
2023-04-11 00:28:53 +02: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
|
|
|
}
|
2023-04-11 00:28:53 +02: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();
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2023-01-20 16:31:36 +01:00
|
|
|
getStakeholder(alias: string, shouldUpdate: boolean = false): Observable<Stakeholder> {
|
|
|
|
if (!this.stakeholderSubject.value || this.stakeholderSubject.value.alias !== alias || shouldUpdate) {
|
2022-09-23 11:24:15 +02:00
|
|
|
this.promise = new Promise<void>((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());
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-10-30 12:33:00 +01:00
|
|
|
async getStakeholderAsync() {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (this.promise) {
|
2023-03-21 13:02:59 +01:00
|
|
|
await this.promise;
|
|
|
|
this.promise = null;
|
|
|
|
}
|
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
|
|
|
}
|
2023-04-11 00:28:53 +02: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);
|
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2021-02-04 14:36:29 +01:00
|
|
|
getStakeholders(url: string, type: string = null, defaultId: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
|
2023-04-11 00:28:53 +02:00
|
|
|
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
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2022-02-16 17:18:12 +01:00
|
|
|
getMyStakeholders(url: string, type: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
|
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
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02: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
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
buildStakeholder(url: string, stakeholder: Stakeholder): Observable<Stakeholder> {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (stakeholder.alias && stakeholder.alias.startsWith('/')) {
|
|
|
|
stakeholder.alias = stakeholder.alias.slice(1);
|
|
|
|
}
|
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
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2022-07-19 14:18:56 +02:00
|
|
|
changeVisibility(url: string, path: string[], visibility: Visibility, propagate: boolean = false): Observable<any> {
|
2023-04-11 00:28:53 +02:00
|
|
|
return this.http.post<Visibility>(url + '/' + path.join('/') + '/change-visibility' + '?visibility=' + visibility + (propagate ? '&propagate=true' : ''), null, CustomOptions.registryOptions());
|
2020-06-03 15:43:14 +02:00
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
saveElement(url: string, element: any, path: string[] = []): Observable<any> {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (element.alias && element.alias.startsWith('/')) {
|
|
|
|
element.alias = element.alias.slice(1);
|
|
|
|
}
|
2020-06-03 15:43:14 +02:00
|
|
|
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 => {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (path.length === 0) {
|
|
|
|
return this.formalize(this.checkIsUpload(element));
|
|
|
|
} else {
|
|
|
|
return this.formalize(element);
|
|
|
|
}
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +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 => {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (path.length === 0) {
|
2021-07-16 14:02:33 +02:00
|
|
|
return this.formalize(this.checkIsUpload(element));
|
|
|
|
} else {
|
|
|
|
return this.formalize(element);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
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);
|
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02: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
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2022-07-14 13:37:07 +02:00
|
|
|
reorderElements(url: string, path: string[], ids: string[]): Observable<any> {
|
|
|
|
path = HelperFunctions.encodeArray(path);
|
|
|
|
return this.http.post<any>(url + '/' + path.join('/') + '/reorder', ids, CustomOptions.registryOptions());
|
|
|
|
}
|
2023-04-11 00:28:53 +02: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);
|
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
getStakeholderAsObservable(): Observable<Stakeholder> {
|
|
|
|
return this.stakeholderSubject.asObservable();
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
setStakeholder(stakeholder: Stakeholder) {
|
|
|
|
this.stakeholderSubject.next(stakeholder);
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2021-02-01 11:14:08 +01:00
|
|
|
private checkIsUpload(response: Stakeholder | Stakeholder[]): any | any[] {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (Array.isArray(response)) {
|
2021-02-01 11:14:08 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2020-06-03 15:43:14 +02:00
|
|
|
private formalize(element: any) {
|
|
|
|
return HelperFunctions.copy(element);
|
|
|
|
}
|
|
|
|
}
|