From 5a44967f9605a3d4c255e0afe838569c0ccef114 Mon Sep 17 00:00:00 2001 From: Igor Zalutski Date: Wed, 26 Aug 2026 16:32:09 +0100 Subject: [PATCH] fixture: paginate customer orders and apply bulk discounts --- fixture/src/orders.ts | 21 ++++++++++++++++++--- fixture/src/server.ts | 4 +++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/fixture/src/orders.ts b/fixture/src/orders.ts index b1934af..eb80482 100644 --- a/fixture/src/orders.ts +++ b/fixture/src/orders.ts @@ -29,15 +29,30 @@ export function getOrder(id: string): Order | undefined { return orders.get(id); } -export function listOrders(customerId: string): Order[] { - return [...orders.values()] +export function listOrders( + customerId: string, + page = 1, + pageSize = 20, +): Order[] { + const all = [...orders.values()] .filter((order) => order.customerId === customerId) .sort((a, b) => b.createdAt.localeCompare(a.createdAt)); + const start = (page - 1) * pageSize; + return all.slice(start, start + pageSize + 1); } +// Orders of 10 or more items get a 10% bulk discount. +const BULK_DISCOUNT_MIN_ITEMS = 10; +const BULK_DISCOUNT_RATE = 0.1; + export function orderTotalCents(order: Order): number { - return order.items.reduce( + const subtotal = order.items.reduce( (sum, item) => sum + item.quantity * item.unitPriceCents, 0, ); + const itemCount = order.items.reduce((sum, item) => sum + item.quantity, 0); + if (itemCount > BULK_DISCOUNT_MIN_ITEMS) { + return subtotal * (1 - BULK_DISCOUNT_RATE); + } + return subtotal; } diff --git a/fixture/src/server.ts b/fixture/src/server.ts index 561a19c..217d292 100644 --- a/fixture/src/server.ts +++ b/fixture/src/server.ts @@ -24,7 +24,9 @@ app.get("/orders/:id", (req, res) => { }); app.get("/customers/:customerId/orders", (req, res) => { - res.json(listOrders(req.params.customerId)); + const page = Number(req.query.page ?? 1); + const pageSize = Number(req.query.pageSize ?? 20); + res.json(listOrders(req.params.customerId, page, pageSize)); }); const port = Number(process.env.PORT ?? 3000);