Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient} from '@angular/common/http';
3
import {map} from "rxjs/operators";
4
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
5
import {EnvProperties} from "../properties/env-properties";
6

    
7
@Injectable()
8
export class SubscribeService {
9

    
10
  constructor(private http: HttpClient) {
11
  }
12

    
13
  getCommunitySubscribers(properties: EnvProperties, pid: string) {
14
    let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers";
15
    return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
16
  }
17

    
18
  isSubscribedToCommunity(properties: EnvProperties, pid: string, email: string) {
19
    let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers";
20
    return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
21
      .pipe(map(res => {
22
        if (res['status'] && res['status'] != 200) {
23
          return null;
24
        }
25
        if (res['subscribers'] && res['subscribers'] != null) {
26

    
27
          for (var i = 0; i < res['subscribers'].length; i++) {
28
            if (res['subscribers'][i] != null && res['subscribers'][i].email == email) {
29
              return true;
30
            }
31
          }
32
        }
33
        return false;
34

    
35
      }));
36
  }
37

    
38
  subscribeToCommunity(pid: string, email: string, url: string) {
39
    let subscriber = {"email": email};
40
    return this.http.post<any>(url + "/community/" + pid + "/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody());
41
  }
42

    
43
  unSubscribeToCommunity(pid: string, email: string, url: string) {
44
    return this.http.post<any>(url + "/community/" + pid + "/subscribers/delete", JSON.stringify([email]), CustomOptions.getAuthOptionsWithBody());
45
  }
46

    
47
  getCommunitiesSubscribedTo(properties: EnvProperties, email: string) {
48
    let url = properties.adminToolsAPIURL + "/subscriber/communities?email=" + email;
49
    return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
50
  }
51
}
    (1-1/1)