fix salary field
This commit is contained in:
@@ -1,5 +1,38 @@
|
||||
import { api } from '../api'
|
||||
|
||||
const toNumber = (v: any): number => {
|
||||
if (v === null || v === undefined || v === '') return 0
|
||||
if (typeof v === 'number') return Number.isFinite(v) ? v : 0
|
||||
const s = typeof v === 'string' ? v : (v?.toString?.() ?? String(v))
|
||||
const cleaned = s.replace(/[^0-9.-]/g, '')
|
||||
const n = Number(cleaned)
|
||||
return Number.isFinite(n) ? n : 0
|
||||
}
|
||||
|
||||
const normalizeEmployeeFromApi = (e: any) => {
|
||||
const basic = toNumber(e?.basicSalary ?? e?.baseSalary ?? e?.salary)
|
||||
return {
|
||||
...e,
|
||||
baseSalary: basic,
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeEmployeeToApi = (data: any) => {
|
||||
const d = { ...(data || {}) }
|
||||
|
||||
if (d.baseSalary !== undefined) {
|
||||
d.basicSalary = toNumber(d.baseSalary)
|
||||
delete d.baseSalary
|
||||
}
|
||||
|
||||
if (d.salary !== undefined) {
|
||||
d.basicSalary = toNumber(d.salary)
|
||||
delete d.salary
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
export interface Employee {
|
||||
id: string
|
||||
uniqueEmployeeId: string
|
||||
@@ -25,7 +58,12 @@ export interface Employee {
|
||||
position?: any
|
||||
reportingToId?: string
|
||||
reportingTo?: any
|
||||
|
||||
baseSalary: number
|
||||
|
||||
basicSalary?: any
|
||||
|
||||
currency?: string
|
||||
status: string
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
@@ -49,6 +87,7 @@ export interface CreateEmployeeData {
|
||||
departmentId: string
|
||||
positionId: string
|
||||
reportingToId?: string
|
||||
|
||||
baseSalary: number
|
||||
}
|
||||
|
||||
@@ -84,8 +123,11 @@ export const employeesAPI = {
|
||||
|
||||
const response = await api.get(`/hr/employees?${params.toString()}`)
|
||||
const { data, pagination } = response.data
|
||||
|
||||
const normalized = Array.isArray(data) ? data.map(normalizeEmployeeFromApi) : []
|
||||
|
||||
return {
|
||||
employees: data || [],
|
||||
employees: normalized,
|
||||
total: pagination?.total || 0,
|
||||
page: pagination?.page || 1,
|
||||
pageSize: pagination?.pageSize || 20,
|
||||
@@ -96,19 +138,21 @@ export const employeesAPI = {
|
||||
// Get single employee by ID
|
||||
getById: async (id: string): Promise<Employee> => {
|
||||
const response = await api.get(`/hr/employees/${id}`)
|
||||
return response.data.data
|
||||
return normalizeEmployeeFromApi(response.data.data)
|
||||
},
|
||||
|
||||
// Create new employee
|
||||
create: async (data: CreateEmployeeData): Promise<Employee> => {
|
||||
const response = await api.post('/hr/employees', data)
|
||||
return response.data.data
|
||||
const payload = normalizeEmployeeToApi(data)
|
||||
const response = await api.post('/hr/employees', payload)
|
||||
return normalizeEmployeeFromApi(response.data.data)
|
||||
},
|
||||
|
||||
// Update existing employee
|
||||
update: async (id: string, data: UpdateEmployeeData): Promise<Employee> => {
|
||||
const response = await api.put(`/hr/employees/${id}`, data)
|
||||
return response.data.data
|
||||
const payload = normalizeEmployeeToApi(data)
|
||||
const response = await api.put(`/hr/employees/${id}`, payload)
|
||||
return normalizeEmployeeFromApi(response.data.data)
|
||||
},
|
||||
|
||||
// Delete employee
|
||||
|
||||
Reference in New Issue
Block a user