2018-09-24 12:08:33 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Http, RequestOptions, Headers, Response} from '@angular/http';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
|
|
|
import {Observable, throwError} from 'rxjs';
|
2018-09-24 12:08:33 +02:00
|
|
|
//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 {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient ) {}
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
getUserEmailPreferencesForCommunity (communityId:string, apiUrl:string):any {
|
|
|
|
let url = apiUrl +"users/notification"+"?communityId="+communityId;
|
|
|
|
|
|
|
|
return this.http.get(url, CustomOptions.getAuthOptions())
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(request => <any> request.json())
|
2019-02-14 11:44:30 +01:00
|
|
|
//.do(request => console.info("Get user email preferences for communityId= "+communityId ));
|
2018-09-24 12:08:33 +02:00
|
|
|
//.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
|
2019-06-03 15:20:36 +02:00
|
|
|
//var response: Response = new Response({"body" : {"code":200, "data": []}, "status": 200, "headers": null, "url": "", "merge": null});
|
2018-09-24 12:08:33 +02:00
|
|
|
|
|
|
|
return this.http.get(url, CustomOptions.getAuthOptions())
|
|
|
|
//.timeoutWith(100, Observable.throw(response)) // goes to error
|
|
|
|
//.timeoutWith(100, Observable.of(response)) // goes to
|
2019-02-14 11:44:30 +01:00
|
|
|
//.do(request => console.info(request))
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(request => <any> request.json())
|
2019-02-14 11:44:30 +01:00
|
|
|
//.do(request => console.info("Get user email preferences for OpenAIRE" ));
|
2018-09-24 12:08:33 +02:00
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveUserEmailPreferences (notification: any, apiUrl: string) {
|
|
|
|
let url = apiUrl +"users/notification/save";
|
|
|
|
|
|
|
|
let body = JSON.stringify( notification );
|
2019-02-14 11:44:30 +01:00
|
|
|
//console.warn('Json body: : '+body);
|
2019-06-03 15:20:36 +02:00
|
|
|
//let headers = new Headers({ 'Content-Type': 'application/json' });
|
|
|
|
//let options = new RequestOptions({ headers: headers });
|
2018-09-24 12:08:33 +02:00
|
|
|
return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody())
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => res.json())
|
2019-02-14 11:44:30 +01:00
|
|
|
//.do(request => console.info("Insert Response:"+request.status) );
|
2018-09-24 12:08:33 +02:00
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
private handleError (error: HttpErrorResponse) {
|
2018-09-24 12:08:33 +02:00
|
|
|
// 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);
|
2019-06-03 15:20:36 +02:00
|
|
|
return throwError(error || 'Server error');
|
2018-09-24 12:08:33 +02:00
|
|
|
}
|
|
|
|
}
|