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