Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient} from '@angular/common/http';
3
import {map, tap} from "rxjs/operators";
4
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
5
import {EnvProperties} from "../properties/env-properties";
6
import {COOKIE} from "../../login/utils/helper.class";
7
import {BehaviorSubject, Observable} from "rxjs";
8

    
9
@Injectable()
10
export class SubscribeService {
11
  private isSubscribedSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);
12

    
13
  constructor(private http: HttpClient) {}
14

    
15
  public initIsSubscribedToCommunity(properties: EnvProperties, pid: string) {
16
    let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/";
17
    this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody()).subscribe((isSubscribed) => {
18
      this.isSubscribedSubject.next(isSubscribed);
19
    });
20
  }
21

    
22
  public get isSubscribed(): Observable<boolean> {
23
    return this.isSubscribedSubject.asObservable();
24
  }
25

    
26
  getCommunitySubscribers(properties: EnvProperties, pid: string) {
27
    let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers";
28
    return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
29
  }
30

    
31
  getNumberOfCommunitySubscribers(properties: EnvProperties, pid: string) {
32
    let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers/count";
33
    return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
34
  }
35

    
36
  isSubscribedToCommunity(properties: EnvProperties, pid: string) {
37
    let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/";
38
    return this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody())
39
      .pipe(map(res => {
40
        // if (res['status'] && res['status'] != 200) {
41
        //   return null;
42
        // }
43
        // if (res['subscribers'] && res['subscribers'] != null) {
44
        //
45
        //   for (var i = 0; i < res['subscribers'].length; i++) {
46
        //     if (res['subscribers'][i] != null && res['subscribers'][i].email == email) {
47
        //       return true;
48
        //     }
49
        //   }
50
        // }
51
        // return false;
52
        return res;
53
      }));
54
  }
55

    
56
  subscribeToCommunity(properties: EnvProperties, pid: string) {
57
    return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody())
58
      .pipe(tap(isSubscribed => {
59
        this.isSubscribedSubject.next(isSubscribed);}));
60
  }
61

    
62
  unSubscribeToCommunity(properties: EnvProperties, pid: string) {
63
    return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody())
64
      .pipe(tap(unSubscribed => {
65
        this.isSubscribedSubject.next(!unSubscribed);}));
66
  }
67

    
68
  subscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
69
    let subscriber = {"email": email};
70
    return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody());
71
  }
72

    
73
  unSubscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
74
    return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscribers/delete", JSON.stringify([email]), CustomOptions.getAuthOptionsWithBody());
75
  }
76

    
77
  getCommunitiesSubscribedTo(properties: EnvProperties/*, email: string*/) {
78
    let url = properties.adminToolsAPIURL + "/subscriber/communities";//?email=" + email;
79
    return this.http.get<any>(url, CustomOptions.getAuthOptionsWithBody());
80
  }
81
}
    (1-1/1)