Project

General

Profile

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

    
6
import {EnvProperties} from '../../utils/properties/env-properties';
7
import {CustomOptions} from '../../claims/claim-utils/service/customOptions.class';
8

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

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

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

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

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

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

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

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

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