Refactored all API calls to use the new clientFetch utility instead of apiRequest in dashboard pages, modal components, and profit analytics service. This improves consistency and aligns with updated API handling patterns.
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
import { clientFetch } 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 clientFetch(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 clientFetch(url);
|
|
};
|