import {throwError as observableThrowError} from 'rxjs'; import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {catchError} from 'rxjs/operators'; import {CustomOptions} from '../../../services/servicesUtils/customOptions.class'; @Injectable() export class ClaimsService { constructor(private http: HttpClient ) { } private getClaimRequest(size : number, page : number, url :string, fromCache:boolean):any { return this.http.get(url, CustomOptions.getAuthOptions()); } getClaims( size : number, page : number, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string):any { let url = apiUrl +"claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+(types.length>0?"&"+types:types); return this.getClaimRequest(size,page,url,true); } getClaimsByUser( size : number, page : number, user:string, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string):any { //console.info('ClaimsService: getClaims for user : '+user); let url = apiUrl +"users/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+(types.length>0?"&"+types:types); return this.getClaimRequest(size,page,url,false); } getClaimsBycontext( size : number, page : number, contextId:string, keyword:string, sortby: string, descending: boolean, types: string , apiUrl:string):any { //console.info('ClaimsService: getClaims for context : '+contextId); let url = apiUrl +"contexts/"+contextId+"/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+(types.length>0?"&"+types:types); return this.getClaimRequest(size,page,url,true); } getClaimsByResult( size : number, page : number, resultId:string, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string ):any { //console.info('ClaimsService: getClaims for entity : '+resultId); let url = apiUrl +"results/"+resultId+"/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+(types.length>0?"&"+types:types); return this.getClaimRequest(size,page,url,true); } getClaimsByProject( size : number, page : number, projectId:string, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string ):any { //console.info('ClaimsService: getClaims for project : '+projectId); let url = apiUrl +"projects/"+projectId+"/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+(types.length>0?"&"+types:types); return this.getClaimRequest(size,page,url,true); } deleteClaimById(claimId:string , apiUrl:string):any{ //console.warn('Trying to delete claim with id : '+claimId); let url = apiUrl +"claims/"+claimId; // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.delete( url, CustomOptions.getAuthOptionsWithBody())//.map(request => request.json()) // .do(request => console.info("After delete" )) .pipe(catchError(this.handleError)); } deleteBulk(claimIds:string[], apiUrl:string):any{ //console.warn('Trying to delete claims with ids : '+claimIds); var url = ""; for(var claimId of claimIds){ url=url+(url.length >0 ?"&":"")+"claimId="+claimId; } url= apiUrl +"claims/bulk?"+url; // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.delete( url, CustomOptions.getAuthOptions())//.map(request => request.json()) // .do(request => console.info("After delete" )) .pipe(catchError(this.handleError)); } insertBulkClaims(claims, apiUrl:string):any{ // console.warn('Trying toinsert claims : '+claims); let url = apiUrl +"claims/bulk"; let body = JSON.stringify( claims ); //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) ) .pipe(catchError(this.handleError)); } insertClaim(claim, apiUrl:string):any{ //console.warn('Trying toinsert claim : '+claim); let url = apiUrl +"claims"; let body = JSON.stringify( claim ); // 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) ) .pipe(catchError(this.handleError)); } insertDirectRecords(records, apiUrl:string):any{ //console.warn('Trying to feedrecords : '+records); let url = apiUrl +"feed/bulk"; let body = JSON.stringify( records ); //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) ) .pipe(catchError(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 observableThrowError(error || 'Server error'); } // getClaim(id:string, apiUrl:string):any { // let url = apiUrl+"claims/"+id; // return new Promise((resolve, reject) => { // this.http.get(url) // //.map(res => res.json()) // .subscribe( // data => { // resolve(data['data']); // }, // err => { // reject(err); // } // ) // ; // }); // } }