import axios from 'axios'; import apiClient from '@/lib/api/axios-instance'; import { createHeaders } from '@/lib/api'; import { API_BASE_URL } from '../config'; import { Communication, ResponseData } from '../types'; // API endpoints for communications export const getCommunications = async (): Promise | Communication[]> => { try { const response = await apiClient.get(`/api/Communication`); console.log("Raw Communication API response:", response); // If the response already has success property, it's in the expected format if (response.data && typeof response.data.success !== 'undefined') { return response.data; } // If the response is an array, let's wrap it in a ResponseData if (Array.isArray(response.data)) { console.log("Wrapping array response in ResponseData structure"); return { data: response.data, success: true }; } // Handle other potential formats console.warn("API returned unexpected format:", response.data); return { data: Array.isArray(response.data) ? response.data : [], success: true }; } catch (error) { console.error("Error in getCommunications:", error); return { data: [], success: false, message: error instanceof Error ? error.message : "Unknown error occurred" }; } }; // Export alias for compatibility export const getAllCommunications = getCommunications; export const getCommunicationById = async (id: number): Promise> => { const response = await apiClient.get(`/api/Communication/${id}`); return response.data; }; export const createCommunication = async (communication: any): Promise> => { const response = await apiClient.post(`/api/Communication`, communication); return response.data; }; export const updateCommunication = async (id: number, communication: any): Promise> => { const response = await apiClient.put(`/api/Communication/${id}`, communication); return response.data; }; export const deleteCommunication = async (id: number): Promise> => { const response = await apiClient.delete(`/api/Communication/${id}`); return { data: response.status === 200, success: response.status === 200 }; }; // Additional helper methods export const getCommunicationsByProjectId = async (projectId: number): Promise> => { const response = await getCommunications(); // Extract the communications array depending on the response type const communications = Array.isArray(response) ? response : response.data; const filteredCommunications = communications.filter(comm => comm.projectId === projectId); return { data: filteredCommunications, success: true }; }; export const getCommunicationsByDate = async (startDate: string, endDate: string): Promise> => { const response = await getCommunications(); // Extract the communications array depending on the response type const communications = Array.isArray(response) ? response : response.data; const start = new Date(startDate); const end = new Date(endDate); const filteredCommunications = communications.filter(comm => { const commDate = new Date(comm.communicationDate); return commDate >= start && commDate <= end; }); return { data: filteredCommunications, success: true }; };