Refine seller dashboard chart
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m24s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m24s
This commit is contained in:
@@ -469,7 +469,7 @@ const buildRfmClients = (baseClients) => {
|
||||
|
||||
const getDashboardAnalytics = async (range = {}) => {
|
||||
const { params, whereClause } = buildDateFilter(range);
|
||||
const [totalsResult, salesResult, revenueResult, sellerRevenueResult, sellerOrdersResult] = await Promise.all([
|
||||
const [totalsResult, salesResult, revenueResult, sellerRevenueResult, sellerOrdersResult, sellerRevenueByDateResult] = await Promise.all([
|
||||
pool.query(`
|
||||
SELECT
|
||||
COALESCE(SUM(quantidade * valor_unitario), 0) as total_revenue,
|
||||
@@ -543,6 +543,39 @@ const getDashboardAnalytics = async (range = {}) => {
|
||||
GROUP BY seller_key
|
||||
ORDER BY value DESC
|
||||
LIMIT 10;
|
||||
`, params),
|
||||
pool.query(`
|
||||
WITH seller_orders AS (
|
||||
SELECT
|
||||
COALESCE(${SELLER_ID_SQL}, 'name:' || ${SELLER_NAME_SQL}) as seller_key,
|
||||
COALESCE(${SELLER_NAME_SQL}, ${SELLER_ID_SQL}, 'Sem vendedor') as seller_name,
|
||||
data_pedido_date,
|
||||
quantidade,
|
||||
valor_unitario,
|
||||
pedido_id,
|
||||
data_pedido,
|
||||
valor_pedido
|
||||
FROM orders
|
||||
${whereClause}
|
||||
AND (${SELLER_ID_SQL} IS NOT NULL OR ${SELLER_NAME_SQL} IS NOT NULL)
|
||||
),
|
||||
top_sellers AS (
|
||||
SELECT seller_key
|
||||
FROM seller_orders
|
||||
GROUP BY seller_key
|
||||
ORDER BY COALESCE(SUM(quantidade * valor_unitario), 0) DESC
|
||||
LIMIT 8
|
||||
)
|
||||
SELECT
|
||||
seller_orders.seller_key as id,
|
||||
MAX(seller_orders.seller_name) as name,
|
||||
seller_orders.data_pedido_date::text as date,
|
||||
COALESCE(SUM(seller_orders.quantidade * seller_orders.valor_unitario), 0) as value,
|
||||
COUNT(DISTINCT COALESCE(NULLIF(seller_orders.pedido_id, ''), seller_orders.data_pedido || '_' || seller_orders.valor_pedido::text))::int as orders
|
||||
FROM seller_orders
|
||||
INNER JOIN top_sellers ON top_sellers.seller_key = seller_orders.seller_key
|
||||
GROUP BY seller_orders.seller_key, seller_orders.data_pedido_date
|
||||
ORDER BY seller_orders.data_pedido_date ASC, value DESC;
|
||||
`, params)
|
||||
]);
|
||||
|
||||
@@ -578,6 +611,13 @@ const getDashboardAnalytics = async (range = {}) => {
|
||||
name: row.name,
|
||||
id: row.id,
|
||||
value: toNumber(row.value)
|
||||
})),
|
||||
sellerRevenueByDate: sellerRevenueByDateResult.rows.map(row => ({
|
||||
name: row.name,
|
||||
id: row.id,
|
||||
date: row.date,
|
||||
value: toNumber(row.value),
|
||||
orders: toNumber(row.orders)
|
||||
}))
|
||||
};
|
||||
};
|
||||
|
||||
@@ -153,6 +153,12 @@ test('getDashboardAnalytics includes seller revenue and order metrics', async ()
|
||||
};
|
||||
}
|
||||
|
||||
if (sql.includes('data_pedido_date::text as date')) {
|
||||
return {
|
||||
rows: [{ id: '977226210', name: 'KEDMA DA SILVA', date: '2026-06-05', value: 400, orders: 2 }]
|
||||
};
|
||||
}
|
||||
|
||||
if (sql.includes('seller_orders') && sql.includes('SUM(quantidade * valor_unitario)')) {
|
||||
return {
|
||||
rows: [{ id: '977226210', name: 'KEDMA DA SILVA', value: 750 }]
|
||||
@@ -181,7 +187,7 @@ test('getDashboardAnalytics includes seller revenue and order metrics', async ()
|
||||
const sellerRevenueQuery = calls.find(call => call.sql.includes('seller_orders') && call.sql.includes('SUM(quantidade * valor_unitario)'));
|
||||
const sellerOrdersQuery = calls.find(call => call.sql.includes('seller_orders') && call.sql.includes('COUNT(DISTINCT'));
|
||||
|
||||
assert.equal(calls.length, 5);
|
||||
assert.equal(calls.length, 6);
|
||||
calls.forEach(call => {
|
||||
assert.deepEqual(call.params, ['2026-06-01', '2026-06-24']);
|
||||
});
|
||||
@@ -194,6 +200,9 @@ test('getDashboardAnalytics includes seller revenue and order metrics', async ()
|
||||
assert.deepEqual(dashboard.ordersBySeller, [
|
||||
{ id: '977226210', name: 'KEDMA DA SILVA', value: 3 }
|
||||
]);
|
||||
assert.deepEqual(dashboard.sellerRevenueByDate, [
|
||||
{ id: '977226210', name: 'KEDMA DA SILVA', date: '2026-06-05', value: 400, orders: 2 }
|
||||
]);
|
||||
} finally {
|
||||
pool.query = originalQuery;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user