Show all clients in clients page
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 56s

This commit is contained in:
Cauê Faleiros
2026-06-16 16:24:10 -03:00
parent 37067751c7
commit ed1f129b07
4 changed files with 70 additions and 33 deletions

View File

@@ -235,27 +235,24 @@ const getClientAnalytics = async (range = {}) => {
const { params, whereClause } = buildDateFilter(range);
const result = await pool.query(`
SELECT
MAX(cliente_nome) as name,
cliente_fone as phone,
COALESCE(NULLIF(cliente_nome, ''), 'Cliente Desconhecido') as name,
MAX(NULLIF(cliente_fone, '')) as phone,
COALESCE(SUM(quantidade), 0) as quantity_purchased,
COALESCE(SUM(quantidade * valor_unitario), 0) as total_spent,
COUNT(*)::int as order_line_count,
COUNT(DISTINCT COALESCE(NULLIF(pedido_id, ''), data_pedido || '_' || valor_pedido::text))::int as order_count,
MAX(data_pedido_date) as last_purchase_date
FROM orders
${whereClause}
AND cliente_fone IS NOT NULL
AND cliente_fone != ''
GROUP BY cliente_fone
ORDER BY total_spent DESC
LIMIT 500;
GROUP BY COALESCE(NULLIF(cliente_nome, ''), 'Cliente Desconhecido')
ORDER BY total_spent DESC;
`, params);
return result.rows.map(row => ({
name: row.name,
phone: row.phone,
phone: row.phone || '',
quantityPurchased: toNumber(row.quantity_purchased),
totalSpent: toNumber(row.total_spent),
orderLineCount: toNumber(row.order_line_count),
orderCount: toNumber(row.order_count),
lastPurchaseDate: row.last_purchase_date
}));
};
@@ -282,8 +279,7 @@ const getRfmAnalytics = async (range = {}) => {
AND cliente_fone IS NOT NULL
AND cliente_fone != ''
GROUP BY cliente_fone
ORDER BY monetary DESC
LIMIT 1000;
ORDER BY monetary DESC;
`, params),
pool.query(`
SELECT