Spaces:
Sleeping
Sleeping
| import axios from 'axios'; | |
| import apiClient from '@/lib/api/axios-instance'; | |
| import { createHeaders } from '@/lib/api'; | |
| import { API_BASE_URL } from '../config'; | |
| import { | |
| AppraisalCycle, | |
| AppraisalCycleCreateRequest, | |
| AppraisalCycleUpdateRequest, | |
| Appraisal, | |
| AppraisalCreateRequest, | |
| AppraisalUpdateRequest, | |
| AssessmentArea, | |
| AssessmentAreaCreateRequest, | |
| AssessmentAreaUpdateRequest, | |
| RoleAssessmentQuestion, | |
| RoleAssessmentQuestionCreateRequest, | |
| RoleAssessmentQuestionUpdateRequest, | |
| AssessmentAnswer, | |
| AssessmentAnswerCreateRequest, | |
| AssessmentAnswerUpdateRequest, | |
| Competency, | |
| CompetencyCreateRequest, | |
| CompetencyUpdateRequest, | |
| CompetencyIndicator, | |
| CompetencyIndicatorCreateRequest, | |
| CompetencyIndicatorUpdateRequest, | |
| CompetencyRating, | |
| CompetencyRatingCreateRequest, | |
| CompetencyRatingUpdateRequest, | |
| TrainingNeed, | |
| TrainingNeedCreateRequest, | |
| TrainingNeedUpdateRequest, | |
| HrRemark, | |
| HrRemarkCreateRequest, | |
| HrRemarkUpdateRequest, | |
| ResponseData, | |
| ManagementRemark, | |
| ManagementRemarkCreateRequest, | |
| ManagementRemarkUpdateRequest, | |
| } from '../types'; | |
| // This should match other API calls in the codebase | |
| const BASE_URL = `${API_BASE_URL}/api`; | |
| // ===================================================================== | |
| // Appraisal Cycles | |
| // ===================================================================== | |
| export const getAppraisalCycles = async (): Promise<ResponseData<AppraisalCycle[]>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AppraisalCycle`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AppraisalCycle[]>; | |
| } | |
| // If response.data is an array, assume it's the cycles data | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as AppraisalCycle[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAppraisalCycles:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getAppraisalCycleById = async (cycleId: number): Promise<ResponseData<AppraisalCycle>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AppraisalCycle/${cycleId}`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Similar error handling as in getAppraisalCycles | |
| if (!response.data) { | |
| return { | |
| data: {} as AppraisalCycle, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AppraisalCycle>; | |
| } | |
| // Assume response.data is the cycle object | |
| return { | |
| data: response.data as AppraisalCycle, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAppraisalCycleById:", error); | |
| return { | |
| data: {} as AppraisalCycle, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const createAppraisalCycle = async (cycle: AppraisalCycleCreateRequest): Promise<ResponseData<AppraisalCycle>> => { | |
| const response = await apiClient.post(`/api/AppraisalCycle`, cycle); | |
| return response.data; | |
| }; | |
| export const updateAppraisalCycle = async (cycle: AppraisalCycleUpdateRequest): Promise<ResponseData<AppraisalCycle>> => { | |
| const response = await apiClient.put(`/api/AppraisalCycle/${cycle.cycleId}`, cycle); | |
| return response.data; | |
| }; | |
| export const deleteAppraisalCycle = async (cycleId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/AppraisalCycle/${cycleId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Appraisals | |
| // ===================================================================== | |
| export const getAppraisals = async (): Promise<ResponseData<Appraisal[]>> => { | |
| const response = await apiClient.get(`/api/Appraisal`); | |
| return response.data; | |
| }; | |
| export const getAppraisalById = async (appraisalId: number): Promise<ResponseData<Appraisal>> => { | |
| const response = await apiClient.get(`/api/Appraisal/${appraisalId}`); | |
| return response.data; | |
| }; | |
| export const getAppraisalsByEmployeeId = async (employeeId: number): Promise<ResponseData<Appraisal[]>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Appraisal/employee/${employeeId}`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<Appraisal[]>; | |
| } | |
| // If response.data is an array, assume it's the appraisals data | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as Appraisal[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAppraisalsByEmployeeId:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getAppraisalsByAppraiserId = async (appraiserId: number): Promise<ResponseData<Appraisal[]>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Appraisal/appraiser/${appraiserId}`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<Appraisal[]>; | |
| } | |
| // If response.data is an array, assume it's the appraisals data | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as Appraisal[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAppraisalsByAppraiserId:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getAppraisalsByCycleId = async (cycleId: number): Promise<ResponseData<Appraisal[]>> => { | |
| const appraisals = await getAppraisals(); | |
| // Handle case when appraisals.data is undefined | |
| if (!appraisals.success || !appraisals.data) { | |
| console.warn("No appraisals data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredAppraisals = appraisals.data.filter(appraisal => appraisal.cycleId === cycleId); | |
| return { | |
| data: filteredAppraisals, | |
| success: true | |
| }; | |
| }; | |
| export const createAppraisal = async (appraisal: AppraisalCreateRequest): Promise<ResponseData<Appraisal>> => { | |
| const response = await apiClient.post(`/api/Appraisal`, appraisal); | |
| return response.data; | |
| }; | |
| export const updateAppraisal = async (appraisal: AppraisalUpdateRequest): Promise<ResponseData<Appraisal>> => { | |
| const response = await apiClient.put(`/api/Appraisal/${appraisal.appraisalId}`, appraisal); | |
| return response.data; | |
| }; | |
| export const deleteAppraisal = async (appraisalId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/Appraisal/${appraisalId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Role Assessment Questions | |
| // ===================================================================== | |
| export const getRoleAssessmentQuestions = async (): Promise<ResponseData<RoleAssessmentQuestion[]>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/RoleAssessmentQuestion`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<RoleAssessmentQuestion[]>; | |
| } | |
| // If response.data is an array, assume it's the questions data | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as RoleAssessmentQuestion[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getRoleAssessmentQuestions:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getRoleAssessmentQuestionsByRoleId = async (roleId: number): Promise<ResponseData<RoleAssessmentQuestion[]>> => { | |
| const questions = await getRoleAssessmentQuestions(); | |
| // Handle case when questions.data is undefined | |
| if (!questions.success || !questions.data) { | |
| console.warn("No role assessment questions data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredQuestions = questions.data.filter(question => question.roleId === roleId); | |
| return { | |
| data: filteredQuestions, | |
| success: true | |
| }; | |
| }; | |
| export const getRoleAssessmentQuestionById = async (questionId: number): Promise<ResponseData<RoleAssessmentQuestion>> => { | |
| const response = await apiClient.get(`/api/RoleAssessmentQuestion/${questionId}`); | |
| return response.data; | |
| }; | |
| export const createRoleAssessmentQuestion = async (questionData: RoleAssessmentQuestionCreateRequest): Promise<ResponseData<RoleAssessmentQuestion>> => { | |
| const response = await apiClient.post(`/api/RoleAssessmentQuestion`, questionData); | |
| return response.data; | |
| }; | |
| export const updateRoleAssessmentQuestion = async (questionData: RoleAssessmentQuestionUpdateRequest): Promise<ResponseData<RoleAssessmentQuestion>> => { | |
| const response = await apiClient.put(`/api/RoleAssessmentQuestion/${questionData.questionId}`, questionData); | |
| return response.data; | |
| }; | |
| export const deleteRoleAssessmentQuestion = async (questionId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/RoleAssessmentQuestion/${questionId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Competencies | |
| // ===================================================================== | |
| export const getCompetencies = async (): Promise<ResponseData<Competency[]>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Competency`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<Competency[]>; | |
| } | |
| // If response.data is an array, assume it's the competencies data | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as Competency[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getCompetencies:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getCompetencyById = async (competencyId: number): Promise<ResponseData<Competency>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Competency/${competencyId}`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| return { | |
| data: {} as Competency, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<Competency>; | |
| } | |
| // Assume response.data is the competency object | |
| return { | |
| data: response.data as Competency, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in getCompetencyById:", error); | |
| return { | |
| data: {} as Competency, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const createCompetency = async (competency: CompetencyCreateRequest): Promise<ResponseData<Competency>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Competency`; | |
| console.log("Calling API: POST", apiUrl, competency); | |
| const response = await apiClient.post<any>(apiUrl, competency); | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<Competency>; | |
| } | |
| return { | |
| data: response.data as Competency, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in createCompetency:", error); | |
| return { | |
| data: {} as Competency, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const updateCompetency = async (competency: CompetencyUpdateRequest): Promise<ResponseData<Competency>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Competency/${competency.competencyId}`; | |
| console.log("Calling API: PUT", apiUrl, competency); | |
| const response = await apiClient.put<any>(apiUrl, competency); | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<Competency>; | |
| } | |
| return { | |
| data: response.data as Competency, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in updateCompetency:", error); | |
| return { | |
| data: {} as Competency, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const deleteCompetency = async (competencyId: number): Promise<ResponseData<boolean>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Competency/${competencyId}`; | |
| console.log("Calling API: DELETE", apiUrl); | |
| const response = await apiClient.delete(apiUrl); | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<boolean>; | |
| } | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| } catch (error) { | |
| console.error("Error in deleteCompetency:", error); | |
| return { | |
| data: false, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| // ===================================================================== | |
| // Competency Indicators | |
| // ===================================================================== | |
| export const getCompetencyIndicators = async (): Promise<ResponseData<CompetencyIndicator[]>> => { | |
| try { | |
| console.log("Fetching all competency indicators"); | |
| const apiUrl = `${BASE_URL}/CompetencyIndicator`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data for competency indicators"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data for competency indicators" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| console.log(`Received ${response.data.data?.length || 0} competency indicators with success=${response.data.success}`); | |
| return response.data as ResponseData<CompetencyIndicator[]>; | |
| } | |
| // If response.data is an array, assume it's the indicators data | |
| if (Array.isArray(response.data)) { | |
| console.log(`Received ${response.data.length} competency indicators as direct array`); | |
| return { | |
| data: response.data as CompetencyIndicator[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format for competency indicators:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format for competency indicators" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getCompetencyIndicators:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getCompetencyIndicatorsByCompetencyId = async (competencyId: number): Promise<ResponseData<CompetencyIndicator[]>> => { | |
| try { | |
| console.log(`Fetching competency indicators for competencyId: ${competencyId}`); | |
| // First try to fetch using direct API endpoint if available | |
| try { | |
| const apiUrl = `${BASE_URL}/CompetencyIndicator/by-competency/${competencyId}`; | |
| console.log("Attempting to call filtered API endpoint:", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| if (response.data) { | |
| if (typeof response.data.success !== 'undefined') { | |
| console.log(`Received ${response.data.data?.length || 0} competency indicators via filtered endpoint`); | |
| return response.data as ResponseData<CompetencyIndicator[]>; | |
| } | |
| if (Array.isArray(response.data)) { | |
| console.log(`Received ${response.data.length} competency indicators via filtered endpoint as direct array`); | |
| return { | |
| data: response.data as CompetencyIndicator[], | |
| success: true | |
| }; | |
| } | |
| } | |
| } catch (error) { | |
| console.log("Filtered endpoint failed, falling back to filtering client-side", error); | |
| } | |
| // Fall back to the original approach | |
| const indicators = await getCompetencyIndicators(); | |
| // Handle case when indicators.data is undefined | |
| if (!indicators.success || !indicators.data) { | |
| console.warn("No competency indicators data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredIndicators = indicators.data.filter(indicator => indicator.competencyId === competencyId); | |
| console.log(`Filtered ${indicators.data.length} indicators to ${filteredIndicators.length} for competency ${competencyId}`); | |
| return { | |
| data: filteredIndicators, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error(`Error fetching indicators for competency ${competencyId}:`, error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getCompetencyIndicatorById = async (indicatorId: number): Promise<ResponseData<CompetencyIndicator>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/CompetencyIndicator/${indicatorId}`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data for competency indicator"); | |
| return { | |
| data: {} as CompetencyIndicator, | |
| success: false, | |
| message: "API response is missing data for competency indicator" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<CompetencyIndicator>; | |
| } | |
| // Assume response.data is the indicator object | |
| return { | |
| data: response.data as CompetencyIndicator, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error(`Error in getCompetencyIndicatorById (${indicatorId}):`, error); | |
| return { | |
| data: {} as CompetencyIndicator, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const createCompetencyIndicator = async (indicator: CompetencyIndicatorCreateRequest): Promise<ResponseData<CompetencyIndicator>> => { | |
| try { | |
| console.log("Creating competency indicator:", indicator); | |
| const apiUrl = `/api/CompetencyIndicator`; | |
| console.log("Calling API: POST", apiUrl); | |
| const response = await apiClient.post<any>(apiUrl, indicator); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: {} as CompetencyIndicator, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<CompetencyIndicator>; | |
| } | |
| // If response.data is the indicator object, wrap it in ResponseData | |
| if (response.data.indicatorId) { | |
| return { | |
| data: response.data as CompetencyIndicator, | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: {} as CompetencyIndicator, | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in createCompetencyIndicator:", error); | |
| return { | |
| data: {} as CompetencyIndicator, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const updateCompetencyIndicator = async (indicator: CompetencyIndicatorUpdateRequest): Promise<ResponseData<CompetencyIndicator>> => { | |
| const response = await apiClient.put(`/api/CompetencyIndicator/${indicator.indicatorId}`, indicator); | |
| return response.data; | |
| }; | |
| export const deleteCompetencyIndicator = async (indicatorId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/CompetencyIndicator/${indicatorId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Assessment Answers - FIXED | |
| // ===================================================================== | |
| export const getAssessmentAnswers = async (): Promise<ResponseData<AssessmentAnswer[]>> => { | |
| const response = await apiClient.get(`/api/AssessmentAnswer`); | |
| return response.data; | |
| }; | |
| export const getAssessmentAnswersByAppraisalId = async (appraisalId: number): Promise<ResponseData<AssessmentAnswer[]>> => { | |
| try { | |
| // Use the correct endpoint path as specified in API documentation | |
| const apiUrl = `/api/AssessmentAnswer/by-appraisal/${appraisalId}`; | |
| console.log("Calling API for assessment answers by appraisal ID:", apiUrl); | |
| try { | |
| // First attempt: Try to use the correct filtered endpoint | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property and the expected format | |
| if (response.data) { | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AssessmentAnswer[]>; | |
| } | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as AssessmentAnswer[], | |
| success: true | |
| }; | |
| } | |
| } | |
| } catch (error) { | |
| console.log("Filtered endpoint failed, falling back to all answers", error); | |
| // If filtered endpoint fails, fall back to getting all answers | |
| } | |
| // Fall back to getting all answers and filtering client-side | |
| const answers = await getAssessmentAnswers(); | |
| // Handle case when answers.data is undefined | |
| if (!answers.success || !answers.data) { | |
| console.warn("No assessment answers data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredAnswers = answers.data.filter(answer => answer.appraisalId === appraisalId); | |
| console.log(`Filtered ${answers.data.length} answers to ${filteredAnswers.length} for appraisal ${appraisalId}`); | |
| return { | |
| data: filteredAnswers, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error getting assessment answers by appraisal ID:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getAssessmentAnswerById = async (answerId: number): Promise<ResponseData<AssessmentAnswer>> => { | |
| const response = await apiClient.get(`/api/AssessmentAnswer/${answerId}`); | |
| return response.data; | |
| }; | |
| export const createAssessmentAnswer = async (answer: AssessmentAnswerCreateRequest): Promise<ResponseData<AssessmentAnswer>> => { | |
| try { | |
| console.log("Creating assessment answer:", answer); | |
| const apiUrl = `/api/AssessmentAnswer`; | |
| console.log("Calling API: POST", apiUrl); | |
| // Add default timestamps if not provided | |
| const payload = { | |
| ...answer, | |
| createdAt: answer.createdAt || new Date().toISOString(), | |
| updatedAt: answer.updatedAt || new Date().toISOString() | |
| }; | |
| const response = await apiClient.post<any>(apiUrl, payload); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: {} as AssessmentAnswer, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AssessmentAnswer>; | |
| } | |
| // If response.data is the answer object, wrap it in ResponseData | |
| if (response.data.answerId) { | |
| return { | |
| data: response.data as AssessmentAnswer, | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: {} as AssessmentAnswer, | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in createAssessmentAnswer:", error); | |
| return { | |
| data: {} as AssessmentAnswer, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const updateAssessmentAnswer = async (answer: AssessmentAnswerUpdateRequest): Promise<ResponseData<AssessmentAnswer>> => { | |
| const response = await apiClient.put(`/api/AssessmentAnswer/${answer.answerId}`, answer); | |
| return response.data; | |
| }; | |
| export const deleteAssessmentAnswer = async (answerId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/AssessmentAnswer/${answerId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Competency Ratings - FIXED | |
| // ===================================================================== | |
| export const getCompetencyRatings = async (): Promise<ResponseData<CompetencyRating[]>> => { | |
| const response = await apiClient.get(`/api/CompetencyRating`); | |
| return response.data; | |
| }; | |
| export const getCompetencyRatingsByAppraisalId = async (appraisalId: number): Promise<ResponseData<CompetencyRating[]>> => { | |
| try { | |
| // Use the correct endpoint path as specified in API documentation | |
| const apiUrl = `/api/CompetencyRating/by-appraisal/${appraisalId}`; | |
| console.log("Calling API for competency ratings by appraisal ID:", apiUrl); | |
| try { | |
| // First attempt: Try to use the correct filtered endpoint | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property and the expected format | |
| if (response.data) { | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<CompetencyRating[]>; | |
| } | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as CompetencyRating[], | |
| success: true | |
| }; | |
| } | |
| } | |
| } catch (error) { | |
| console.log("Filtered endpoint failed, falling back to all ratings", error); | |
| // If filtered endpoint fails, fall back to getting all ratings | |
| } | |
| // Fall back to getting all ratings and filtering client-side | |
| const ratings = await getCompetencyRatings(); | |
| // Handle case when ratings.data is undefined | |
| if (!ratings.success || !ratings.data) { | |
| console.warn("No competency ratings data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredRatings = ratings.data.filter(rating => rating.appraisalId === appraisalId); | |
| console.log(`Filtered ${ratings.data.length} ratings to ${filteredRatings.length} for appraisal ${appraisalId}`); | |
| return { | |
| data: filteredRatings, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error getting competency ratings by appraisal ID:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getCompetencyRatingById = async (ratingId: number): Promise<ResponseData<CompetencyRating>> => { | |
| const response = await apiClient.get(`/api/CompetencyRating/${ratingId}`); | |
| return response.data; | |
| }; | |
| export const createCompetencyRating = async (rating: CompetencyRatingCreateRequest): Promise<ResponseData<CompetencyRating>> => { | |
| const response = await apiClient.post(`/api/CompetencyRating`, rating); | |
| return response.data; | |
| }; | |
| export const updateCompetencyRating = async (rating: CompetencyRatingUpdateRequest): Promise<ResponseData<CompetencyRating>> => { | |
| const response = await apiClient.put(`/api/CompetencyRating/${rating.ratingId}`, rating); | |
| return response.data; | |
| }; | |
| export const deleteCompetencyRating = async (ratingId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/CompetencyRating/${ratingId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Training Needs - FIXED | |
| // ===================================================================== | |
| export const getTrainingNeeds = async (): Promise<ResponseData<TrainingNeed[]>> => { | |
| const response = await apiClient.get(`/api/TrainingNeed`); | |
| return response.data; | |
| }; | |
| export const getTrainingNeedsByAppraisalId = async (appraisalId: number): Promise<ResponseData<TrainingNeed[]>> => { | |
| try { | |
| // Use the correct endpoint path as specified in API documentation | |
| const apiUrl = `/api/TrainingNeed/by-appraisal/${appraisalId}`; | |
| console.log("Calling API for training needs by appraisal ID:", apiUrl); | |
| try { | |
| // First attempt: Try to use the correct filtered endpoint | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property and the expected format | |
| if (response.data) { | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<TrainingNeed[]>; | |
| } | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as TrainingNeed[], | |
| success: true | |
| }; | |
| } | |
| } | |
| } catch (error) { | |
| console.log("Filtered endpoint failed, falling back to all training needs", error); | |
| // If filtered endpoint fails, fall back to getting all training needs | |
| } | |
| // Fall back to getting all training needs and filtering client-side | |
| const trainingNeeds = await getTrainingNeeds(); | |
| // Handle case when trainingNeeds.data is undefined | |
| if (!trainingNeeds.success || !trainingNeeds.data) { | |
| console.warn("No training needs data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredNeeds = trainingNeeds.data.filter(need => need.appraisalId === appraisalId); | |
| console.log(`Filtered ${trainingNeeds.data.length} training needs to ${filteredNeeds.length} for appraisal ${appraisalId}`); | |
| return { | |
| data: filteredNeeds, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error getting training needs by appraisal ID:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getTrainingNeedById = async (trainingId: number): Promise<ResponseData<TrainingNeed>> => { | |
| const response = await apiClient.get(`/api/TrainingNeed/${trainingId}`); | |
| return response.data; | |
| }; | |
| export const createTrainingNeed = async (trainingNeed: TrainingNeedCreateRequest): Promise<ResponseData<TrainingNeed>> => { | |
| try { | |
| console.log("Creating training need:", trainingNeed); | |
| const apiUrl = `/api/TrainingNeed`; | |
| console.log("Calling API: POST", apiUrl); | |
| const response = await apiClient.post<any>(apiUrl, trainingNeed); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: {} as TrainingNeed, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<TrainingNeed>; | |
| } | |
| // If response.data is the training need object, wrap it in ResponseData | |
| if (response.data.trainingId) { | |
| return { | |
| data: response.data as TrainingNeed, | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: {} as TrainingNeed, | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in createTrainingNeed:", error); | |
| return { | |
| data: {} as TrainingNeed, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const updateTrainingNeed = async (trainingNeed: TrainingNeedUpdateRequest): Promise<ResponseData<TrainingNeed>> => { | |
| try { | |
| console.log("Updating training need:", trainingNeed); | |
| const apiUrl = `/api/TrainingNeed/${trainingNeed.trainingId}`; | |
| console.log("Calling API: PUT", apiUrl); | |
| const response = await apiClient.put<any>(apiUrl, trainingNeed); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: {} as TrainingNeed, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<TrainingNeed>; | |
| } | |
| // If response.data is the training need object, wrap it in ResponseData | |
| if (response.data.trainingId) { | |
| return { | |
| data: response.data as TrainingNeed, | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: {} as TrainingNeed, | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in updateTrainingNeed:", error); | |
| return { | |
| data: {} as TrainingNeed, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const deleteTrainingNeed = async (trainingId: number): Promise<ResponseData<boolean>> => { | |
| try { | |
| console.log("Deleting training need:", trainingId); | |
| const apiUrl = `/api/TrainingNeed/${trainingId}`; | |
| console.log("Calling API: DELETE", apiUrl); | |
| const response = await apiClient.delete(apiUrl); | |
| // Check if response has data property | |
| if (response.data && typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<boolean>; | |
| } | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200, | |
| message: response.status === 200 ? "Training need deleted successfully" : "Failed to delete training need" | |
| }; | |
| } catch (error) { | |
| console.error("Error in deleteTrainingNeed:", error); | |
| return { | |
| data: false, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| // ===================================================================== | |
| // HR Remarks - FIXED | |
| // ===================================================================== | |
| export const getHrRemarks = async (): Promise<ResponseData<HrRemark[]>> => { | |
| const response = await apiClient.get(`/api/HrRemark`); | |
| return response.data; | |
| }; | |
| export const getHrRemarksByAppraisalId = async (appraisalId: number): Promise<ResponseData<HrRemark[]>> => { | |
| try { | |
| // Use the correct endpoint path as specified in API documentation | |
| const apiUrl = `/api/HrRemark/by-appraisal/${appraisalId}`; | |
| console.log("Calling API for HR remarks by appraisal ID:", apiUrl); | |
| try { | |
| // First attempt: Try to use the correct filtered endpoint | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property and the expected format | |
| if (response.data) { | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<HrRemark[]>; | |
| } | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as HrRemark[], | |
| success: true | |
| }; | |
| } | |
| } | |
| } catch (error) { | |
| console.log("Filtered endpoint failed, falling back to all HR remarks", error); | |
| // If filtered endpoint fails, fall back to getting all HR remarks | |
| } | |
| // Fall back to getting all HR remarks and filtering client-side | |
| const hrRemarks = await getHrRemarks(); | |
| // Handle case when hrRemarks.data is undefined | |
| if (!hrRemarks.success || !hrRemarks.data) { | |
| console.warn("No HR remarks data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredRemarks = hrRemarks.data.filter(remark => remark.appraisalId === appraisalId); | |
| console.log(`Filtered ${hrRemarks.data.length} HR remarks to ${filteredRemarks.length} for appraisal ${appraisalId}`); | |
| return { | |
| data: filteredRemarks, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error getting HR remarks by appraisal ID:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getHrRemarkById = async (hrRemarkId: number): Promise<ResponseData<HrRemark>> => { | |
| const response = await apiClient.get(`/api/HrRemark/${hrRemarkId}`); | |
| return response.data; | |
| }; | |
| export const createHrRemark = async (hrRemark: HrRemarkCreateRequest): Promise<ResponseData<HrRemark>> => { | |
| const response = await apiClient.post(`/api/HrRemark`, hrRemark); | |
| return response.data; | |
| }; | |
| export const updateHrRemark = async (hrRemark: HrRemarkUpdateRequest): Promise<ResponseData<HrRemark>> => { | |
| const response = await apiClient.put(`/api/HrRemark/${hrRemark.hrRemarkId}`, hrRemark); | |
| return response.data; | |
| }; | |
| export const deleteHrRemark = async (hrRemarkId: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/HrRemark/${hrRemarkId}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Management Remarks - FIXED | |
| // ===================================================================== | |
| export const getManagementRemarks = async (): Promise<ResponseData<ManagementRemark[]>> => { | |
| const response = await apiClient.get(`/api/ManagementRemark`); | |
| return response.data; | |
| }; | |
| export const getManagementRemarkById = async (id: number): Promise<ResponseData<ManagementRemark>> => { | |
| const response = await apiClient.get(`/api/ManagementRemark/${id}`); | |
| return response.data; | |
| }; | |
| export const getManagementRemarksByAppraisalId = async (appraisalId: number): Promise<ResponseData<ManagementRemark[]>> => { | |
| try { | |
| // Use the correct endpoint path as specified in API documentation | |
| const apiUrl = `/api/ManagementRemark/by-appraisal/${appraisalId}`; | |
| console.log("Calling API for management remarks by appraisal ID:", apiUrl); | |
| try { | |
| // First attempt: Try to use the correct filtered endpoint | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property and the expected format | |
| if (response.data) { | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<ManagementRemark[]>; | |
| } | |
| if (Array.isArray(response.data)) { | |
| return { | |
| data: response.data as ManagementRemark[], | |
| success: true | |
| }; | |
| } | |
| } | |
| } catch (error) { | |
| console.log("Filtered endpoint failed, falling back to all management remarks", error); | |
| // If filtered endpoint fails, fall back to getting all management remarks | |
| } | |
| // Fall back to getting all management remarks and filtering client-side | |
| const remarks = await getManagementRemarks(); | |
| // Handle case when remarks.data is undefined | |
| if (!remarks.success || !remarks.data) { | |
| console.warn("No management remarks data available"); | |
| return { | |
| data: [], | |
| success: true | |
| }; | |
| } | |
| const filteredRemarks = remarks.data.filter(remark => remark.appraisalId === appraisalId); | |
| console.log(`Filtered ${remarks.data.length} management remarks to ${filteredRemarks.length} for appraisal ${appraisalId}`); | |
| return { | |
| data: filteredRemarks, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error getting management remarks by appraisal ID:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const createManagementRemark = async (remark: ManagementRemarkCreateRequest): Promise<ResponseData<ManagementRemark>> => { | |
| const response = await apiClient.post(`/api/ManagementRemark`, remark); | |
| return response.data; | |
| }; | |
| export const updateManagementRemark = async (remark: ManagementRemarkUpdateRequest): Promise<ResponseData<ManagementRemark>> => { | |
| const response = await apiClient.put(`/api/ManagementRemark/${remark.id}`, remark); | |
| return response.data; | |
| }; | |
| export const deleteManagementRemark = async (id: number): Promise<ResponseData<boolean>> => { | |
| const response = await apiClient.delete(`/api/ManagementRemark/${id}`); | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| }; | |
| // ===================================================================== | |
| // Report Generation | |
| // ===================================================================== | |
| export const generateAppraisalReport = async (appraisalId: number): Promise<ResponseData<string>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Appraisal/${appraisalId}/report`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: "", | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<string>; | |
| } | |
| // Assume response.data is the report URL or content | |
| return { | |
| data: response.data as string, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in generateAppraisalReport:", error); | |
| return { | |
| data: "", | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getAppraisalStatistics = async (): Promise<ResponseData<any>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/Appraisal/statistics`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: {}, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<any>; | |
| } | |
| // Assume response.data is the statistics object | |
| return { | |
| data: response.data, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAppraisalStatistics:", error); | |
| return { | |
| data: {}, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| // ===================================================================== | |
| // Assessment Areas | |
| // ===================================================================== | |
| export const getAssessmentAreas = async (): Promise<ResponseData<AssessmentArea[]>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AssessmentArea`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| console.log("Raw Assessment Areas API response:", response); | |
| // Check if response has data property | |
| if (!response.data) { | |
| console.error("API response is missing data"); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| console.log("Response already in expected format:", response.data); | |
| return response.data as ResponseData<AssessmentArea[]>; | |
| } | |
| // If response.data is an array, assume it's the areas data | |
| if (Array.isArray(response.data)) { | |
| console.log("Response is an array, wrapping in success structure"); | |
| return { | |
| data: response.data as AssessmentArea[], | |
| success: true | |
| }; | |
| } | |
| // If we reach here, we have data but it's in an unexpected format | |
| console.warn("API returned data in an unexpected format:", response.data); | |
| return { | |
| data: [], | |
| success: false, | |
| message: "API returned data in an unexpected format" | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAssessmentAreas:", error); | |
| return { | |
| data: [], | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const getAssessmentAreaById = async (areaId: number): Promise<ResponseData<AssessmentArea>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AssessmentArea/${areaId}`; | |
| console.log("Calling API: GET", apiUrl); | |
| const response = await apiClient.get<any>(apiUrl); | |
| // Check if response has data property | |
| if (!response.data) { | |
| return { | |
| data: {} as AssessmentArea, | |
| success: false, | |
| message: "API response is missing data" | |
| }; | |
| } | |
| // Check if response.data already has the expected structure | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AssessmentArea>; | |
| } | |
| // Assume response.data is the assessment area object | |
| return { | |
| data: response.data as AssessmentArea, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in getAssessmentAreaById:", error); | |
| return { | |
| data: {} as AssessmentArea, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const createAssessmentArea = async (area: AssessmentAreaCreateRequest): Promise<ResponseData<AssessmentArea>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AssessmentArea`; | |
| console.log("Calling API: POST", apiUrl, area); | |
| const response = await apiClient.post<any>(apiUrl, area); | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AssessmentArea>; | |
| } | |
| return { | |
| data: response.data as AssessmentArea, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in createAssessmentArea:", error); | |
| return { | |
| data: {} as AssessmentArea, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const updateAssessmentArea = async (area: AssessmentAreaUpdateRequest): Promise<ResponseData<AssessmentArea>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AssessmentArea/${area.areaId}`; | |
| console.log("Calling API: PUT", apiUrl, area); | |
| const response = await apiClient.put<any>(apiUrl, area); | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<AssessmentArea>; | |
| } | |
| return { | |
| data: response.data as AssessmentArea, | |
| success: true | |
| }; | |
| } catch (error) { | |
| console.error("Error in updateAssessmentArea:", error); | |
| return { | |
| data: {} as AssessmentArea, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |
| export const deleteAssessmentArea = async (areaId: number): Promise<ResponseData<boolean>> => { | |
| try { | |
| const apiUrl = `${BASE_URL}/AssessmentArea/${areaId}`; | |
| console.log("Calling API: DELETE", apiUrl); | |
| const response = await apiClient.delete(apiUrl); | |
| if (typeof response.data.success !== 'undefined') { | |
| return response.data as ResponseData<boolean>; | |
| } | |
| return { | |
| data: response.status === 200, | |
| success: response.status === 200 | |
| }; | |
| } catch (error) { | |
| console.error("Error in deleteAssessmentArea:", error); | |
| return { | |
| data: false, | |
| success: false, | |
| message: error instanceof Error ? error.message : "Unknown error occurred" | |
| }; | |
| } | |
| }; | |