1
|
import {Injectable} from "@angular/core";
|
2
|
import {HttpClient} from "@angular/common/http";
|
3
|
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
|
4
|
import {Indicator, Section, Stakeholder, StakeholderInfo, Visibility} from "../entities/stakeholder";
|
5
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
6
|
import {map} from "rxjs/operators";
|
7
|
import {ActivatedRoute} from "@angular/router";
|
8
|
import {properties} from "../../../../environments/environment";
|
9
|
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
|
10
|
|
11
|
let maps: string[] = ['parameters', 'filters'];
|
12
|
|
13
|
export interface Reorder {
|
14
|
action: 'moved' | 'added' | 'removed',
|
15
|
target: string,
|
16
|
ids: string[];
|
17
|
}
|
18
|
|
19
|
@Injectable({
|
20
|
providedIn: "root"
|
21
|
})
|
22
|
export class StakeholderService {
|
23
|
|
24
|
private stakeholderSubject: BehaviorSubject<Stakeholder> = null;
|
25
|
private promise: Promise<any>;
|
26
|
private sub;
|
27
|
|
28
|
constructor(private http: HttpClient, private route: ActivatedRoute) {
|
29
|
this.stakeholderSubject = new BehaviorSubject<Stakeholder>(null);
|
30
|
}
|
31
|
|
32
|
ngOnDestroy() {
|
33
|
this.clearSubscriptions();
|
34
|
}
|
35
|
|
36
|
clearSubscriptions() {
|
37
|
if (this.sub instanceof Subscriber) {
|
38
|
this.sub.unsubscribe();
|
39
|
}
|
40
|
}
|
41
|
|
42
|
getStakeholder(alias: string): Observable<Stakeholder> {
|
43
|
if (!this.stakeholderSubject.value || this.stakeholderSubject.value.alias !== alias) {
|
44
|
this.promise = new Promise<any>((resolve, reject) => {
|
45
|
this.sub = this.http.get<Stakeholder>(properties.monitorServiceAPIURL + '/stakeholder/' + encodeURIComponent(alias), CustomOptions.registryOptions()).pipe(map(stakeholder => {
|
46
|
return this.formalize(stakeholder);
|
47
|
})).subscribe(stakeholder => {
|
48
|
this.stakeholderSubject.next(stakeholder);
|
49
|
resolve();
|
50
|
}, error => {
|
51
|
this.stakeholderSubject.next(null);
|
52
|
resolve();
|
53
|
});
|
54
|
});
|
55
|
}
|
56
|
return from(this.getStakeholderAsync());
|
57
|
}
|
58
|
|
59
|
async getStakeholderAsync() {
|
60
|
await this.promise;
|
61
|
this.clearSubscriptions();
|
62
|
return this.stakeholderSubject.getValue();
|
63
|
}
|
64
|
|
65
|
getAlias(url: string): Observable<string[]> {
|
66
|
return this.http.get<Stakeholder[]>(url + '/stakeholder/alias', CustomOptions.registryOptions()).pipe(map(stakeholders => {
|
67
|
return this.formalize(stakeholders);
|
68
|
}));
|
69
|
}
|
70
|
|
71
|
getStakeholders(url: string, type: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
|
72
|
return this.http.get<Stakeholder[]>(url + '/stakeholder' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
|
73
|
return this.formalize(stakeholders);
|
74
|
}));
|
75
|
}
|
76
|
|
77
|
getMyStakeholders(url: string, type: string = null): Observable<Stakeholder[]> {
|
78
|
return this.http.get<Stakeholder[]>(url + '/my-stakeholder' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
|
79
|
return this.formalize(stakeholders);
|
80
|
}));
|
81
|
}
|
82
|
|
83
|
getDefaultStakeholders(url: string, type: string = null): Observable<Stakeholder[]> {
|
84
|
return this.http.get<Stakeholder[]>(url + '/stakeholder/default' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(stakeholders => {
|
85
|
return this.formalize(stakeholders);
|
86
|
}));
|
87
|
}
|
88
|
|
89
|
buildStakeholder(url: string, stakeholder: Stakeholder): Observable<Stakeholder> {
|
90
|
return this.http.post<Stakeholder>(url + '/build-stakeholder', stakeholder, CustomOptions.registryOptions()).pipe(map(stakeholder => {
|
91
|
return this.formalize(stakeholder);
|
92
|
}));
|
93
|
}
|
94
|
|
95
|
changeVisibility(url: string, path: string[], visibility: Visibility): Observable<any> {
|
96
|
return this.http.post<Visibility>(url + '/' + path.join('/') + '/change-visibility' + '?visibility=' + visibility, null, CustomOptions.registryOptions());
|
97
|
}
|
98
|
|
99
|
saveElement(url: string, element: any, path: string[] = []): Observable<any> {
|
100
|
path = HelperFunctions.encodeArray(path);
|
101
|
return this.http.post<any>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
|
102
|
'/save', element, CustomOptions.registryOptions()).pipe(map(element => {
|
103
|
return this.formalize(element);
|
104
|
}));
|
105
|
}
|
106
|
|
107
|
saveSection(url: string, element: any, path: string[] = [], index: number = -1): Observable<Section> {
|
108
|
path = HelperFunctions.encodeArray(path);
|
109
|
return this.http.post<Section>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
|
110
|
'/save/' + index, element, CustomOptions.registryOptions()).pipe(map(element => {
|
111
|
return this.formalize(element);
|
112
|
}));
|
113
|
}
|
114
|
|
115
|
deleteElement(url: string, path: string[], childrenAction: string = null): Observable<any> {
|
116
|
path = HelperFunctions.encodeArray(path);
|
117
|
let params: string = "";
|
118
|
if (childrenAction) {
|
119
|
params = "?children=" + childrenAction;
|
120
|
}
|
121
|
return this.http.delete<any>(url + '/' + path.join('/') + '/delete' + params, CustomOptions.registryOptions());
|
122
|
}
|
123
|
|
124
|
reorderIndicators(url: string, path: string[], reorder: Reorder, type: string = 'chart'): Observable<Indicator[]> {
|
125
|
path = HelperFunctions.encodeArray(path);
|
126
|
return this.http.post<Indicator[]>(url + '/' + path.join('/') + '/' + type + '/reorder', reorder, CustomOptions.registryOptions()).pipe(map(indicators => {
|
127
|
return this.formalize(indicators);
|
128
|
}));
|
129
|
}
|
130
|
|
131
|
getStakeholderAsObservable(): Observable<Stakeholder> {
|
132
|
return this.stakeholderSubject.asObservable();
|
133
|
}
|
134
|
|
135
|
setStakeholder(stakeholder: Stakeholder) {
|
136
|
this.stakeholderSubject.next(stakeholder);
|
137
|
}
|
138
|
|
139
|
private formalize(element: any) {
|
140
|
return HelperFunctions.copy(element);
|
141
|
}
|
142
|
}
|