Spaces:
Sleeping
Sleeping
| import { API_BASE_URL } from '@/config'; | |
| import { isAxiosError } from 'axios'; | |
| import apiClient from '@/lib/api/axios-instance'; | |
| import { EmployeeProjectDetail } from '../types'; | |
| const API_ENDPOINT = '/api/EmployeeProjectDetail'; | |
| // Employee Project Detail API service | |
| export const employeeProjectDetailApi = { | |
| // Get all employee project details | |
| getAll: async (): Promise<{success: boolean; data: EmployeeProjectDetail[]; message?: string}> => { | |
| try { | |
| console.log('EmployeeProjectDetail API: Fetching all employee project details'); | |
| const response = await apiClient.get(API_ENDPOINT); | |
| console.log(`EmployeeProjectDetail API: Successfully fetched ${response.data.length} project details`); | |
| return { | |
| success: true, | |
| data: response.data | |
| }; | |
| } catch (error) { | |
| console.error('EmployeeProjectDetail API: Error fetching project details:', error); | |
| if (isAxiosError(error) && error.message.includes('Network Error')) { | |
| console.error('EmployeeProjectDetail API: Possible CORS or network connectivity issue'); | |
| } | |
| return { | |
| success: false, | |
| data: [], | |
| message: 'Failed to fetch project details. Please try again.' | |
| }; | |
| } | |
| }, | |
| // Get employee project detail by id | |
| getById: async (id: number): Promise<{success: boolean; data?: EmployeeProjectDetail; message?: string}> => { | |
| try { | |
| console.log(`EmployeeProjectDetail API: Fetching project detail with ID ${id}`); | |
| const response = await apiClient.get(`${API_ENDPOINT}/${id}`); | |
| console.log(`EmployeeProjectDetail API: Successfully fetched project detail with ID ${id}`); | |
| return { | |
| success: true, | |
| data: response.data | |
| }; | |
| } catch (error) { | |
| console.error(`EmployeeProjectDetail API: Error fetching project detail with ID ${id}:`, error); | |
| const errorMessage = isAxiosError(error) | |
| ? `Failed to fetch project detail: ${error.response?.status} ${error.message}` | |
| : `Error fetching project detail: ${error instanceof Error ? error.message : 'Unknown error'}`; | |
| return { | |
| success: false, | |
| message: errorMessage | |
| }; | |
| } | |
| }, | |
| // Get employee project details by employee id | |
| getByEmployeeId: async (employeeId: number): Promise<{success: boolean; data: EmployeeProjectDetail[]; message?: string}> => { | |
| try { | |
| console.log(`EmployeeProjectDetail API: Fetching project details for employee ID ${employeeId}`); | |
| const response = await apiClient.get(`${API_ENDPOINT}/by-employee/${employeeId}`); | |
| console.log(`EmployeeProjectDetail API: Successfully fetched ${response.data.length} project details for employee ID ${employeeId}`); | |
| return { | |
| success: true, | |
| data: response.data | |
| }; | |
| } catch (error) { | |
| console.error(`EmployeeProjectDetail API: Error fetching project details for employee ID ${employeeId}:`, error); | |
| if (isAxiosError(error) && error.message.includes('Network Error')) { | |
| console.error('EmployeeProjectDetail API: Possible CORS or network connectivity issue'); | |
| } | |
| return { | |
| success: false, | |
| data: [], | |
| message: 'Failed to fetch employee project details. Please try again.' | |
| }; | |
| } | |
| }, | |
| // Create a new employee project detail | |
| create: async (data: Partial<EmployeeProjectDetail>): Promise<{success: boolean; data?: EmployeeProjectDetail; message?: string}> => { | |
| try { | |
| console.log('EmployeeProjectDetail API: Creating new project detail', data); | |
| const response = await apiClient.post(API_ENDPOINT, data); | |
| console.log('EmployeeProjectDetail API: Successfully created project detail', response.data); | |
| return { | |
| success: true, | |
| data: response.data, | |
| message: 'Project detail created successfully' | |
| }; | |
| } catch (error) { | |
| console.error('EmployeeProjectDetail API: Error creating project detail:', error); | |
| const errorMessage = isAxiosError(error) | |
| ? `Failed to create project detail: ${error.response?.status} ${error.message}` | |
| : `Error creating project detail: ${error instanceof Error ? error.message : 'Unknown error'}`; | |
| return { | |
| success: false, | |
| message: errorMessage | |
| }; | |
| } | |
| }, | |
| // Update an existing employee project detail | |
| update: async (id: number, data: Partial<EmployeeProjectDetail>): Promise<{success: boolean; data?: EmployeeProjectDetail; message?: string}> => { | |
| try { | |
| console.log(`EmployeeProjectDetail API: Updating project detail with ID ${id}`, data); | |
| const response = await apiClient.put(`${API_ENDPOINT}/${id}`, data); | |
| console.log(`EmployeeProjectDetail API: Successfully updated project detail with ID ${id}`, response.data); | |
| return { | |
| success: true, | |
| data: response.data, | |
| message: 'Project detail updated successfully' | |
| }; | |
| } catch (error) { | |
| console.error(`EmployeeProjectDetail API: Error updating project detail with ID ${id}:`, error); | |
| const errorMessage = isAxiosError(error) | |
| ? `Failed to update project detail: ${error.response?.status} ${error.message}` | |
| : `Error updating project detail: ${error instanceof Error ? error.message : 'Unknown error'}`; | |
| return { | |
| success: false, | |
| message: errorMessage | |
| }; | |
| } | |
| }, | |
| // Delete an employee project detail | |
| delete: async (id: number): Promise<{success: boolean; message?: string}> => { | |
| try { | |
| console.log(`EmployeeProjectDetail API: Deleting project detail with ID ${id}`); | |
| await apiClient.delete(`${API_ENDPOINT}/${id}`); | |
| console.log(`EmployeeProjectDetail API: Successfully deleted project detail with ID ${id}`); | |
| return { | |
| success: true, | |
| message: 'Project detail deleted successfully' | |
| }; | |
| } catch (error) { | |
| console.error(`EmployeeProjectDetail API: Error deleting project detail with ID ${id}:`, error); | |
| const errorMessage = isAxiosError(error) | |
| ? `Failed to delete project detail: ${error.response?.status} ${error.message}` | |
| : `Error deleting project detail: ${error instanceof Error ? error.message : 'Unknown error'}`; | |
| return { | |
| success: false, | |
| message: errorMessage | |
| }; | |
| } | |
| } | |
| }; | |
| // Export for backward compatibility | |
| export const getEmployeeProjectDetails = async (): Promise<EmployeeProjectDetail[]> => { | |
| const result = await employeeProjectDetailApi.getAll(); | |
| return result.data; | |
| }; | |
| export const getEmployeeProjectDetailById = async (id: number): Promise<EmployeeProjectDetail> => { | |
| const result = await employeeProjectDetailApi.getById(id); | |
| if (!result.success || !result.data) { | |
| throw new Error(result.message || 'Failed to get employee project detail'); | |
| } | |
| return result.data; | |
| }; | |
| export const getEmployeeProjectDetailsByEmployeeId = async (employeeId: number): Promise<EmployeeProjectDetail[]> => { | |
| const result = await employeeProjectDetailApi.getByEmployeeId(employeeId); | |
| return result.data; | |
| }; | |
| export const createEmployeeProjectDetail = async (data: Partial<EmployeeProjectDetail>): Promise<EmployeeProjectDetail> => { | |
| const result = await employeeProjectDetailApi.create(data); | |
| if (!result.success || !result.data) { | |
| throw new Error(result.message || 'Failed to create employee project detail'); | |
| } | |
| return result.data; | |
| }; | |
| export const updateEmployeeProjectDetail = async (id: number, data: Partial<EmployeeProjectDetail>): Promise<EmployeeProjectDetail> => { | |
| const result = await employeeProjectDetailApi.update(id, data); | |
| if (!result.success) { | |
| throw new Error(result.message || 'Failed to update employee project detail'); | |
| } | |
| return result.data as EmployeeProjectDetail; | |
| }; | |
| export const deleteEmployeeProjectDetail = async (id: number): Promise<void> => { | |
| const result = await employeeProjectDetailApi.delete(id); | |
| if (!result.success) { | |
| throw new Error(result.message || 'Failed to delete employee project detail'); | |
| } | |
| }; |