openaire-library/utils/subscribe/subscribe.service.ts

48 lines
2.2 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {map} from "rxjs/operators";
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
import {EnvProperties} from "../properties/env-properties";
@Injectable()
export class SubscribeService {
constructor(private http:HttpClient) {
}
getCommunitySubscribers(properties: EnvProperties, pid:string){
let url = properties.adminToolsAPIURL+ "/community/"+pid+"/subscribers";
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
}
isSubscribedToCommunity(properties: EnvProperties, pid:string, email:string){
let url = properties.adminToolsAPIURL + "/community/"+pid+"/subscribers";
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.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;
}));
}
subscribeToCommunity(pid:string, email:string, url:string){
let subscriber = {"email":email};
return this.http.post<any>(url+"/community/"+pid+"/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody());
}
unSubscribeToCommunity(pid:string, email:string, url:string){
return this.http.post<any>(url+"/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<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
}
}