import { Injectable } from '@angular/core' ; import { catchError , map , switchMap } from 'rxjs/operators' ; import { ApiCallService , ApiService , httpObserveOptions , HttpProtocolHelper , resp , } from '../../services/api-call.service' ; import { ValidatorService } from '../../services/validator.service' ; import { CmdbCategory , CmdbCategoryNode , CmdbCategoryTree } from '../models/cmdb-category' ; import { FormControl } from '@angular/forms' ; import { Observable , timer } from 'rxjs' ; import { HttpHeaders , HttpParams , HttpResponse } from '@angular/common/http' ; import { APIDeleteSingleResponse , APIGetMultiResponse , APIGetSingleResponse , APIInsertSingleResponse , APIUpdateSingleResponse } from '../../services/models/api-response' ; import { CollectionParameters } from '../../services/models/api-parameter' ; export const checkCategoryExistsValidator = ( categoryService , time = 500 ) => { return ( control ) => { return timer ( time ) . pipe ( switchMap ( ( ) => { return categoryService . getCategoryByName ( control . value ) . pipe ( map ( ( response ) => { if ( response === null ) { return null ; } else { return { categoryExists : true } ; } } ) , catchError ( ( ) => { return new Promise ( resolve => { resolve ( null ) ; } ) ; } ) ) ; } ) ) ; } ; } ; @ Injectable ( { providedIn : 'root' } ) export class CategoryService < T = CmdbCategory > implements ApiService { /**
     * Nested url path value inside the REST structure
     */ public servicePrefix : string = 'categories' ; public options = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , params : { } , observe : resp } ; constructor ( private api ) { } public getCategories ( params = { filter : undefined , limit : 10 , sort : 'public_id' , order : 1 , page : 1 } ) { const options = this . options ; 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 . servicePrefix + '/' , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } /**
     * Get a category by id
     */ public getCategory ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callGet < T > ( this . servicePrefix + '/' + publicID , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return undefined ; } return apiResponse . body . result ; } ) ) ; } param category public getCategoryByName ( category ) { const params = encodeURIComponent ( `{"name": "${ category }"}` ) ; return this . api . callGet < T > ( this . servicePrefix + `/?filter=` + params ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . body . count === 0 ) { return null ; } else { return apiResponse . body . results [ 0 ] ; } } ) ) ; } param regex public getCategoriesByName ( regex ) { regex = ValidatorService . validateRegex ( regex ) . trim ( ) ; const filter = { $or : [ { name : { $regex : regex , $options : 'ismx' } } , { label : { $regex : regex , $options : 'ismx' } } ] } ; const options = HttpProtocolHelper . createHttpProtocolOptions ( this . options , JSON . stringify ( filter ) , 0 ) ; return this . api . callGet < T [ ] > ( this . servicePrefix + '/' , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . body . count === 0 ) { return null ; } else { return apiResponse . body . results ; } } ) ) ; } /**
     * Get the complete category tree with
     * nested structure and type instances
     */ public getCategoryTree ( ) { const options = this . options ; options . params = new HttpParams ( ) . append ( 'view' , 'tree' ) ; return this . api . callGet < CmdbCategoryTree > ( `${ this . servicePrefix }/` , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body . results as CmdbCategoryTree ; } ) ) ; } param category public postCategory ( category ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callPost < CmdbCategory > ( this . servicePrefix + '/' , category , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return null ; } return apiResponse . body . raw ; } ) ) ; } param category public updateCategory ( category ) { const options = httpObserveOptions ; options . params = new HttpParams ( ) ; return this . api . callPut < number > ( this . servicePrefix + '/' + category . public_id , category , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . result ; } ) ) ; } param publicID public deleteCategory ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callDelete < number > ( this . servicePrefix + '/' + publicID , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . raw . public_id ; } ) ) ; } }