2020-06-03 15:43:14 +02:00
|
|
|
import {Injectable} from "@angular/core";
|
|
|
|
import {HttpClient} from "@angular/common/http";
|
|
|
|
import {BehaviorSubject, Observable} from "rxjs";
|
2020-06-05 14:56:28 +02:00
|
|
|
import {Indicator, Section, Stakeholder, StakeholderInfo} from "../entities/stakeholder";
|
2020-06-03 15:43:14 +02:00
|
|
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
|
|
|
import {map} from "rxjs/operators";
|
|
|
|
|
|
|
|
let maps: string[] = ['parameters', 'filters'];
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: "root"
|
|
|
|
})
|
|
|
|
export class StakeholderService {
|
|
|
|
|
|
|
|
private stakeholderSubject: BehaviorSubject<Stakeholder> = null;
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
this.stakeholderSubject = new BehaviorSubject<Stakeholder>(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
getStakeholder(url: string, alias:string): Observable<Stakeholder> {
|
|
|
|
/*return new BehaviorSubject<Stakeholder>(
|
|
|
|
StakeholderCreator.createFunderFromDefaultProfile("ec","funder","ec__________::EC",
|
|
|
|
"European Commission","EC",
|
|
|
|
false,"ec",true,true)).asObservable();*/
|
|
|
|
return this.http.get<Stakeholder>(url + '/stakeholder/' + encodeURIComponent(alias)).pipe(map(stakeholder => {
|
|
|
|
return this.formalize(stakeholder);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllStakeholders(url: string, type: string = null): Observable<Stakeholder[]> {
|
|
|
|
return this.http.get<Stakeholder[]>(url + '/stakeholder/all' + ((type)?('?type=' + type):'')).pipe(map(stakeholders => {
|
|
|
|
return this.formalize(stakeholders);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:56:28 +02:00
|
|
|
getStakeholders(url: string, type: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
|
2020-06-03 15:43:14 +02:00
|
|
|
return this.http.get<Stakeholder[]>(url + '/stakeholder' + ((type)?('?type=' + type):'')).pipe(map(stakeholders => {
|
|
|
|
return this.formalize(stakeholders);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultStakeholders(url: string, type: string = null): Observable<Stakeholder[]> {
|
|
|
|
return this.http.get<Stakeholder[]>(url + '/stakeholder/default' + ((type)?('?type=' + type):'')).pipe(map(stakeholders => {
|
|
|
|
return this.formalize(stakeholders);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
buildStakeholder(url: string, stakeholder: Stakeholder): Observable<Stakeholder> {
|
|
|
|
return this.http.post<Stakeholder>(url + '/build-stakeholder', stakeholder).pipe(map(stakeholder => {
|
|
|
|
return this.formalize(stakeholder);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
saveStakeholder(url: string, stakeholder: Stakeholder): Observable<Stakeholder> {
|
|
|
|
return this.http.post<Stakeholder>(url + '/stakeholder/save', stakeholder).pipe(map(stakeholder => {
|
|
|
|
return this.formalize(stakeholder);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleStatus(url: string, path: string[]): Observable<boolean> {
|
|
|
|
return this.http.post<boolean>(url + '/' + path.join('/') + '/toggle-status', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleAccess(url: string, path: string[]): Observable<boolean> {
|
|
|
|
return this.http.post<boolean>(url + '/' + path.join('/') + '/toggle-access', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveElement(url: string, element: any, path: string[] = []): Observable<any> {
|
|
|
|
path = HelperFunctions.encodeArray(path);
|
|
|
|
return this.http.post<any>(url + ((path.length > 0)?'/':'') + path.join('/') +
|
|
|
|
'/save', element).pipe(map(element => {
|
|
|
|
return this.formalize(element);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
saveSection(url: string, element: any, path: string[] = [], index: number = -1): Observable<Section> {
|
|
|
|
path = HelperFunctions.encodeArray(path);
|
|
|
|
return this.http.post<Section>(url + ((path.length > 0)?'/':'') + path.join('/') +
|
|
|
|
'/save/' + index, element).pipe(map(element => {
|
|
|
|
return this.formalize(element);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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 = "";
|
|
|
|
if(childrenAction) {
|
|
|
|
params = "?children="+childrenAction;
|
|
|
|
}
|
|
|
|
return this.http.delete<any>(url + '/' + path.join('/') + '/delete'+params);
|
2020-06-03 15:43:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reorderIndicators(url: string, path: string[], indicatorIds: string[], type: string = 'chart'): Observable<Indicator[]> {
|
|
|
|
path = HelperFunctions.encodeArray(path);
|
|
|
|
return this.http.post<Indicator[]>(url + '/' + path.join('/') + '/' + type + '/reorder', indicatorIds).pipe(map(indicators => {
|
|
|
|
return this.formalize(indicators);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
getStakeholderAsObservable(): Observable<Stakeholder> {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|