Add custom date range support to profit analytics

Introduces a date range picker to the profit analytics dashboard, allowing users to select custom date ranges for profit calculations. Updates ProfitAnalyticsChart and profit-analytics-service to handle both period and date range queries, improving flexibility and user experience.
This commit is contained in:
g
2025-11-28 18:32:38 +00:00
parent 28e292abb3
commit b10b2f2701
5 changed files with 98 additions and 14 deletions

View File

@@ -2,6 +2,8 @@ import { apiRequest } from '../api';
export interface ProfitOverview {
period: string;
startDate?: string;
endDate?: string;
summary: {
totalRevenue: number;
revenueFromTrackedProducts: number;
@@ -40,10 +42,45 @@ export interface ProfitTrend {
profitMargin: number;
}
export const getProfitOverview = async (period: string = '30'): Promise<ProfitOverview> => {
return apiRequest(`/analytics/profit-overview?period=${period}`);
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 (period: string = '30'): Promise<ProfitTrend[]> => {
return apiRequest(`/analytics/profit-trends?period=${period}`);
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);
};