Project

General

Profile

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

    
6

    
7
import { CustomOptions } from '../../services/servicesUtils/customOptions.class';
8
import {catchError} from "rxjs/operators";
9

    
10
@Injectable()
11
export class ClaimsByTokenService {
12

    
13
    constructor(private http: HttpClient ) {}
14

    
15
    getClaims(openaireId: string,  apiURL:string):any {
16

    
17
        //let url = apiURL+"project/claims?projectToken="+token;
18
        let url = apiURL+"projects/"+openaireId+"/all_claims";
19

    
20
        let key = url;
21

    
22
        return this.http.get(url, CustomOptions.getAuthOptions());
23
                    //.map(res => <any> res.text())
24
                    //.map(request => <any> request.json());
25

    
26
    }
27

    
28

    
29
/*
30
    getClaims(email: string, token: string, user_token: string):any {
31
      let url = OpenaireProperties.getClaimsAPIURL();   // What else?
32
      let body = JSON.stringify( {"email": email, "token": token} );
33
      console.warn('Json body:  : '+body);
34
      let headers = new Headers({ 'Content-Type': 'application/json' });
35
      let options = new RequestOptions({ headers: headers });
36
      return this.http.post(url, body, options)
37
                            .map(res => res.json())
38
                            .do(request => console.info("Insert Response:"+request.status) )
39
                            .catch(this.handleError);
40
    }
41
*/
42

    
43
  updateClaimsCuration( selectedRight: Set<string>, selectedWrong: Set<string>, apiURL:string) {
44
    let url = apiURL + "curate/bulk";
45
    let claimsCurationInfo: any = []; //e.g.:  [{"id":"2","approved":true},{"id":"1","approved":true}]
46

    
47
    selectedRight.forEach(function(selected) {
48
      //console.info(selected);
49
      let claimCurationInfo: {"id": string, "approved": boolean} = {"id": selected, "approved": true};
50
      claimsCurationInfo.push(claimCurationInfo);
51
    });
52

    
53
    selectedWrong.forEach(function(selected) {
54
      let claimCurationInfo: any = {"id": selected, "approved": false};
55
      claimsCurationInfo.push(claimCurationInfo);
56
    });
57

    
58

    
59
    let body = JSON.stringify( claimsCurationInfo );
60
    //console.warn('Json body:  : '+body);
61

    
62
    return this.http.post(url, body,  CustomOptions.getAuthOptionsWithBody())
63
                          //.map(res => res.json())
64
                          //.do(request => console.info("Insert Response:"+request.status) )
65
                          .pipe(catchError(this.handleError));
66
  }
67

    
68

    
69
    updateClaimCuration( claimCurationInfo: {"id": string, "approved": boolean}, apiURL:string) {
70
      let url = apiURL + "curate/bulk";
71
      let claimsCurationInfo: any = []; //e.g.:  [{"id":"2","approved":true},{"id":"1","approved":true}]
72
      claimsCurationInfo.push(claimCurationInfo);
73

    
74

    
75
      let body = JSON.stringify( claimsCurationInfo );
76
      //console.warn('Json body:  : '+body);
77

    
78
      return this.http.post(url, body,  CustomOptions.getAuthOptionsWithBody())
79
                            //.map(res => res.json())
80
                            .pipe(catchError(this.handleError));
81
    }
82

    
83
  private handleError (error: HttpErrorResponse) {
84
    // in a real world app, we may send the error to some remote logging infrastructure
85
    // instead of just logging it to the console
86
    console.log(error);
87
    return throwError(error || 'Server error');
88
    //return Observable.throw(error  || 'Server error');
89
  }
90
}
(4-4/4)