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

86 lines
3.8 KiB
TypeScript
Raw Normal View History

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<boolean> = new BehaviorSubject(false);
constructor(private http: HttpClient) {
console.log("Subscribe service constructor");
}
public initIsSubscribedToCommunity(properties: EnvProperties, pid: string) {
let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/";
this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody()).subscribe((isSubscribed) => {
this.isSubscribedSubject.next(isSubscribed);
});
}
public get isSubscribed(): Observable<boolean> {
return this.isSubscribedSubject.asObservable();
}
getCommunitySubscribers(properties: EnvProperties, pid: string) {
let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers";
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
}
getNumberOfCommunitySubscribers(properties: EnvProperties, pid: string) {
let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers/count";
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
}
isSubscribedToCommunity(properties: EnvProperties, pid: string) {
let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/";
return this.http.get<boolean>(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<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody())
.pipe(tap(isSubscribed => {
console.log("subscribe servive (subscribeToCommunity): isSubscribed: "+isSubscribed);
this.isSubscribedSubject.next(isSubscribed);}));
}
unSubscribeToCommunity(properties: EnvProperties, pid: string) {
return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody())
.pipe(tap(unSubscribed => {
console.log("subscribe servive (unSubscribeToCommunity): isSubscribed: "+!unSubscribed);
this.isSubscribedSubject.next(!unSubscribed);}));
}
subscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
let subscriber = {"email": email};
[Trunk | Library]: 1. community.service.ts: Mark unused method "isSubscribedToCommunity()" as "deprecated" (This service calls community API - made in Italy, not uoa-admin-tools - made in Greece). 2. connectSubscriber.guard.ts: Call "isSubscribedToCommunity()" method from "subscribe.service.ts" (not "community.service.ts"). 3. curator.service.ts: getCurators(properties: EnvProperties, url: string) --> getCurators(properties: EnvProperties, emails: string): Build path for request in service. 4. email.service.ts: Build request path in service a. sendEmail(url: string, email: Email) --> sendEmail(properties: EnvProperties, email: Email) b. contact(url: string, email: Email, recaptcha: string = null) --> contact(properties: EnvProperties, email: Email, recaptcha: string = null) c. Create method "notifyForNewManagers(properties: EnvProperties, communityId: string, email: Email)" (sendEmail was used for everything). 5. subscribe.service.ts: a. subscribeToCommunity(pid: string, email: string, url: string) --> subscribeToCommunity(properties: EnvProperties, pid: string, email: string) b. unSubscribeToCommunity(pid: string, email: string, url: string) --> unSubscribeToCommunity(properties: EnvProperties, pid: string, email: string) 6. layout.service.ts: getLayout(communityId: string, url: string) --> getLayout(properties: EnvProperties, communityId: string): Build path for request in service. 7. feedback.component.ts: In method "contact" from "emailService", build request path in service. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@58420 d315682c-612b-4755-9ff5-7f18f6832af3
2020-04-07 16:57:26 +02:00
return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody());
}
unSubscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
[Trunk | Library]: 1. community.service.ts: Mark unused method "isSubscribedToCommunity()" as "deprecated" (This service calls community API - made in Italy, not uoa-admin-tools - made in Greece). 2. connectSubscriber.guard.ts: Call "isSubscribedToCommunity()" method from "subscribe.service.ts" (not "community.service.ts"). 3. curator.service.ts: getCurators(properties: EnvProperties, url: string) --> getCurators(properties: EnvProperties, emails: string): Build path for request in service. 4. email.service.ts: Build request path in service a. sendEmail(url: string, email: Email) --> sendEmail(properties: EnvProperties, email: Email) b. contact(url: string, email: Email, recaptcha: string = null) --> contact(properties: EnvProperties, email: Email, recaptcha: string = null) c. Create method "notifyForNewManagers(properties: EnvProperties, communityId: string, email: Email)" (sendEmail was used for everything). 5. subscribe.service.ts: a. subscribeToCommunity(pid: string, email: string, url: string) --> subscribeToCommunity(properties: EnvProperties, pid: string, email: string) b. unSubscribeToCommunity(pid: string, email: string, url: string) --> unSubscribeToCommunity(properties: EnvProperties, pid: string, email: string) 6. layout.service.ts: getLayout(communityId: string, url: string) --> getLayout(properties: EnvProperties, communityId: string): Build path for request in service. 7. feedback.component.ts: In method "contact" from "emailService", build request path in service. git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@58420 d315682c-612b-4755-9ff5-7f18f6832af3
2020-04-07 16:57:26 +02:00
return this.http.post<any>(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<any>(url, CustomOptions.getAuthOptionsWithBody());
}
}