import { Injectable } from '@angular/core' ; import { ApiCallService , ApiService , httpObserveOptions , resp } from '../../services/api-call.service' ; import { User } from '../models/user' ; import { AuthService } from '../../auth/services/auth.service' ; import { switchMap , map , catchError } from 'rxjs/operators' ; import { Observable , timer } from 'rxjs' ; import { HttpBackend , HttpHeaders , HttpParams , HttpResponse } from '@angular/common/http' ; import { FormControl } from '@angular/forms' ; import { CollectionParameters } from '../../services/models/api-parameter' ; import { APIDeleteSingleResponse , APIGetMultiResponse , APIGetSingleResponse , APIInsertSingleResponse , APIUpdateSingleResponse } from '../../services/models/api-response' ; export const userExistsValidator = ( userService , time = 500 ) => { return ( control ) => { return timer ( time ) . pipe ( switchMap ( ( ) => { return userService . getUserByName ( control . value ) . pipe ( map ( ( response ) => { if ( response === null ) { return null ; } else { return { userExists : true } ; } } ) , catchError ( ( ) => { return new Promise ( resolve => { resolve ( null ) ; } ) ; } ) ) ; } ) ) ; } ; } ; @ Injectable ( { providedIn : 'root' } ) export class UserService < T = User > implements ApiService { public readonly servicePrefix : string = 'users' ; public readonly options = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , params : { } , observe : resp } ; constructor ( private api , private backend , private authService ) { } /**
     * Get the current login user.
     */ public getCurrentUser ( ) { return this . authService . currentUserValue ; } param params public getUsers ( 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 < Array < T > > ( this . servicePrefix + '/' , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } param publicID public getUser ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callGet < T > ( this . servicePrefix + '/' + publicID , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . result as T ; } ) ) ; } /**
     * Get the number of all users.
     */ public countUsers ( filter ? ) { const options = this . options ; let httpParams = new HttpParams ( ) ; if ( filter !== undefined ) { httpParams = httpParams . set ( 'filter' , JSON . stringify ( filter ) ) ; } options . params = httpParams ; return this . api . callHead < T > ( `${ this . servicePrefix }/` , options ) . pipe ( map ( ( apiResponse ) => { return + apiResponse . headers . get ( 'X-Total-Count' ) ; } ) ) ; } param name public getUserByName ( name ) { const options = this . options ; const filter = { user_name : name } ; let params = new HttpParams ( ) ; params = params . set ( 'filter' , JSON . stringify ( filter ) ) ; options . params = params ; return this . api . callGet < Array < T > > ( this . servicePrefix + '/' , options ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . body . count === 0 ) { return null ; } return apiResponse . body . results [ 0 ] as T ; } ) ) ; } param userID param newPassword public changeUserPassword ( userID , newPassword ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callPatch < T > ( `${ this . servicePrefix }/${ userID }/password` , { password : newPassword } , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . result as T ; } ) ) ; } param groupID public getUsersByGroup ( groupID ) { const options = this . options ; let params = new HttpParams ( ) ; params = params . set ( 'filter' , JSON . stringify ( { group_id : groupID } ) ) ; options . params = params ; return this . api . callGet < Array < T > > ( `${ this . servicePrefix }/` , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . results as Array < T > ; } ) ) ; } param user public postUser ( user ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callPost < User > ( `${ this . servicePrefix }/` , user , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . raw as T ; } ) ) ; } param publicID param user public putUser ( publicID , user ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callPut < T > ( `${ this . servicePrefix }/${ publicID }` , user , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . result as T ; } ) ) ; } param publicID public deleteUser ( publicID ) { const options = this . options ; options . params = new HttpParams ( ) ; return this . api . callDelete < User > ( `${ this . servicePrefix }/${ publicID }` , options ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body . raw as User ; } ) ) ; } }