1
|
import {Injectable} from "@angular/core";
|
2
|
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
3
|
import {Observable} from "rxjs";
|
4
|
import {Indicator, Stakeholder} from "../utils/entities/stakeholder";
|
5
|
import {map} from "rxjs/operators";
|
6
|
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
|
7
|
|
8
|
@Injectable()
|
9
|
export class StakeholderService {
|
10
|
|
11
|
constructor(private http: HttpClient) {
|
12
|
}
|
13
|
|
14
|
getStakeholder(alias: string): Observable<Stakeholder> {
|
15
|
return this.http.get<any>('./assets/stakeholders.json').pipe(map(json => {
|
16
|
let stakeholder = json.stakeholders.filter(stakeholder => stakeholder.alias === alias)[0];
|
17
|
if (stakeholder) {
|
18
|
stakeholder.topics = stakeholder.topics.filter(topic => topic.isActive && topic.isPublic);
|
19
|
return stakeholder;
|
20
|
} else {
|
21
|
throw new HttpErrorResponse({
|
22
|
error: {
|
23
|
message: 'No Stakeholder has been found',
|
24
|
status: 404
|
25
|
}
|
26
|
})
|
27
|
}
|
28
|
}));
|
29
|
}
|
30
|
|
31
|
getIndicator(id: string): Observable<Indicator> {
|
32
|
return this.http.get<any>('./assets/stakeholders.json').pipe(map(json => {
|
33
|
return json.indicators.filter(indicator => indicator.id === id)[0];
|
34
|
}))
|
35
|
}
|
36
|
|
37
|
getIndicators(ids: string[]): Observable<Indicator[]> {
|
38
|
return this.http.get<any>('./assets/stakeholders.json').pipe(map(json => {
|
39
|
return json.indicators.filter(indicator => ids.indexOf(indicator.id) !== -1);
|
40
|
}))
|
41
|
}
|
42
|
|
43
|
getNumber(properties: EnvProperties, url: string, jsonPath: string[], stakeholder: Stakeholder): Observable<any> {
|
44
|
const statsUrl = url.replace('{index_shortName}', stakeholder.index_shortName).
|
45
|
replace('{index_id}', stakeholder.index_id).
|
46
|
replace('{index_name}', stakeholder.index_name);
|
47
|
return this.http.get<any>(properties.statisticsAPIURL + statsUrl).pipe(map(response => {
|
48
|
jsonPath.forEach(field => {
|
49
|
response = response[field];
|
50
|
});
|
51
|
return response;
|
52
|
}));
|
53
|
}
|
54
|
}
|