Replaces all usages of clientFetch with the new apiRequest utility across dashboard pages, modal components, and the profit analytics service. This standardizes API interaction and improves consistency in request handling.
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
import { apiRequest } from '../api';
|
|
|
|
export interface ProfitOverview {
|
|
period: string;
|
|
startDate?: string;
|
|
endDate?: string;
|
|
summary: {
|
|
totalRevenue: number;
|
|
revenueFromTrackedProducts: number;
|
|
totalCost: number;
|
|
totalProfit: number;
|
|
overallProfitMargin: number;
|
|
averageProfitPerUnit: number;
|
|
costDataCoverage: number;
|
|
totalProductsSold: number;
|
|
productsWithCostData: number;
|
|
};
|
|
topProfitableProducts: Array<{
|
|
productId: string;
|
|
productName: string;
|
|
totalQuantitySold: number;
|
|
totalRevenue: number;
|
|
totalCost: number;
|
|
totalProfit: number;
|
|
averageProfit: number;
|
|
profitMargin: number;
|
|
}>;
|
|
hasCostData: boolean;
|
|
}
|
|
|
|
export interface ProfitTrend {
|
|
_id: {
|
|
year: number;
|
|
month: number;
|
|
day: number;
|
|
};
|
|
revenue: number;
|
|
cost: number;
|
|
profit: number;
|
|
orders: number;
|
|
itemsWithCostData: number;
|
|
profitMargin: number;
|
|
}
|
|
|
|
export interface DateRange {
|
|
from: Date;
|
|
to: Date;
|
|
}
|
|
|
|
export const getProfitOverview = async (
|
|
periodOrRange?: string | DateRange
|
|
): Promise<ProfitOverview> => {
|
|
let url = '/analytics/profit-overview';
|
|
|
|
if (periodOrRange && typeof periodOrRange !== 'string') {
|
|
// Date range provided
|
|
const startDate = periodOrRange.from.toISOString().split('T')[0];
|
|
const endDate = periodOrRange.to.toISOString().split('T')[0];
|
|
url += `?startDate=${startDate}&endDate=${endDate}`;
|
|
} else {
|
|
// Period provided (backward compatibility)
|
|
const period = periodOrRange || '30';
|
|
url += `?period=${period}`;
|
|
}
|
|
|
|
return apiRequest(url);
|
|
};
|
|
|
|
export const getProfitTrends = async (
|
|
periodOrRange?: string | DateRange
|
|
): Promise<ProfitTrend[]> => {
|
|
let url = '/analytics/profit-trends';
|
|
|
|
if (periodOrRange && typeof periodOrRange !== 'string') {
|
|
// Date range provided
|
|
const startDate = periodOrRange.from.toISOString().split('T')[0];
|
|
const endDate = periodOrRange.to.toISOString().split('T')[0];
|
|
url += `?startDate=${startDate}&endDate=${endDate}`;
|
|
} else {
|
|
// Period provided (backward compatibility)
|
|
const period = periodOrRange || '30';
|
|
url += `?period=${period}`;
|
|
}
|
|
|
|
return apiRequest(url);
|
|
};
|