Orders list for the ops dashboard
Node · Node · intermediate · greenfield
Adds the orders endpoint for the ops dashboard: fetches the 50 most recent orders and enriches each with the customer's name and email. Tested locally, responses look complete.
Production: ~200k orders, ~40k customers in Postgres. Every open dashboard polls this endpoint every 30 seconds.
Requirements
- Add `GET /orders` returning the 50 most recent orders.
- Each order includes the customer's name and email.
- Fetching the orders and their customer details must use at most two database queries per request, no matter how many orders are returned.
Files touched
- src/routes/orders.js
--- src/routes/orders.js
+const express = require("express");
+const db = require("../lib/db");
+
+const router = express.Router();
+
+router.get("/orders", async (req, res) => {
+ const orders = await db.orders.findRecent(50);
+ for (const order of orders) {
+ const customer = await db.customers.findById(order.customerId);
+ order.customerName = customer.name;
+ order.customerEmail = customer.email;
+ }
+ res.json(orders);
+});
+
+module.exports = router;