openaire-library/connect/userEmailPreferences/mailPrefs.service.ts

57 lines
2.6 KiB
TypeScript
Raw Normal View History

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';
import { CustomOptions } from '../../services/servicesUtils/customOptions.class';
@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');
}
}