2018-09-24 12:08:33 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Http, RequestOptions, Headers, Response} from '@angular/http';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
//import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
2018-11-08 14:49:40 +01:00
|
|
|
import { CustomOptions } from '../../services/servicesUtils/customOptions.class';
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MailPrefsService {
|
|
|
|
constructor(private http: Http ) {}
|
|
|
|
|
|
|
|
getUserEmailPreferencesForCommunity (communityId:string, apiUrl:string):any {
|
|
|
|
let url = apiUrl +"users/notification"+"?communityId="+communityId;
|
|
|
|
|
|
|
|
return this.http.get(url, CustomOptions.getAuthOptions())
|
|
|
|
.map(request => <any> request.json())
|
|
|
|
.do(request => console.info("Get user email preferences for communityId= "+communityId ));
|
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserEmailPreferencesForOpenaire (apiUrl:string):any {
|
|
|
|
let url = apiUrl +"users/notification";
|
|
|
|
|
|
|
|
//var error: HttpErrorResponse = new HttpErrorResponse({"error": {"code": 204}, "status" : 204}); // doesn't work
|
|
|
|
var response: Response = new Response({"body" : {"code":200, "data": []}, "status": 200, "headers": null, "url": "", "merge": null});
|
|
|
|
|
|
|
|
return this.http.get(url, CustomOptions.getAuthOptions())
|
|
|
|
//.timeoutWith(100, Observable.throw(response)) // goes to error
|
|
|
|
//.timeoutWith(100, Observable.of(response)) // goes to
|
|
|
|
.do(request => console.info(request))
|
|
|
|
.map(request => <any> request.json())
|
|
|
|
.do(request => console.info("Get user email preferences for OpenAIRE" ));
|
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveUserEmailPreferences (notification: any, apiUrl: string) {
|
|
|
|
let url = apiUrl +"users/notification/save";
|
|
|
|
|
|
|
|
let body = JSON.stringify( notification );
|
|
|
|
console.warn('Json body: : '+body);
|
|
|
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
|
|
|
let options = new RequestOptions({ headers: headers });
|
|
|
|
return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody())
|
|
|
|
.map(res => res.json())
|
|
|
|
.do(request => console.info("Insert Response:"+request.status) );
|
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleError (error: Response) {
|
|
|
|
// in a real world app, we may send the error to some remote logging infrastructure
|
|
|
|
// instead of just logging it to the console
|
|
|
|
console.log(error);
|
|
|
|
return Observable.throw(error || 'Server error');
|
|
|
|
}
|
|
|
|
}
|