dashboard
This commit is contained in:
43
src/modules/0-ui/handlers/stats.js
Normal file
43
src/modules/0-ui/handlers/stats.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import { syncOrdersIncremental } from "../../4-woo-orders/wooOrders.js";
|
||||
import * as ordersRepo from "../../4-woo-orders/ordersRepo.js";
|
||||
|
||||
/**
|
||||
* Obtiene estadísticas de pedidos para el dashboard
|
||||
*/
|
||||
export async function handleGetOrderStats({ tenantId }) {
|
||||
// 1. Sincronizar pedidos nuevos de Woo
|
||||
const syncResult = await syncOrdersIncremental({ tenantId });
|
||||
|
||||
// 2. Obtener todas las estadísticas en paralelo
|
||||
const [monthlyStats, productStats, yoyStats, totals] = await Promise.all([
|
||||
ordersRepo.getMonthlyStats({ tenantId }),
|
||||
ordersRepo.getProductStats({ tenantId }),
|
||||
ordersRepo.getYoyStats({ tenantId }),
|
||||
ordersRepo.getTotals({ tenantId }),
|
||||
]);
|
||||
|
||||
return {
|
||||
// Stats mensuales (para gráficas de barras/líneas)
|
||||
months: monthlyStats.months,
|
||||
totals: monthlyStats.totals,
|
||||
order_counts: monthlyStats.order_counts,
|
||||
by_source: monthlyStats.by_source,
|
||||
by_shipping: monthlyStats.by_shipping,
|
||||
by_payment: monthlyStats.by_payment,
|
||||
|
||||
// Totales agregados (para donuts)
|
||||
totals_aggregated: totals,
|
||||
|
||||
// Stats por producto
|
||||
top_products_revenue: productStats.by_revenue,
|
||||
top_products_kg: productStats.by_kg,
|
||||
top_products_units: productStats.by_units,
|
||||
|
||||
// YoY
|
||||
yoy: yoyStats,
|
||||
|
||||
// Info de sync
|
||||
synced: syncResult.synced,
|
||||
total_in_cache: syncResult.total,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user