Skip to content

Fix pricing bug, dashboard N+1 query, and add Duplicate Order feature - #5

Open
ayyushrk wants to merge 3 commits into
MulearnMCE:masterfrom
ayyushrk:fix/pricing-dashboard-duplicate-order
Open

Fix pricing bug, dashboard N+1 query, and add Duplicate Order feature#5
ayyushrk wants to merge 3 commits into
MulearnMCE:masterfrom
ayyushrk:fix/pricing-dashboard-duplicate-order

Conversation

@ayyushrk

@ayyushrk ayyushrk commented Aug 12, 2026

Copy link
Copy Markdown

This PR fixes the three issues from the qualifying task.

1. Pricing bug

The bug was in computeOrderTotals. When a Pro user redeemed a discount code
that included free shipping, the code added the shipping rate to the discount
amount no matter what — even if that user's shipping was already free because
they were above the Pro threshold. So they'd get credited for "free shipping"
twice: once because shipping was already 0, and again as an extra discount.
That's what was pushing totals lower than they should've been.

Fix: only add that shipping credit to the discount if shipping wasn't already
free from the threshold. Added a test file (tests/pricing.test.ts) to lock
this in.

2. Dashboard performance

listOrders was fetching all orders, then looping over them and firing a
separate query for each order's items — classic N+1. For someone with a lot
of order history that's a lot of round trips. Switched it to one query using
Prisma's include so everything comes back together.

3. Duplicate Order

Added a POST /api/orders/[id]/duplicate route that checks the order's items
against current stock/availability, and a button on the dashboard
(DuplicateOrderButton) that calls it and adds whatever's still available into
the cart. If something's out of stock or no longer active it just gets
skipped and reported back instead of failing the whole thing.

Checked

  • tsc --noEmit passes
  • vitest run — all tests pass
  • Tested it manually on /dashboard, cart updates correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant