import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {map, tap} from "rxjs/operators"; import {CustomOptions} from "../../services/servicesUtils/customOptions.class"; import {EnvProperties} from "../properties/env-properties"; import {COOKIE} from "../../login/utils/helper.class"; import {BehaviorSubject, Observable} from "rxjs"; @Injectable() export class SubscribeService { private isSubscribedSubject: BehaviorSubject = new BehaviorSubject(false); constructor(private http: HttpClient) {} public initIsSubscribedToCommunity(properties: EnvProperties, pid: string) { let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/"; this.http.get(url, CustomOptions.getAuthOptionsWithBody()).subscribe((isSubscribed) => { this.isSubscribedSubject.next(isSubscribed); }, error => { this.isSubscribedSubject.error(error); }); } public get isSubscribed(): Observable { return this.isSubscribedSubject.asObservable(); } getCommunitySubscribers(properties: EnvProperties, pid: string) { let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers"; return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); } getNumberOfCommunitySubscribers(properties: EnvProperties, pid: string) { let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers/count"; return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); } isSubscribedToCommunity(properties: EnvProperties, pid: string) { let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/"; return this.http.get(url, CustomOptions.getAuthOptionsWithBody()) .pipe(map(res => { // if (res['status'] && res['status'] != 200) { // return null; // } // if (res['subscribers'] && res['subscribers'] != null) { // // for (var i = 0; i < res['subscribers'].length; i++) { // if (res['subscribers'][i] != null && res['subscribers'][i].email == email) { // return true; // } // } // } // return false; return res; })); } subscribeToCommunity(properties: EnvProperties, pid: string) { return this.http.post(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody()) .pipe(tap(isSubscribed => { this.isSubscribedSubject.next(isSubscribed);})); } unSubscribeToCommunity(properties: EnvProperties, pid: string) { return this.http.post(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody()) .pipe(tap(unSubscribed => { this.isSubscribedSubject.next(!unSubscribed);})); } subscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) { let subscriber = {"email": email}; return this.http.post(properties.adminToolsAPIURL + "/community/" + pid + "/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody()); } unSubscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) { return this.http.post(properties.adminToolsAPIURL + "/community/" + pid + "/subscribers/delete", JSON.stringify([email]), CustomOptions.getAuthOptionsWithBody()); } getCommunitiesSubscribedTo(properties: EnvProperties/*, email: string*/) { let url = properties.adminToolsAPIURL + "/subscriber/communities";//?email=" + email; return this.http.get(url, CustomOptions.getAuthOptionsWithBody()); } }