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

141 lines
4.5 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {CommunityInfo} from './communityInfo';
import {map} from "rxjs/operators";
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
import {properties} from "../../../../environments/environment";
import {StringUtils} from "../../utils/string-utils.class";
import {SelectionCriteria} from "../../utils/entities/contentProvider";
@Injectable({providedIn: 'root'})
export class CommunityService {
public community: BehaviorSubject<CommunityInfo> = new BehaviorSubject(null);
public communityId: string = null;
private promise: Promise<void> = null;
private subs = [];
constructor(private http: HttpClient) {}
clearSubscriptions() {
this.subs.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
public getCommunityAsObservable() {
return this.community.asObservable();
}
setCommunity(community: CommunityInfo) {
this.community.next(community);
}
getCommunityInfo(communityId: string) {
return this.http.get<CommunityInfo>(properties.communityAPI + communityId)
.pipe(map(community => this.parseCommunity(community)));
}
getCommunity(communityId: string, refresh = false) {
if (this.communityId !== communityId || !this.community.value || refresh) {
this.communityId = communityId;
this.promise = new Promise<void>((resolve, reject) => {
this.subs.push(this.getCommunityInfo(communityId).subscribe(community => {
this.community.next(community);
resolve();
},
error => {
this.community.next(null);
resolve();
}));
});
}
return from(this.getCommunityAsync());
[Trunk | Library]: 1. community.service.ts: a. Add BehaviorSubject for community. b. Method "isCommunityManager" replaced by "isCommunityManagerByState" (filtering applied in community stored in BehaviorSubject). c. Method "isRIType" replaced by "isRITypeByState" (filtering applied in community stored in BehaviorSubject). d. Method "isCommunityType" replaced by "isCommunityTypeByState" (filtering applied in community stored in BehaviorSubject). 2. connectAdminLoginGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityManagerByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 3. connectCommunityGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityTypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 4. connectRIGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isRITypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 5. isRouteEnabled.guard.ts: Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 6. curator.service.ts: [Undo change of r59073] Use "useCache" property, not "useLongCache" for curators. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59140 d315682c-612b-4755-9ff5-7f18f6832af3
2020-07-17 01:08:10 +02:00
}
async getCommunityAsync() {
await this.promise;
// this.clearSubscriptions();
return this.community.getValue();
}
[Trunk | Library]: 1. community.service.ts: a. Add BehaviorSubject for community. b. Method "isCommunityManager" replaced by "isCommunityManagerByState" (filtering applied in community stored in BehaviorSubject). c. Method "isRIType" replaced by "isRITypeByState" (filtering applied in community stored in BehaviorSubject). d. Method "isCommunityType" replaced by "isCommunityTypeByState" (filtering applied in community stored in BehaviorSubject). 2. connectAdminLoginGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityManagerByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 3. connectCommunityGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityTypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 4. connectRIGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isRITypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 5. isRouteEnabled.guard.ts: Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 6. curator.service.ts: [Undo change of r59073] Use "useCache" property, not "useLongCache" for curators. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59140 d315682c-612b-4755-9ff5-7f18f6832af3
2020-07-17 01:08:10 +02:00
updateCommunity(url: string, community: any) {
if (!community.logoUrl) {
community.logoUrl = '';
}
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
return this.http.post(url, community, options);
[Trunk | Library]: 1. community.service.ts: a. Add BehaviorSubject for community. b. Method "isCommunityManager" replaced by "isCommunityManagerByState" (filtering applied in community stored in BehaviorSubject). c. Method "isRIType" replaced by "isRITypeByState" (filtering applied in community stored in BehaviorSubject). d. Method "isCommunityType" replaced by "isCommunityTypeByState" (filtering applied in community stored in BehaviorSubject). 2. connectAdminLoginGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityManagerByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 3. connectCommunityGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityTypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 4. connectRIGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isRITypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 5. isRouteEnabled.guard.ts: Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 6. curator.service.ts: [Undo change of r59073] Use "useCache" property, not "useLongCache" for curators. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59140 d315682c-612b-4755-9ff5-7f18f6832af3
2020-07-17 01:08:10 +02:00
}
public updateSubjects(subjects: string[], fos: string[], sdg: string[]) {
let communityInfo: CommunityInfo = this.community.value;
communityInfo.subjects = subjects;
communityInfo.fos = fos;
communityInfo.sdg = sdg;
this.community.next(communityInfo);
}
public updateAdvancedCriteria(selectionCriteria: SelectionCriteria) {
let communityInfo: CommunityInfo = this.community.value;
communityInfo.selectionCriteria = selectionCriteria;
this.community.next(communityInfo);
}
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';
community.type = resData.type;
if (resData.hasOwnProperty('status')) {
community.status = resData.status;
const status = ['all', 'hidden', 'manager'];
if (status.indexOf(community['status']) === -1) {
community.status = 'hidden';
}
}
if (resData.subjects != null) {
community.subjects = Array.isArray(resData.subjects)?resData.subjects:[resData.subjects];
} else {
community.subjects = [];
}
if (resData.sdg != null) {
community.sdg = Array.isArray(resData.sdg)?resData.sdg:[resData.sdg];
} else {
community.sdg = [];
}
if (resData.fos != null) {
community.fos = Array.isArray(resData.fos)?resData.fos:[resData.fos];
} else {
community.fos = [];
}
if (resData.advancedConstraint != null) {
community.selectionCriteria = resData.advancedConstraint;
} else {
community.selectionCriteria = new SelectionCriteria();
}
return CommunityInfo.checkIsUpload(community);
[Trunk | Library]: 1. community.service.ts: a. Add BehaviorSubject for community. b. Method "isCommunityManager" replaced by "isCommunityManagerByState" (filtering applied in community stored in BehaviorSubject). c. Method "isRIType" replaced by "isRITypeByState" (filtering applied in community stored in BehaviorSubject). d. Method "isCommunityType" replaced by "isCommunityTypeByState" (filtering applied in community stored in BehaviorSubject). 2. connectAdminLoginGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityManagerByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 3. connectCommunityGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isCommunityTypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 4. connectRIGuard.guard.ts: a. Get properties from environment (no service needed). b. Call method "isRITypeByState" of CommunityService. c. Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 5. isRouteEnabled.guard.ts: Add "canDeactivate" method to unsubscribe (TODO: call method from routing modules). 6. curator.service.ts: [Undo change of r59073] Use "useCache" property, not "useLongCache" for curators. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59140 d315682c-612b-4755-9ff5-7f18f6832af3
2020-07-17 01:08:10 +02:00
}
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'));
}
}