PR Review Lab · Show Order History on the Account Page

30:00left of 30:00

Show Order History on the Account Page

Adds an endpoint that returns a customer's recent orders with their items, a status column on orders, and the Order History list on the account page.

Mapi/routes/orders.ts1 of 3

Files Changed

api/routes/

db/migrations/

web/src/account/

api/routes/orders.ts

TypeScript · Click a line number to comment · Tap a line to comment
@@ -7,8 +7,26 @@ export const router = Router();
router.get("/orders/:id", requireAuth, async (req, res) => {
const { rows } = await db.query(
"SELECT * FROM orders WHERE id = $1 AND user_id = $2",
[req.params.id, req.user.id],
);
if (rows.length === 0) return res.sendStatus(404);
res.json(rows[0]);
});
Added:
Added: router.get("/users/:userId/orders", requireAuth, async (req, res) => {
Added: const userId = req.params.userId;
Added: const limit = Number(req.query.limit ?? 20);
Added: const orders = await db.query(
Added: `SELECT id, total_cents, status, created_at FROM orders
Added: WHERE user_id = $1 ORDER BY created_at DESC LIMIT ${limit}`,
Added: [userId],
Added: );
Added: for (const order of orders.rows) {
Added: const items = await db.query(
Added: "SELECT sku, name, quantity FROM order_items WHERE order_id = $1",
Added: [order.id],
Added: );
Added: order.items = items.rows;
Added: }
Added: res.json(orders.rows);
Added: });