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 {
|
2024-06-11 16:32:16 +02:00
|
|
|
Indicator, ManageStakeholders,
|
2024-05-21 10:05:16 +02:00
|
|
|
Section,
|
|
|
|
Stakeholder,
|
2024-06-11 16:32:16 +02:00
|
|
|
StakeholderInfo, StakeholderType,
|
|
|
|
SubCategory, Umbrella,
|
2024-05-21 10:05:16 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-11 16:32:16 +02:00
|
|
|
export interface UpdateUmbrella {
|
|
|
|
type: StakeholderType;
|
|
|
|
action: "ADD" | "REMOVE",
|
|
|
|
child: 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) => {
|
2024-09-05 11:59:25 +02: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 => {
|
2024-06-11 16:32:16 +02:00
|
|
|
this.stakeholderSubject.next(stakeholder);
|
2020-10-30 12:33:00 +01:00
|
|
|
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> {
|
2024-05-31 06:40:24 +02:00
|
|
|
if (!this.stakeholderSubject.value || this.stakeholderSubject.value.alias !== child || shouldUpdate) {
|
2024-05-30 09:41:26 +02:00
|
|
|
this.promise = new Promise<void>((resolve, reject) => {
|
2024-09-05 11:59:25 +02:00
|
|
|
this.sub = this.http.get<Stakeholder>(properties.monitorServiceAPIURL + 'stakeholder/' + encodeURIComponent(parent) + '/' + type + '/' + encodeURIComponent(child), CustomOptions.registryOptions()).pipe(map(stakeholder => {
|
2024-05-30 09:41:26 +02:00
|
|
|
return HelperFunctions.copy(Stakeholder.checkIsUpload(stakeholder));
|
|
|
|
})).subscribe(stakeholder => {
|
|
|
|
this.stakeholderSubject.next(stakeholder);
|
|
|
|
resolve();
|
|
|
|
}, error => {
|
|
|
|
this.stakeholderSubject.next(null);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2024-05-31 06:40:24 +02:00
|
|
|
}
|
2024-05-30 09:41:26 +02:00
|
|
|
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) => {
|
2024-09-05 11:59:25 +02:00
|
|
|
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
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
getAlias(): Observable<string[]> {
|
|
|
|
return this.http.get<Stakeholder[]>(properties.monitorServiceAPIURL + '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
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
getStakeholders(type: string = null, defaultId: string = null): Observable<(Stakeholder & StakeholderInfo)[]> {
|
|
|
|
return this.http.get<Stakeholder[]>(properties.monitorServiceAPIURL + 'stakeholder' + ((type) ? ('?type=' + type) : (defaultId?'?defaultId=' + defaultId:'')) + ((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-09-12 09:49:03 +02:00
|
|
|
getMyStakeholders(type: string = null): Observable<ManageStakeholders> {
|
|
|
|
return this.http.get<ManageStakeholders>(properties.monitorServiceAPIURL + 'my-stakeholder' + ((type) ? ('?type=' + type) : ''), CustomOptions.registryOptions()).pipe(map(manageStakeholder => {
|
2024-05-21 10:05:16 +02:00
|
|
|
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-09-12 09:49:03 +02:00
|
|
|
buildStakeholder(stakeholder: Stakeholder, copyId: string, standalone: boolean = true, 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,
|
2024-06-11 16:32:16 +02:00
|
|
|
umbrella: umbrella,
|
|
|
|
standalone: standalone
|
2024-04-12 10:14:26 +02:00
|
|
|
}
|
2024-09-12 09:49:03 +02:00
|
|
|
return this.http.post<Stakeholder>(properties.monitorServiceAPIURL + '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
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
changeVisibility(path: string[], visibility: Visibility, propagate: boolean = false): Observable<any> {
|
|
|
|
path.push('change-visibility');
|
|
|
|
return this.http.post<Visibility>(properties.monitorServiceAPIURL + path.join('/') + '?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-09-12 09:49:03 +02:00
|
|
|
saveElement(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);
|
|
|
|
}
|
2024-09-12 09:49:03 +02:00
|
|
|
path.push('save');
|
|
|
|
if(isFull) {
|
|
|
|
path.push('full');
|
|
|
|
}
|
|
|
|
return this.http.post<any>(properties.monitorServiceAPIURL + path.join('/'),
|
|
|
|
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
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
saveBulkElements(indicators, path: string[] = []): Observable<any> {
|
|
|
|
path.push('save-bulk');
|
|
|
|
return this.http.post<any>(properties.monitorServiceAPIURL + path.join('/'),
|
|
|
|
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
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
saveSection(element: any, path: string[] = [], index: number = -1): Observable<Section> {
|
|
|
|
path.push('save/');
|
|
|
|
return this.http.post<Section>(properties.monitorServiceAPIURL + path.join('/') + index,
|
|
|
|
element, CustomOptions.registryOptions()).pipe(map(element => {
|
2024-02-20 09:58:47 +01:00
|
|
|
return HelperFunctions.copy(element);
|
2020-06-03 15:43:14 +02:00
|
|
|
}));
|
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
deleteElement(path: string[], childrenAction: string = null): Observable<any> {
|
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
|
|
|
}
|
2024-09-12 09:49:03 +02:00
|
|
|
path.push('delete');
|
|
|
|
return this.http.delete<any>(properties.monitorServiceAPIURL + path.join('/') + params, CustomOptions.registryOptions());
|
2020-06-03 15:43:14 +02:00
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
reorderElements(path: string[], ids: string[]): Observable<any> {
|
|
|
|
path.push('reorder');
|
|
|
|
return this.http.post<any>(properties.monitorServiceAPIURL + path.join('/'), ids, CustomOptions.registryOptions());
|
2022-07-14 13:37:07 +02:00
|
|
|
}
|
2023-04-11 00:28:53 +02:00
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
reorderIndicators(path: string[], indicators: string[]): Observable<Indicator[]> {
|
|
|
|
path.push('reorder');
|
|
|
|
return this.http.post<Indicator[]>(properties.monitorServiceAPIURL + path.join('/'), 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-09-12 09:49:03 +02:00
|
|
|
moveIndicator(path: string[], moveIndicator: MoveIndicator): Observable<SubCategory> {
|
|
|
|
path.push('moveIndicator');
|
|
|
|
return this.http.post<SubCategory>(properties.monitorServiceAPIURL + path.join('/'), moveIndicator, CustomOptions.registryOptions()).pipe(map(subCategory => {
|
2024-04-05 09:57:39 +02:00
|
|
|
return HelperFunctions.copy(subCategory);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2024-09-12 09:49:03 +02:00
|
|
|
updateUmbrella(id: string, update: UpdateUmbrella): Observable<Umbrella> {
|
|
|
|
return this.http.post<Umbrella>(properties.monitorServiceAPIURL + id + '/umbrella', update, CustomOptions.registryOptions()).pipe(map(umbrella => {
|
2024-06-11 16:32:16 +02:00
|
|
|
return HelperFunctions.copy(umbrella);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|