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";
|
2024-05-21 10:05:16 +02:00
|
|
|
import {
|
|
|
|
Indicator,
|
|
|
|
ManageStakeholder,
|
|
|
|
Section,
|
|
|
|
Stakeholder,
|
|
|
|
StakeholderInfo,
|
|
|
|
SubCategory,
|
|
|
|
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";
|
2020-06-03 15:43:14 +02:00
|
|
|
|
2024-04-05 09:57:39 +02:00
|
|
|
export interface SectionInfo {
|
|
|
|
id: string;
|
|
|
|
indicators: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MoveIndicator {
|
|
|
|
target: string;
|
|
|
|
from: SectionInfo;
|
|
|
|
to: SectionInfo;
|
2020-12-09 14:22:52 +01:00
|
|
|
}
|
|
|
|
|
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 => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(Stakeholder.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());
|
|
|
|
}
|
2024-02-20 09:58:47 +01:00
|
|
|
|
2024-05-30 09:41:26 +02:00
|
|
|
getChildStakeholder(parent: string, type: string, child: string, shouldUpdate: boolean = false): Observable<Stakeholder> {
|
|
|
|
// TODO: if statement
|
|
|
|
this.promise = new Promise<void>((resolve, reject) => {
|
|
|
|
this.sub = this.http.get<Stakeholder>(properties.monitorServiceAPIURL + '/stakeholder/' + encodeURIComponent(parent) + '/' + type + '/' + encodeURIComponent(child), CustomOptions.registryOptions()).pipe(map(stakeholder => {
|
|
|
|
return HelperFunctions.copy(Stakeholder.checkIsUpload(stakeholder));
|
|
|
|
})).subscribe(stakeholder => {
|
|
|
|
this.stakeholderSubject.next(stakeholder);
|
|
|
|
resolve();
|
|
|
|
}, error => {
|
|
|
|
this.stakeholderSubject.next(null);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return from(this.getStakeholderAsync());
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:58:47 +01:00
|
|
|
getResearcherStakeholder(orcid, name, results, shouldUpdate: boolean = false): Observable<Stakeholder> {
|
2024-01-11 17:28:02 +01:00
|
|
|
if (!this.stakeholderSubject.value || this.stakeholderSubject.value.alias !== orcid || shouldUpdate) {
|
|
|
|
this.promise = new Promise<void>((resolve, reject) => {
|
|
|
|
this.sub = this.http.get<Stakeholder>(properties.monitorServiceAPIURL + '/stakeholder/' + encodeURIComponent("researcher"), CustomOptions.registryOptions()).pipe(map(stakeholder => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(Stakeholder.checkIsUpload(stakeholder));
|
2024-01-11 17:28:02 +01:00
|
|
|
})).subscribe(stakeholder => {
|
|
|
|
stakeholder.index_id = orcid;
|
|
|
|
stakeholder.index_name = name;
|
|
|
|
stakeholder.name = name;
|
|
|
|
stakeholder.alias = orcid;
|
2024-02-20 09:58:47 +01:00
|
|
|
if (results < 7 && stakeholder.topics[0]?.categories[0]?.subCategories[0]) {
|
|
|
|
stakeholder.topics[0].categories[0].subCategories[0].charts = []; // keep only numbers - charts wont show much anyway
|
2024-01-11 17:28:02 +01:00
|
|
|
}
|
|
|
|
this.stakeholderSubject.next(stakeholder);
|
|
|
|
resolve();
|
|
|
|
}, error => {
|
2024-02-20 09:58:47 +01:00
|
|
|
let stakeholder = new Stakeholder(null, "researcher", orcid, name, name, orcid, "PUBLIC", null, null, "");
|
2024-01-11 17:28:02 +01:00
|
|
|
this.stakeholderSubject.next(stakeholder);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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 => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(stakeholders);
|
2020-12-08 17:39:10 +01:00
|
|
|
}));
|
|
|
|
}
|
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 => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(Stakeholder.checkIsUpload(stakeholders));
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-05-21 10:05:16 +02:00
|
|
|
getMyStakeholders(url: string, type: string = null): Observable<ManageStakeholder> {
|
|
|
|
return this.http.get<ManageStakeholder>(url + '/my-stakeholder' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(manageStakeholder => {
|
|
|
|
return HelperFunctions.copy({
|
|
|
|
templates: Stakeholder.checkIsUpload(manageStakeholder.templates),
|
|
|
|
standalone: Stakeholder.checkIsUpload(manageStakeholder.standalone),
|
|
|
|
dependent: Stakeholder.checkIsUpload(manageStakeholder.dependent),
|
|
|
|
umbrella: Stakeholder.checkIsUpload(manageStakeholder.umbrella),
|
|
|
|
});
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-05-21 10:05:16 +02:00
|
|
|
buildStakeholder(url: string, stakeholder: Stakeholder, copyId: string, umbrella: boolean = false): Observable<Stakeholder> {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (stakeholder.alias && stakeholder.alias.startsWith('/')) {
|
|
|
|
stakeholder.alias = stakeholder.alias.slice(1);
|
|
|
|
}
|
2024-05-21 10:05:16 +02:00
|
|
|
let buildStakeholder = {
|
2024-04-12 10:14:26 +02:00
|
|
|
stakeholder: stakeholder,
|
2024-05-21 10:05:16 +02:00
|
|
|
copyId: copyId,
|
|
|
|
umbrella: umbrella
|
2024-04-12 10:14:26 +02:00
|
|
|
}
|
2024-05-21 10:05:16 +02:00
|
|
|
return this.http.post<Stakeholder>(url + '/build-stakeholder', buildStakeholder, CustomOptions.registryOptions()).pipe(map(stakeholder => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(Stakeholder.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
|
|
|
|
2024-04-12 10:14:26 +02:00
|
|
|
saveElement(url: string, element: any, path: string[] = [], isFull: boolean = false): Observable<any> {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (element.alias && element.alias.startsWith('/')) {
|
|
|
|
element.alias = element.alias.slice(1);
|
|
|
|
}
|
2021-01-14 15:32:01 +01:00
|
|
|
return this.http.post<any>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
|
2024-04-12 10:14:26 +02:00
|
|
|
'/save' + (isFull?'/full':''), element, CustomOptions.registryOptions()).pipe(map(element => {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (path.length === 0) {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(Stakeholder.checkIsUpload(element));
|
2023-04-11 00:28:53 +02:00
|
|
|
} else {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(element);
|
2023-04-11 00:28:53 +02:00
|
|
|
}
|
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> {
|
|
|
|
return this.http.post<any>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
|
2024-02-20 09:58:47 +01:00
|
|
|
'/save-bulk', indicators, CustomOptions.registryOptions()).pipe(map(element => {
|
2023-04-11 00:28:53 +02:00
|
|
|
if (path.length === 0) {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(Stakeholder.checkIsUpload(element));
|
2021-07-16 14:02:33 +02:00
|
|
|
} else {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(element);
|
2021-07-16 14:02:33 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
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> {
|
2021-01-14 15:32:01 +01:00
|
|
|
return this.http.post<Section>(url + ((path.length > 0) ? '/' : '') + path.join('/') +
|
2024-02-20 09:58:47 +01:00
|
|
|
'/save/' + index, element, CustomOptions.registryOptions()).pipe(map(element => {
|
|
|
|
return HelperFunctions.copy(element);
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
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> {
|
|
|
|
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> {
|
|
|
|
return this.http.post<any>(url + '/' + path.join('/') + '/reorder', ids, CustomOptions.registryOptions());
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-04-05 09:57:39 +02:00
|
|
|
reorderIndicators(url: string, path: string[], indicators: string[]): Observable<Indicator[]> {
|
|
|
|
return this.http.post<Indicator[]>(url + '/' + path.join('/') + '/reorder', indicators, CustomOptions.registryOptions()).pipe(map(indicators => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(indicators);
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-04-05 09:57:39 +02:00
|
|
|
moveIndicator(url: string, path: string[], moveIndicator: MoveIndicator): Observable<SubCategory> {
|
|
|
|
return this.http.post<SubCategory>(url + '/' + path.join('/') + '/moveIndicator', moveIndicator, CustomOptions.registryOptions()).pipe(map(subCategory => {
|
|
|
|
return HelperFunctions.copy(subCategory);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|