1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient, HttpHeaders} from "@angular/common/http";
|
3
|
import {CommunityInfo} from './communityInfo';
|
4
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
5
|
import {map} from "rxjs/operators";
|
6
|
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
|
7
|
import {properties} from "../../../../environments/environment";
|
8
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
9
|
import {Stakeholder} from "../../monitor/entities/stakeholder";
|
10
|
import {StringUtils} from "../../utils/string-utils.class";
|
11
|
|
12
|
@Injectable({providedIn: 'root'})
|
13
|
export class CommunityService {
|
14
|
|
15
|
public community: BehaviorSubject<CommunityInfo> = null;
|
16
|
private promise: Promise<boolean> = null;
|
17
|
private sub;
|
18
|
|
19
|
constructor(private http: HttpClient) {
|
20
|
this.community = new BehaviorSubject(null);
|
21
|
}
|
22
|
|
23
|
ngOnDestroy() {
|
24
|
this.clearSubscriptions();
|
25
|
}
|
26
|
|
27
|
clearSubscriptions() {
|
28
|
if (this.sub instanceof Subscriber) {
|
29
|
this.sub.unsubscribe();
|
30
|
}
|
31
|
}
|
32
|
|
33
|
public getCommunityAsObservable() {
|
34
|
return this.community.asObservable();
|
35
|
}
|
36
|
|
37
|
setCommunity(community: CommunityInfo) {
|
38
|
this.community.next(community);
|
39
|
}
|
40
|
|
41
|
getCommunity(communityId: string, refresh = false) {
|
42
|
if(!this.community.value || this.community.value.communityId !== communityId || refresh) {
|
43
|
this.promise = new Promise<any>((resolve, reject) => {
|
44
|
this.sub = this.http.get<CommunityInfo>(properties.communityAPI + communityId)
|
45
|
.pipe(map(community => this.parseCommunity(community))).subscribe(community => {
|
46
|
this.community.next(community);
|
47
|
resolve();
|
48
|
},
|
49
|
error => {
|
50
|
this.community.next(null);
|
51
|
resolve();
|
52
|
})
|
53
|
});
|
54
|
}
|
55
|
return from(this.getCommunityAsync());
|
56
|
}
|
57
|
|
58
|
async getCommunityAsync() {
|
59
|
await this.promise;
|
60
|
this.clearSubscriptions();
|
61
|
return this.community.getValue();
|
62
|
}
|
63
|
|
64
|
private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] {
|
65
|
if(Array.isArray(response)) {
|
66
|
response.forEach(value => {
|
67
|
value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
|
68
|
});
|
69
|
} else {
|
70
|
response.isUpload = response.logoUrl && !StringUtils.isValidUrl(response.logoUrl);
|
71
|
}
|
72
|
return response;
|
73
|
}
|
74
|
|
75
|
|
76
|
updateCommunity(url: string, community: any) {
|
77
|
//const headers = new Headers({'Content-Type': 'application/json'});
|
78
|
//const options = new RequestOptions({headers: headers});
|
79
|
if(!community.logoUrl) {
|
80
|
community.logoUrl = '';
|
81
|
}
|
82
|
const options = {
|
83
|
headers: new HttpHeaders({
|
84
|
'Content-Type': 'application/json',
|
85
|
})
|
86
|
};
|
87
|
|
88
|
const body = JSON.stringify(community);
|
89
|
return this.http.post(url, body, options);
|
90
|
/*.map(res => res.json())*/
|
91
|
}
|
92
|
|
93
|
private parseCommunity(data: any): CommunityInfo {
|
94
|
|
95
|
const resData = Array.isArray(data) ? data[0] : data;
|
96
|
|
97
|
const community: CommunityInfo = new CommunityInfo();
|
98
|
community['title'] = resData.name;
|
99
|
community['shortTitle'] = resData.shortName;
|
100
|
community['communityId'] = resData.id;
|
101
|
community['queryId'] = resData.queryId;
|
102
|
community['logoUrl'] = resData.logoUrl;
|
103
|
community['description'] = resData.description;
|
104
|
community['date'] = resData.creationDate;
|
105
|
community['zenodoCommunity'] = resData.zenodoCommunity;
|
106
|
community['status'] = 'all';
|
107
|
if (resData.hasOwnProperty('status')) {
|
108
|
community['status'] = resData.status;
|
109
|
const status = ['all', 'hidden', 'manager'];
|
110
|
if (status.indexOf(community['status']) === -1) {
|
111
|
community['status'] = 'hidden';
|
112
|
}
|
113
|
}
|
114
|
if (resData.type != null) {
|
115
|
community['type'] = resData.type;
|
116
|
}
|
117
|
|
118
|
if (resData.managers != null) {
|
119
|
if (community['managers'] === undefined) {
|
120
|
community['managers'] = new Array<string>();
|
121
|
}
|
122
|
|
123
|
const managers = resData.managers;
|
124
|
const length = Array.isArray(managers) ? managers.length : 1;
|
125
|
|
126
|
for (let i = 0; i < length; i++) {
|
127
|
const manager = Array.isArray(managers) ? managers[i] : managers;
|
128
|
community.managers[i] = manager;
|
129
|
}
|
130
|
}
|
131
|
|
132
|
if (resData.subjects != null) {
|
133
|
if (community['subjects'] === undefined) {
|
134
|
community['subjects'] = new Array<string>();
|
135
|
}
|
136
|
|
137
|
const subjects = resData.subjects;
|
138
|
const length = Array.isArray(subjects) ? subjects.length : 1;
|
139
|
|
140
|
for (let i = 0; i < length; i++) {
|
141
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
142
|
community.subjects[i] = subject;
|
143
|
}
|
144
|
}
|
145
|
return this.checkIsUpload(community);
|
146
|
}
|
147
|
|
148
|
isRIType(community: string): Observable<boolean> {
|
149
|
return this.getCommunity(community).pipe(map(community => community.type === 'ri'));
|
150
|
}
|
151
|
|
152
|
isCommunityType(community: string): Observable<boolean> {
|
153
|
return this.getCommunity(community).pipe(map(community => community.type === 'community'));
|
154
|
}
|
155
|
}
|