import { Injectable } from '@angular/core' ; import { HttpInterceptorHandler , ApiCallService , ApiService , httpFileOptions , httpObserveOptions } from '../services/api-call.service' ; import { HttpBackend , HttpClient , HttpParams , HttpResponse } from '@angular/common/http' ; import { FormControl } from '@angular/forms' ; import { switchMap , map , catchError } from 'rxjs/operators' ; import { Observable , timer } from 'rxjs' ; import { DocTemplate } from './models/cmdb-doctemplate' ; import { BasicAuthInterceptor } from '../auth/interceptors/basic-auth.interceptor' ; import { CollectionParameters } from '../services/models/api-parameter' ; import { APIGetMultiResponse } from '../services/models/api-response' ; @ Injectable ( { providedIn : 'root' } ) export class DocapiService < T = DocTemplate > implements ApiService { public readonly servicePrefix : string = 'docapi/template' ; public readonly newServicePrefix : string = 'docs/template' ; constructor ( private api ) { } public getDocTemplateList ( 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 < T > ( `${ this . newServicePrefix }` , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public getObjectDocTemplateList ( typeId ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; const searchfilter = { template_type : 'OBJECT' , template_parameters : { type : typeId } } ; return this . api . callGet < T > ( `${ this . servicePrefix }/by/${ JSON . stringify ( searchfilter ) }` , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } public getRenderedObjectDoc ( templateId , objectId ) { return this . api . callGet < any > ( `${ this . servicePrefix }/${ templateId }/render/${ objectId }` , httpFileOptions ) ; } public getDocTemplate ( publicId ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; return this . api . callGet < DocTemplate > ( this . servicePrefix + '/' + publicId , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } public postDocTemplate ( docInstance ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; return this . api . callPost < DocTemplate > ( this . servicePrefix + '/' , docInstance , options ) ; } public putDocTemplate ( docInstance ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; return this . api . callPut ( this . servicePrefix + '/' , docInstance , options ) ; } public deleteDocTemplate ( publicID ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; return this . api . callDelete < number > ( this . servicePrefix + '/' + publicID , options ) ; } public checkDocTemplateExists ( docName ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; return this . api . callGet < T > ( `${ this . servicePrefix }/name/${ docName }` , options ) ; } } export const checkDocTemplateExistsValidator = ( docApiService , time = 500 ) => { return ( control ) => { return timer ( time ) . pipe ( switchMap ( ( ) => { return docApiService . checkDocTemplateExists ( control . value ) . pipe ( map ( ( ) => { return { docTemplateExists : true } ; } ) , catchError ( ( ) => { return new Promise ( resolve => { resolve ( null ) ; } ) ; } ) ) ; } ) ) ; } ; } ;