import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { catchError , map } from 'rxjs/operators' ; import { SharedService } from '../shared/shared.service' ; import { GlobalService } from './global.service' ; import { ResponseBody } from './response-body' ; import { Staff } from './staff' ; import { StaffList } from './staff-list' ; @ Injectable ( ) export class StaffDataService { constructor ( private globalService , private http ) { } public static getStatusTypes ( ) { return [ { label : 'Active' , value : 10 } , { label : 'Waiting Confirmation' , value : 1 } , { label : 'Disabled' , value : 0 } ] ; } public static getRoleTypes ( ) { return [ { label : 'Administrator' , value : 99 } , { label : 'Staff' , value : 50 } ] ; } addStaff ( staff ) { const headers = GlobalService . getHeaders ( ) ; return this . http . post < ResponseBody > ( this . globalService . apiHost + '/staff' , JSON . stringify ( staff ) , { headers } ) . pipe ( map ( response => { return response ; } ) , catchError ( err => GlobalService . handleError ( err ) ) ) ; } deleteStaffById ( id ) { const headers = GlobalService . getHeaders ( ) ; return this . http . delete < ResponseBody > ( this . globalService . apiHost + '/staff/' + id , { headers } ) . pipe ( map ( response => { return response ; } ) , catchError ( err => GlobalService . handleError ( err ) ) ) ; } updateStaffById ( staff ) { const headers = GlobalService . getHeaders ( ) ; return this . http . put < ResponseBody > ( this . globalService . apiHost + '/staff/' + staff . id , JSON . stringify ( staff ) , { headers } ) . pipe ( map ( response => { return response ; } ) , catchError ( err => GlobalService . handleError ( err ) ) ) ; } getAllStaffs ( extendedQueries ? ) { const headers = GlobalService . getHeaders ( ) ; let queries = { } ; if ( extendedQueries ) { queries = { ... queries , ... extendedQueries } ; } return this . http . get < ResponseBody > ( this . globalService . apiHost + '/staff?' + SharedService . serializeQueryString ( queries ) , { headers } ) . pipe ( map ( response => { return new StaffList ( response . data ) ; } ) , catchError ( err => GlobalService . handleError ( err ) ) ) ; } getStaffById ( id ) { const headers = GlobalService . getHeaders ( ) ; return this . http . get < ResponseBody > ( this . globalService . apiHost + '/staff/' + id , { headers } ) . pipe ( map ( response => { return response . data as Staff ; } ) , catchError ( err => GlobalService . handleError ( err ) ) ) ; } public getPermissionTypes ( ) { const headers = GlobalService . getHeaders ( ) ; return this . http . get < ResponseBody > ( this . globalService . apiHost + '/staff/get-permissions' , { headers } ) . pipe ( map ( response => { return response . data ; } ) , catchError ( err => GlobalService . handleError ( err ) ) ) ; } }