Fix RFV period buyers with historical scoring
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m34s

This commit is contained in:
Cauê Faleiros
2026-06-17 15:07:59 -03:00
parent 10c69457ba
commit 3a907b0743
6 changed files with 89 additions and 48 deletions

View File

@@ -13,6 +13,7 @@ export type ClientSortOption =
| 'items_asc';
export interface ClientSummary {
customerKey: string;
name: string;
phone: string;
totalSpent: number;
@@ -113,6 +114,7 @@ export const buildClientsSummary = (
): ClientSummary[] => {
const orders = filterOrdersByDateRange(ordersData, dateRange);
const clientMap: Record<string, {
name: string;
totalSpent: number;
totalItems: number;
uniqueOrders: Set<string>;
@@ -122,33 +124,35 @@ export const buildClientsSummary = (
orders.forEach(order => {
const clientName = getClientDisplayName(order);
const customerKey = order.Fone_Cliente || `name:${clientName}`;
if (!clientMap[clientName]) {
clientMap[clientName] = { totalSpent: 0, totalItems: 0, uniqueOrders: new Set(), lastPurchase: 0, phone: '' };
if (!clientMap[customerKey]) {
clientMap[customerKey] = { name: clientName, totalSpent: 0, totalItems: 0, uniqueOrders: new Set(), lastPurchase: 0, phone: '' };
}
if (order.Fone_Cliente) {
clientMap[clientName].phone = order.Fone_Cliente;
clientMap[customerKey].phone = order.Fone_Cliente;
}
clientMap[clientName].totalSpent += getOrderItemRevenue(order);
clientMap[clientName].totalItems += order.Quantidade;
clientMap[clientName].uniqueOrders.add(`${order.Data_Pedido}_${order.Valor_Pedido}`);
clientMap[customerKey].totalSpent += getOrderItemRevenue(order);
clientMap[customerKey].totalItems += order.Quantidade;
clientMap[customerKey].uniqueOrders.add(`${order.Data_Pedido}_${order.Valor_Pedido}`);
const orderTime = parseOrderDate(order.Data_Pedido).getTime();
if (orderTime > clientMap[clientName].lastPurchase) {
clientMap[clientName].lastPurchase = orderTime;
if (orderTime > clientMap[customerKey].lastPurchase) {
clientMap[customerKey].lastPurchase = orderTime;
}
});
const normalizedSearch = searchTerm.trim().toLowerCase();
const clients = enrichClientsWithRfmType(Object.keys(clientMap).map(name => ({
name,
phone: clientMap[name].phone,
totalSpent: clientMap[name].totalSpent,
totalItems: clientMap[name].totalItems,
orderCount: clientMap[name].uniqueOrders.size,
lastPurchase: clientMap[name].lastPurchase
const clients = enrichClientsWithRfmType(Object.keys(clientMap).map(customerKey => ({
customerKey,
name: clientMap[customerKey].name,
phone: clientMap[customerKey].phone,
totalSpent: clientMap[customerKey].totalSpent,
totalItems: clientMap[customerKey].totalItems,
orderCount: clientMap[customerKey].uniqueOrders.size,
lastPurchase: clientMap[customerKey].lastPurchase
})), dateRange);
const filteredClients = normalizedSearch