77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { getBaseUrl } from "./constants";
|
|
import { IFetchHealthDataParams, IHealthData, IDashboardData } from "./types";
|
|
|
|
export async function getTransactions({
|
|
transactionType,
|
|
query,
|
|
}: {
|
|
transactionType: string;
|
|
query: string;
|
|
}) {
|
|
const res = await fetch(
|
|
`${getBaseUrl()}/api/dashboard/transactions/${transactionType}?${query}`,
|
|
{
|
|
cache: "no-store",
|
|
}
|
|
);
|
|
|
|
if (!res.ok) {
|
|
// Handle error from the API
|
|
const errorData = await res
|
|
.json()
|
|
.catch(() => ({ message: "Unknown error" }));
|
|
throw new Error(errorData.message || `HTTP error! status: ${res.status}`);
|
|
}
|
|
|
|
return res.json();
|
|
}
|
|
|
|
/**
|
|
* Client-side function to fetch dashboard data (health + overview) via the /api/dashboard proxy
|
|
* This function calls a single endpoint that returns both health and overview data
|
|
*/
|
|
export async function getDashboardData({
|
|
dateStart,
|
|
dateEnd,
|
|
}: IFetchHealthDataParams = {}): Promise<IDashboardData> {
|
|
const params = new URLSearchParams();
|
|
if (dateStart) params.set("dateStart", dateStart);
|
|
if (dateEnd) params.set("dateEnd", dateEnd);
|
|
|
|
const queryString = params.toString();
|
|
const res = await fetch(
|
|
`${getBaseUrl()}/api/dashboard${queryString ? `?${queryString}` : ""}`,
|
|
{
|
|
cache: "no-store",
|
|
}
|
|
);
|
|
|
|
if (!res.ok) {
|
|
const errorData = await res
|
|
.json()
|
|
.catch(() => ({ message: "Unknown error" }));
|
|
throw new Error(errorData.message || `HTTP error! status: ${res.status}`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
|
|
// Extract overviewData and reviewTransactions from the response
|
|
const { overviewData, reviewTransactions, ...healthDataWithStats } = data;
|
|
|
|
return {
|
|
healthData: healthDataWithStats as IHealthData,
|
|
overviewData: overviewData || {
|
|
success: false,
|
|
successful_count: 0,
|
|
waiting_count: 0,
|
|
failed_count: 0,
|
|
cancelled_count: 0,
|
|
},
|
|
reviewTransactions: reviewTransactions || {
|
|
success: false,
|
|
transactions: [],
|
|
total: 0,
|
|
},
|
|
};
|
|
}
|