Add client purchase pattern charts
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m9s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m9s
This commit is contained in:
@@ -38,6 +38,14 @@ export interface ClientDetailsMetrics {
|
||||
date: string;
|
||||
value: number;
|
||||
}>;
|
||||
purchaseWeekdays: Array<{
|
||||
label: string;
|
||||
value: number;
|
||||
}>;
|
||||
purchaseHours: Array<{
|
||||
label: string;
|
||||
value: number;
|
||||
}>;
|
||||
groupedOrders: GroupedClientOrder[];
|
||||
allTimeOrderCount: number;
|
||||
clientName: string;
|
||||
@@ -49,6 +57,22 @@ export interface ClientDetailsMetrics {
|
||||
periodItems: number;
|
||||
}
|
||||
|
||||
const WEEKDAY_LABELS = ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'];
|
||||
|
||||
const getOrderHour = (order: OrderData): number | null => {
|
||||
const timestamp = order.Recebido_Em || (/\d{1,2}:\d{2}/.test(order.Data_Pedido || '') ? order.Data_Pedido : '');
|
||||
if (!timestamp) return null;
|
||||
|
||||
const date = new Date(timestamp);
|
||||
if (!Number.isNaN(date.getTime())) return date.getHours();
|
||||
|
||||
const timeMatch = String(timestamp).match(/\b(\d{1,2}):\d{2}/);
|
||||
if (!timeMatch) return null;
|
||||
|
||||
const hour = Number(timeMatch[1]);
|
||||
return hour >= 0 && hour <= 23 ? hour : null;
|
||||
};
|
||||
|
||||
const scoreTertile = (value: number, values: number[], higherIsBetter = true) => {
|
||||
const numericValues = values.filter(Number.isFinite);
|
||||
if (!numericValues.length) return 1;
|
||||
@@ -230,9 +254,29 @@ export const buildClientDetailsMetrics = (ordersData: OrderData[], customerKey:
|
||||
date,
|
||||
value: spentByDate[date]
|
||||
})).sort((a, b) => parseOrderDate(a.date).getTime() - parseOrderDate(b.date).getTime());
|
||||
const weekdayCounts = WEEKDAY_LABELS.map(label => ({ label, value: 0 }));
|
||||
const hourCounts = Array.from({ length: 24 }, (_, hour) => ({
|
||||
label: `${String(hour).padStart(2, '0')}h`,
|
||||
value: 0
|
||||
}));
|
||||
|
||||
groupedOrders.forEach(group => {
|
||||
const firstItem = group.items[0];
|
||||
const orderDate = parseOrderDate(group.date);
|
||||
if (!Number.isNaN(orderDate.getTime())) {
|
||||
weekdayCounts[orderDate.getDay()].value += 1;
|
||||
}
|
||||
|
||||
const orderHour = firstItem ? getOrderHour(firstItem) : null;
|
||||
if (orderHour !== null) {
|
||||
hourCounts[orderHour].value += 1;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
chartData,
|
||||
purchaseWeekdays: weekdayCounts,
|
||||
purchaseHours: hourCounts,
|
||||
groupedOrders,
|
||||
allTimeOrderCount: allTimeOrderIds.size,
|
||||
clientName,
|
||||
|
||||
Reference in New Issue
Block a user