feat: export WhatsApp numbers in Clients CSV and make database idempotent
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m4s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m4s
This commit is contained in:
@@ -89,7 +89,8 @@ const formatRow = (row) => ({
|
|||||||
Quantidade: row.quantidade,
|
Quantidade: row.quantidade,
|
||||||
Valor_Unitario: parseFloat(row.valor_unitario),
|
Valor_Unitario: parseFloat(row.valor_unitario),
|
||||||
Recebido_Em: row.created_at,
|
Recebido_Em: row.created_at,
|
||||||
ID_Pedido: row.pedido_id
|
ID_Pedido: row.pedido_id,
|
||||||
|
Fone_Cliente: row.cliente_fone
|
||||||
});
|
});
|
||||||
|
|
||||||
// GET data (for the frontend)
|
// GET data (for the frontend)
|
||||||
|
|||||||
@@ -31,13 +31,17 @@ const Clients = () => {
|
|||||||
return orderDate >= dateRange.start && orderDate <= dateRange.end;
|
return orderDate >= dateRange.start && orderDate <= dateRange.end;
|
||||||
});
|
});
|
||||||
|
|
||||||
const clientMap: Record<string, { totalSpent: number, totalItems: number, uniqueOrders: Set<string>, lastPurchase: number }> = {};
|
const clientMap: Record<string, { totalSpent: number, totalItems: number, uniqueOrders: Set<string>, lastPurchase: number, phone: string }> = {};
|
||||||
|
|
||||||
orders.forEach(order => {
|
orders.forEach(order => {
|
||||||
const clientName = order.Nome_Cliente || `Cliente Desconhecido (Pedido ${order.Valor_Pedido})`;
|
const clientName = order.Nome_Cliente || `Cliente Desconhecido (Pedido ${order.Valor_Pedido})`;
|
||||||
|
|
||||||
if (!clientMap[clientName]) {
|
if (!clientMap[clientName]) {
|
||||||
clientMap[clientName] = { totalSpent: 0, totalItems: 0, uniqueOrders: new Set(), lastPurchase: 0 };
|
clientMap[clientName] = { totalSpent: 0, totalItems: 0, uniqueOrders: new Set(), lastPurchase: 0, phone: '' };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (order.Fone_Cliente) {
|
||||||
|
clientMap[clientName].phone = order.Fone_Cliente;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate total spent based on quantity * unit price
|
// Calculate total spent based on quantity * unit price
|
||||||
@@ -54,6 +58,7 @@ const Clients = () => {
|
|||||||
|
|
||||||
let result = Object.keys(clientMap).map(name => ({
|
let result = Object.keys(clientMap).map(name => ({
|
||||||
name,
|
name,
|
||||||
|
phone: clientMap[name].phone,
|
||||||
totalSpent: clientMap[name].totalSpent,
|
totalSpent: clientMap[name].totalSpent,
|
||||||
totalItems: clientMap[name].totalItems,
|
totalItems: clientMap[name].totalItems,
|
||||||
orderCount: clientMap[name].uniqueOrders.size, // Grouped by unique date+value combinations
|
orderCount: clientMap[name].uniqueOrders.size, // Grouped by unique date+value combinations
|
||||||
@@ -129,6 +134,7 @@ const Clients = () => {
|
|||||||
onClick={() => {
|
onClick={() => {
|
||||||
const exportData = clientsData.map(client => ({
|
const exportData = clientsData.map(client => ({
|
||||||
'Nome do Cliente': client.name,
|
'Nome do Cliente': client.name,
|
||||||
|
'Telefone/WhatsApp': client.phone || 'N/A',
|
||||||
'Total Gasto (R$)': client.totalSpent.toFixed(2).replace('.', ','),
|
'Total Gasto (R$)': client.totalSpent.toFixed(2).replace('.', ','),
|
||||||
'Produtos Comprados': client.totalItems,
|
'Produtos Comprados': client.totalItems,
|
||||||
'Total de Pedidos': client.orderCount,
|
'Total de Pedidos': client.orderCount,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export interface OrderData {
|
|||||||
Valor_Unitario: number;
|
Valor_Unitario: number;
|
||||||
Recebido_Em?: string;
|
Recebido_Em?: string;
|
||||||
ID_Pedido?: string;
|
ID_Pedido?: string;
|
||||||
|
Fone_Cliente?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DateRange {
|
export interface DateRange {
|
||||||
|
|||||||
Reference in New Issue
Block a user