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)
|
||||
}))
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user