Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, RequestOptions, Headers, Response} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5
//import { HttpErrorResponse } from '@angular/common/http';
6

    
7
import {EnvProperties} from '../../utils/properties/env-properties';
8
import { CustomOptions } from '../../services/servicesUtils/customOptions.class';
9

    
10
@Injectable()
11
export class MailPrefsService {
12
    constructor(private http: HttpClient ) {}
13

    
14
    getUserEmailPreferencesForCommunity (communityId:string, apiUrl:string):any {
15
      let url = apiUrl +"users/notification"+"?communityId="+communityId;
16

    
17
      return this.http.get(url, CustomOptions.getAuthOptions())
18
                      //.map(request => <any> request.json())
19
                      //.do(request => console.info("Get user email preferences for communityId= "+communityId ));
20
                      //.catch(this.handleError);
21
    }
22

    
23
    getUserEmailPreferencesForOpenaire (apiUrl:string):any {
24
      let url = apiUrl +"users/notification";
25

    
26
      //var error: HttpErrorResponse = new HttpErrorResponse({"error": {"code": 204}, "status" : 204}); // doesn't work
27
      //var response: Response = new Response({"body" : {"code":200, "data": []}, "status": 200, "headers": null, "url": "", "merge": null});
28

    
29
      return this.http.get(url, CustomOptions.getAuthOptions())
30
                      //.timeoutWith(100, Observable.throw(response))   // goes to error
31
                      //.timeoutWith(100, Observable.of(response))      // goes to
32
                      //.do(request => console.info(request))
33
                      //.map(request => <any> request.json())
34
                      //.do(request => console.info("Get user email preferences for OpenAIRE" ));
35
                      //.catch(this.handleError);
36
    }
37

    
38
    saveUserEmailPreferences (notification: any, apiUrl: string) {
39
      let url = apiUrl +"users/notification/save";
40

    
41
      let body = JSON.stringify( notification );
42
      //console.warn('Json body:  : '+body);
43
      //let headers = new Headers({ 'Content-Type': 'application/json' });
44
      //let options = new RequestOptions({ headers: headers });
45
      return this.http.post(url, body,  CustomOptions.getAuthOptionsWithBody())
46
                            //.map(res => res.json())
47
                            //.do(request => console.info("Insert Response:"+request.status) );
48
                            //.catch(this.handleError);
49
    }
50

    
51
    private handleError (error: HttpErrorResponse) {
52
      // in a real world app, we may send the error to some remote logging infrastructure
53
      // instead of just logging it to the console
54
      console.log(error);
55
      return throwError(error  || 'Server error');
56
    }
57
}
(3-3/4)