Add a top-products revenue report

Node · Node · intermediate · modification

Turns the /reports/top-products endpoint into a proper leaderboard: it now aggregates delivered orders per product and returns the five biggest sellers, highest first, instead of dumping every product unsorted. Matches the finance team's request for a top-5 revenue board.

Powers the 'Top products' widget on the finance dashboard; the numbers drive which SKUs get restocked and featured.

Requirements

Files touched

--- src/reports/topProducts.js
 
 // GET /reports/top-products
+// Return the 5 products with the highest total revenue, highest first.
 router.get('/reports/top-products', async (req, res) => {
   const orders = await getOrders();
   const products = aggregate(orders);
-  res.json(products);
+  const ranked = products
+    .sort((a, b) => b.unitsSold - a.unitsSold)
+    .slice(0, 5);
+  res.json(ranked);
 });
 

Review this PR

Node practice