2019-06-05 15:33:18 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2020-04-28 12:15:44 +02:00
|
|
|
import {map, tap} from "rxjs/operators";
|
2019-06-03 15:20:36 +02:00
|
|
|
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
|
2019-06-05 15:33:18 +02:00
|
|
|
import {EnvProperties} from "../properties/env-properties";
|
2020-11-12 17:49:24 +01:00
|
|
|
import {BehaviorSubject, Observable, Subscriber} from "rxjs";
|
2018-07-06 12:33:16 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SubscribeService {
|
2020-04-28 12:15:44 +02:00
|
|
|
private isSubscribedSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);
|
2018-07-06 12:33:16 +02:00
|
|
|
|
2020-05-05 10:46:10 +02:00
|
|
|
constructor(private http: HttpClient) {}
|
2020-11-12 17:49:24 +01:00
|
|
|
sub;
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
|
|
|
clearSubscriptions(){
|
|
|
|
if (this.sub instanceof Subscriber) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 12:15:44 +02:00
|
|
|
public initIsSubscribedToCommunity(properties: EnvProperties, pid: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType +"/" + pid + "/is-subscriber/";
|
2020-11-12 17:49:24 +01:00
|
|
|
this.sub = this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody()).subscribe((isSubscribed) => {
|
2020-04-28 12:15:44 +02:00
|
|
|
this.isSubscribedSubject.next(isSubscribed);
|
2020-08-11 14:16:50 +02:00
|
|
|
}, error => {
|
|
|
|
this.isSubscribedSubject.error(error);
|
2020-04-28 12:15:44 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public get isSubscribed(): Observable<boolean> {
|
|
|
|
return this.isSubscribedSubject.asObservable();
|
2019-11-08 13:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getCommunitySubscribers(properties: EnvProperties, pid: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL + "/" + properties.adminToolsPortalType + "/" + pid + "/subscribers";
|
2019-11-08 13:07:39 +01:00
|
|
|
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:16:38 +02:00
|
|
|
getNumberOfCommunitySubscribers(properties: EnvProperties, pid: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType +"/" + pid + "/subscribers/count";
|
2020-04-10 07:16:38 +02:00
|
|
|
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
|
|
|
}
|
2019-11-08 13:07:39 +01:00
|
|
|
|
2020-04-10 07:16:38 +02:00
|
|
|
isSubscribedToCommunity(properties: EnvProperties, pid: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
let url = properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType +"/" + pid + "/is-subscriber/";
|
2020-04-10 08:09:15 +02:00
|
|
|
return this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody())
|
2020-04-10 07:16:38 +02:00
|
|
|
.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;
|
2019-11-08 13:07:39 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:16:38 +02:00
|
|
|
subscribeToCommunity(properties: EnvProperties, pid: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType +"/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody())
|
2020-04-28 12:15:44 +02:00
|
|
|
.pipe(tap(isSubscribed => {
|
|
|
|
this.isSubscribedSubject.next(isSubscribed);}));
|
2020-04-10 07:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unSubscribeToCommunity(properties: EnvProperties, pid: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType+"/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody())
|
2020-04-28 12:15:44 +02:00
|
|
|
.pipe(tap(unSubscribed => {
|
|
|
|
this.isSubscribedSubject.next(!unSubscribed);}));
|
2020-04-10 07:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
|
2019-11-08 13:07:39 +01:00
|
|
|
let subscriber = {"email": email};
|
2020-09-24 13:18:24 +02:00
|
|
|
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType+"/" + pid + "/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody());
|
2019-11-08 13:07:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 07:16:38 +02:00
|
|
|
unSubscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
|
2020-09-24 13:18:24 +02:00
|
|
|
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType+"/" + pid + "/subscribers/delete", JSON.stringify([email]), CustomOptions.getAuthOptionsWithBody());
|
2019-11-08 13:07:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:09:15 +02:00
|
|
|
getCommunitiesSubscribedTo(properties: EnvProperties/*, email: string*/) {
|
|
|
|
let url = properties.adminToolsAPIURL + "/subscriber/communities";//?email=" + email;
|
|
|
|
return this.http.get<any>(url, CustomOptions.getAuthOptionsWithBody());
|
2019-11-08 13:07:39 +01:00
|
|
|
}
|
2018-07-06 12:33:16 +02:00
|
|
|
}
|