Added error handling and now possible to post employees
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ErrorType } from '@dafa-enums/error-type.enum';
|
||||
import { environment } from '@dafa-environment';
|
||||
import { EmployeesApiResponse } from '@dafa-models/api/employee-response.model';
|
||||
import { Employee, EmployeeDetail, mapEmployeeReponseToEmployee } from '@dafa-models/employee.model';
|
||||
import {
|
||||
Employee,
|
||||
EmployeeApiPostResponse,
|
||||
EmployeesApiResponse,
|
||||
mapEmployeeReponseToEmployee,
|
||||
mapEmployeeToEmployeeApiRequestData,
|
||||
} from '@dafa-models/employee.model';
|
||||
import { SortBy } from '@dafa-models/sort-by.model';
|
||||
import { sort } from '@dafa-utils/sort.util';
|
||||
import { BehaviorSubject, combineLatest, Observable, throwError } from 'rxjs';
|
||||
@@ -64,12 +70,21 @@ export class EmployeeService {
|
||||
this._employeesSortBy$.next({ key, reverse });
|
||||
}
|
||||
|
||||
public createAccount(employeesData: EmployeeDetail): Observable<string> {
|
||||
return this.httpClient.post<EmployeeDetail>(this._employeesApiUrl, employeesData).pipe(
|
||||
map(data => data.id),
|
||||
catchError(error => {
|
||||
return throwError(error);
|
||||
})
|
||||
);
|
||||
public postNewEmployee(employeeData: Employee): Observable<string> {
|
||||
return this.httpClient
|
||||
.post<EmployeeApiPostResponse>(
|
||||
this._employeeApiUrl,
|
||||
mapEmployeeToEmployeeApiRequestData(employeeData),
|
||||
API_HEADERS
|
||||
)
|
||||
.pipe(
|
||||
map(data => {
|
||||
if (data.pyHasError === 'true') {
|
||||
throw new Error(data.pyErrorMessage);
|
||||
}
|
||||
return data.pyUserIdentifier;
|
||||
}),
|
||||
catchError(error => throwError({ message: error, type: ErrorType.API }))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user