import { Injectable } from '@angular/core' ; import { ApiCallService , ApiService , HttpInterceptorHandler , httpObserveOptions , resp } from '../services/api-call.service' ; import { ExportdJob } from '../settings/models/exportd-job' ; import { Observable , timer } from 'rxjs' ; import { FormControl } from '@angular/forms' ; import { catchError , map , switchMap } from 'rxjs/operators' ; import { HttpBackend , HttpClient , HttpHeaders , HttpParams , HttpResponse } from '@angular/common/http' ; import { BasicAuthInterceptor } from '../auth/interceptors/basic-auth.interceptor' ; import { AuthService } from '../auth/services/auth.service' ; import { CollectionParameters } from '../services/models/api-parameter' ; import { APIGetMultiResponse } from '../services/models/api-response' ; export const checkJobExistsValidator = ( jobService , time = 500 ) => { return ( control ) => { return timer ( time ) . pipe ( switchMap ( ( ) => { return jobService . checkJobExists ( control . value ) . pipe ( map ( ( ) => { return { typeExists : true } ; } ) , catchError ( ( ) => { return new Promise ( resolve => { resolve ( null ) ; } ) ; } ) ) ; } ) ) ; } ; } ; @ Injectable ( { providedIn : 'root' } ) export class ExportdJobService < T = ExportdJob > implements ApiService { public servicePrefix : string = 'exportdjob' ; public newServicePrefix : string = 'exportd/jobs' ; public readonly options = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , params : { } , observe : resp } ; constructor ( private api ) { } public getTask ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callGet < ExportdJob > ( this . servicePrefix + '/' + publicID , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } param params public getTasks ( params = { filter : undefined , limit : 10 , sort : 'public_id' , order : 1 , page : 1 } ) { const options = httpObserveOptions ; let httpParams = new HttpParams ( ) ; if ( params . filter !== undefined ) { const filter = JSON . stringify ( params . filter ) ; httpParams = httpParams . set ( 'filter' , filter ) ; } httpParams = httpParams . set ( 'limit' , params . limit . toString ( ) ) ; httpParams = httpParams . set ( 'sort' , params . sort ) ; httpParams = httpParams . set ( 'order' , params . order . toString ( ) ) ; httpParams = httpParams . set ( 'page' , params . page . toString ( ) ) ; options . params = httpParams ; return this . api . callGet < Array < T > > ( this . newServicePrefix , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public postTask ( taskInstance ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callPost < ExportdJob > ( this . servicePrefix + '/' , taskInstance , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public putTask ( taskInstance ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callPut ( this . servicePrefix + '/' , taskInstance , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public deleteTask ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callDelete < number > ( this . servicePrefix + '/' + publicID , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public run_task ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callGet < ExportdJob > ( this . servicePrefix + '/manual/' + publicID , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public checkJobExists ( typeName ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callGet < T > ( `${ this . servicePrefix }/name/${ typeName }` , options ) ; } }