Project

General

Profile

1 50448 sofia.balt
import { Injectable } from '@angular/core';
2 54903 k.triantaf
import { Http,  Headers, RequestOptions } from '@angular/http';
3 55964 argiro.kok
import {HttpClient, HttpHeaders} from "@angular/common/http";
4 51076 sofia.balt
import { CommunityInfo } from './communityInfo';
5 54965 k.triantaf
import {EnvProperties} from '../../utils/properties/env-properties';
6 55964 argiro.kok
import {map} from "rxjs/operators";
7
import {COOKIE} from "../../login/utils/helper.class";
8 50448 sofia.balt
9
@Injectable()
10
export class CommunityService {
11
12 55964 argiro.kok
    constructor(private http: HttpClient) {
13 50449 sofia.balt
      }
14
15 54965 k.triantaf
    getCommunity(properties: EnvProperties, url: string) {
16
        return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
17 55964 argiro.kok
                        //.map(res => <any> res.json())
18
                        .pipe(map(res => this.parseCommunity(res)));
19 50965 sofia.balt
    }
20 51342 sofia.balt
21 54965 k.triantaf
    updateCommunity(url: string, community: any) {
22 55964 argiro.kok
        //const headers = new Headers({'Content-Type': 'application/json'});
23
        //const options = new RequestOptions({headers: headers});
24
25
        const options = {
26
          headers: new HttpHeaders({
27
            'Content-Type': 'application/json',
28
          })
29
        };
30
31 54965 k.triantaf
        const body = JSON.stringify(community);
32
        return this.http.post(url, body, options);
33 51344 sofia.balt
                          /*.map(res => res.json())*/
34 51342 sofia.balt
    }
35
36 54965 k.triantaf
    isCommunityManager(properties: EnvProperties, url: string, manager: string) {
37
      return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
38 55964 argiro.kok
                      //.map(res => <any> res.json())
39
                      .pipe(map(res => this.parseCommunity(res)))
40
                      .pipe(map(community => community.managers.indexOf(manager) !== -1));
41 51261 argiro.kok
    }
42 54903 k.triantaf
43 54965 k.triantaf
    isRIType(properties: EnvProperties, url: string) {
44
      return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
45 55964 argiro.kok
          //.map(res => <any> res.json())
46
          .pipe(map(res => this.parseCommunity(res)))
47
          .pipe(map(community => (community && community.type && community.type === 'ri')));
48 51526 argiro.kok
    }
49 54903 k.triantaf
50 54965 k.triantaf
    isCommunityType(properties: EnvProperties, url: string) {
51
        return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
52 55964 argiro.kok
            //.map(res => <any> res.json())
53
            .pipe(map(res => this.parseCommunity(res)))
54
            .pipe(map(community => (community && community.type && community.type === 'community')));
55 54965 k.triantaf
    }
56 51826 argiro.kok
57 54965 k.triantaf
    isSubscribedToCommunity(pid: string, email: string, url: string) {
58
      return this.http.get(url + '/community/' + pid + '/subscribers')
59 55964 argiro.kok
                      //.map(res =>  ((<any>res === '') ? {} : <any> res.json()))
60
                      .pipe(map(res => {
61
                        if (res['subscribers'] && res['subscribers'] != null) {
62 51826 argiro.kok
63 55964 argiro.kok
                          for (let i = 0; i < res['subscribers'].length; i++ ) {
64
                            if (res['subscribers'][i] != null && res['subscribers'][i].email === email) {
65 51826 argiro.kok
                              return true;
66
                            }
67
                          }
68
                        }
69
                        return false;
70
71 55964 argiro.kok
                      }));
72 51826 argiro.kok
    }
73 50965 sofia.balt
74 54965 k.triantaf
    private parseCommunity(data: any): CommunityInfo {
75 50965 sofia.balt
76 54965 k.triantaf
        const resData = Array.isArray(data) ? data[0] : data;
77 50965 sofia.balt
78 54965 k.triantaf
        const community: CommunityInfo = new CommunityInfo();
79 54903 k.triantaf
        community['title'] = resData.name;
80
        community['shortTitle'] = resData.shortName;
81
        community['communityId'] = resData.id;
82
        community['queryId'] = resData.queryId;
83
        community['logoUrl'] = resData.logoUrl;
84
        community['description'] = resData.description;
85
        community['date'] = resData.creationDate;
86
        community['zenodoCommunity'] = resData.zenodoCommunity;
87 54965 k.triantaf
        community['status'] = 'all';
88
        if (resData.hasOwnProperty('status')) {
89 54903 k.triantaf
          community['status'] = resData.status;
90 54965 k.triantaf
          const status = ['all', 'hidden', 'manager'];
91
          if (status.indexOf(community['status']) === -1) {
92
            community['status'] = 'hidden';
93 54903 k.triantaf
          }
94
        }
95
        if (resData.type != null) {
96
              community['type'] = resData.type;
97
        }
98 50965 sofia.balt
99 54965 k.triantaf
        if (resData.managers != null) {
100
            if (community['managers'] === undefined) {
101 54903 k.triantaf
              community['managers'] = new Array<string>();
102
            }
103 50965 sofia.balt
104 54965 k.triantaf
            const managers = resData.managers;
105
            const length = Array.isArray(managers) ? managers.length : 1;
106 50965 sofia.balt
107 54965 k.triantaf
            for (let i = 0; i < length; i++) {
108
              const manager = Array.isArray(managers) ? managers[i] : managers;
109 54903 k.triantaf
              community.managers[i] = manager;
110
            }
111
        }
112 51511 sofia.balt
113 54965 k.triantaf
        if (resData.subjects != null) {
114
            if (community['subjects'] === undefined) {
115 54903 k.triantaf
              community['subjects'] = new Array<string>();
116
            }
117 51025 sofia.balt
118 54965 k.triantaf
            const subjects = resData.subjects;
119
            const length = Array.isArray(subjects) ? subjects.length : 1;
120 51025 sofia.balt
121 54965 k.triantaf
            for (let i = 0; i < length; i++) {
122
              const subject = Array.isArray(subjects) ? subjects[i] : subjects;
123 54903 k.triantaf
              community.subjects[i] = subject;
124
            }
125
        }
126
        return community;
127 50965 sofia.balt
    }
128
129 50448 sofia.balt
}