Spaces:
Sleeping
Sleeping
| // Employee interface | |
| import { API_BASE_URL } from '@/config'; | |
| import axios, { isAxiosError } from 'axios'; | |
| import { createHeaders } from '@/lib/api'; | |
| import apiClient from '@/lib/api/axios-instance'; | |
| export interface Employee { | |
| id: number; | |
| empCode: string; | |
| firstName: string; | |
| middleName: string; | |
| lastName: string; | |
| email: string; | |
| mobile: string; | |
| type: string; | |
| academics: string; | |
| financialData: string; | |
| description: string; | |
| userId: number; | |
| passwordHash: string; | |
| roleId: number; | |
| avatarUrl: string; | |
| status: string; | |
| reportingToId?: number | null; | |
| } | |
| // Employee API service | |
| export const employeeApi = { | |
| getAll: async (): Promise<Employee[]> => { | |
| try { | |
| console.log('Employee API: Fetching all employees from', `${API_BASE_URL}/api/employee`); | |
| const response = await apiClient.get(`/api/employee`); | |
| console.log(`Employee API: Successfully fetched ${response.data.length} employees`); | |
| return response.data; | |
| } catch (error) { | |
| console.error('Employee API: Error fetching employees:', error); | |
| if (isAxiosError(error) && error.message.includes('Network Error')) { | |
| console.error('Employee API: Possible CORS or network connectivity issue'); | |
| } | |
| return []; // Return empty array on error for graceful degradation | |
| } | |
| }, | |
| getAllForAdmin: async (): Promise<Employee[]> => { | |
| try { | |
| console.log('Employee API: Fetching all employees from', `${API_BASE_URL}/api/employee`); | |
| const response = await apiClient.get(`/api/employee/GetAllForAdmin`); | |
| console.log('Raw API response for employees:', response.data); | |
| console.log(`Employee API: Successfully fetched ${response.data.length} employees`); | |
| return response.data; | |
| } catch (error) { | |
| console.error('Employee API: Error fetching employees:', error); | |
| if (isAxiosError(error) && error.message.includes('Network Error')) { | |
| console.error('Employee API: Possible CORS or network connectivity issue'); | |
| } | |
| return []; // Return empty array on error for graceful degradation | |
| } | |
| }, | |
| getById: async (id: number): Promise<{success: boolean; data?: Employee; message?: string}> => { | |
| try { | |
| console.log(`Employee API: Fetching employee with ID ${id} from`, `${API_BASE_URL}/api/employee/${id}`); | |
| const response = await apiClient.get(`/api/employee/${id}`); | |
| console.log(`Employee API: Successfully fetched employee with ID ${id}`); | |
| console.log('Employee data structure:', JSON.stringify(response.data, null, 2)); | |
| return { | |
| success: true, | |
| data: response.data | |
| }; | |
| } catch (error) { | |
| console.error(`Employee API: Error fetching employee with ID ${id}:`, error); | |
| const errorMessage = isAxiosError(error) | |
| ? `Failed to fetch employee: ${error.response?.status} ${error.message}` | |
| : `Error fetching employee: ${error instanceof Error ? error.message : 'Unknown error'}`; | |
| return { | |
| success: false, | |
| message: errorMessage | |
| }; | |
| } | |
| }, | |
| getByRoleId: async (roleId: number): Promise<{success: boolean; data: Employee[]; message?: string}> => { | |
| try { | |
| console.log(`Employee API: Fetching employees with role ID ${roleId} from`, `${API_BASE_URL}/api/employee/byrole/${roleId}`); | |
| const response = await apiClient.get(`/api/employee/byrole/${roleId}`); | |
| console.log(`Employee API: Successfully fetched ${response.data.length} employees with role ID ${roleId}`); | |
| return { | |
| success: true, | |
| data: response.data | |
| }; | |
| } catch (error) { | |
| console.error(`Employee API: Error fetching employees with role ID ${roleId}:`, error); | |
| const errorMessage = isAxiosError(error) | |
| ? `Failed to fetch employees by role: ${error.response?.status} ${error.message}` | |
| : `Error fetching employees by role: ${error instanceof Error ? error.message : 'Unknown error'}`; | |
| return { | |
| success: false, | |
| data: [], | |
| message: errorMessage | |
| }; | |
| } | |
| }, | |
| getByManagerId: async (managerId: number): Promise<Employee[]> => { | |
| try { | |
| console.log(`Employee API: Fetching employees for manager ID ${managerId} from`, `${API_BASE_URL}/api/employee/GetEmployeesByManager/${managerId}`); | |
| const response = await apiClient.get(`/api/employee/GetEmployeesByManager/${managerId}`); | |
| console.log(`Employee API: Successfully fetched ${response.data.length} employees for manager ID ${managerId}`); | |
| return response.data; | |
| } catch (error) { | |
| console.error(`Employee API: Error fetching employees for manager ID ${managerId}:`, error); | |
| return []; // Return empty array on error for graceful degradation | |
| } | |
| }, | |
| // Update employee status | |
| updateStatus: async (id: number, status: string): Promise<{success: boolean; message?: string}> => { | |
| try { | |
| console.log(`Employee API: Updating status for employee ID ${id} to "${status}"`); | |
| const response = await apiClient.patch(`/api/employee/${id}/status`, { Status: status }); | |
| console.log(`Employee API: Successfully updated status for employee ID ${id}`); | |
| return { | |
| success: true, | |
| message: "Status updated successfully" | |
| }; | |
| } catch (error) { | |
| console.error(`Employee API: Error updating status for employee ID ${id}:`, error); | |
| // Return a formatted error message for better user experience | |
| return { | |
| success: false, | |
| message: "Failed to update employee status. Please try again." | |
| }; | |
| } | |
| }, | |
| // Update employee reporting | |
| async updateEmployeeReporting(id: number, reportingToId: number | null): Promise<{ success: boolean; message?: string }> { | |
| try { | |
| const response = await apiClient.put(`/api/employee/${id}/reporting`, { reportingToId }); | |
| return { success: true }; | |
| } catch (error) { | |
| console.error('Error updating employee reporting:', error); | |
| return { success: false, message: error instanceof Error ? error.message : 'Unknown error' }; | |
| } | |
| } | |
| }; | |
| // Export the getEmployeeById function for backward compatibility | |
| export const getEmployeeById = employeeApi.getById; | |
| // Export the getEmployeesByRole function | |
| export const getEmployeesByRole = employeeApi.getByRoleId; | |