openaire-library/connect/community/community.service.ts

156 lines
4.9 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {CommunityInfo} from './communityInfo';
import {EnvProperties} from '../../utils/properties/env-properties';
import {map} from "rxjs/operators";
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
import {properties} from "../../../../environments/environment";
import {HelperFunctions} from "../../utils/HelperFunctions.class";
import {Stakeholder} from "../../monitor/entities/stakeholder";
import {StringUtils} from "../../utils/string-utils.class";
@Injectable({providedIn: 'root'})
export class CommunityService {
public community: BehaviorSubject<CommunityInfo> = null;
private promise: Promise<boolean> = null;
private sub;
constructor(private http: HttpClient) {
this.community = new BehaviorSubject(null);
}
ngOnDestroy() {
this.clearSubscriptions();
}
clearSubscriptions() {
if (this.sub instanceof Subscriber) {
this.sub.unsubscribe();
}
}
public getCommunityAsObservable() {
return this.community.asObservable();
}
setCommunity(community: CommunityInfo) {
this.community.next(community);
}
getCommunity(communityId: string, refresh = false) {
if(!this.community.value || this.community.value.communityId !== communityId || refresh) {
this.promise = new Promise<any>((resolve, reject) => {
this.sub = this.http.get<CommunityInfo>(properties.communityAPI + communityId)
.pipe(map(community => this.parseCommunity(community))).subscribe(community => {
this.community.next(community);
resolve();
},
error => {
this.community.next(null);
resolve();
})
});
}
return from(this.getCommunityAsync());
}
async getCommunityAsync() {
await this.promise;
this.clearSubscriptions();
return this.community.getValue();
}
private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] {
if(Array.isArray(response)) {
response.forEach(value => {
value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
});
} else {
response.isUpload = response.logoUrl && !StringUtils.isValidUrl(response.logoUrl);
}
return response;
}
updateCommunity(url: string, community: any) {
//const headers = new Headers({'Content-Type': 'application/json'});
//const options = new RequestOptions({headers: headers});
if(!community.logoUrl) {
community.logoUrl = '';
}
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
const body = JSON.stringify(community);
return this.http.post(url, body, options);
/*.map(res => res.json())*/
}
public parseCommunity(data: any): CommunityInfo {
const resData = Array.isArray(data) ? data[0] : data;
const community: CommunityInfo = new CommunityInfo();
community['title'] = resData.name;
community['shortTitle'] = resData.shortName;
community['communityId'] = resData.id;
community['queryId'] = resData.queryId;
community['logoUrl'] = resData.logoUrl;
community['description'] = resData.description;
community['date'] = resData.creationDate;
community['zenodoCommunity'] = resData.zenodoCommunity;
community['status'] = 'all';
if (resData.hasOwnProperty('status')) {
community['status'] = resData.status;
const status = ['all', 'hidden', 'manager'];
if (status.indexOf(community['status']) === -1) {
community['status'] = 'hidden';
}
}
if (resData.type != null) {
community['type'] = resData.type;
}
if (resData.managers != null) {
if (community['managers'] === undefined) {
community['managers'] = new Array<string>();
}
const managers = resData.managers;
const length = Array.isArray(managers) ? managers.length : 1;
for (let i = 0; i < length; i++) {
const manager = Array.isArray(managers) ? managers[i] : managers;
community.managers[i] = manager;
}
}
if (resData.subjects != null) {
if (community['subjects'] === undefined) {
community['subjects'] = new Array<string>();
}
const subjects = resData.subjects;
const length = Array.isArray(subjects) ? subjects.length : 1;
for (let i = 0; i < length; i++) {
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
community.subjects[i] = subject;
}
}
return this.checkIsUpload(community);
}
isRIType(community: string): Observable<boolean> {
return this.getCommunity(community).pipe(map(community => community.type === 'ri'));
}
isCommunityType(community: string): Observable<boolean> {
return this.getCommunity(community).pipe(map(community => community.type === 'community'));
}
}