Fix pricing bug, dashboard N+1 query, and add Duplicate Order feature - #5
Open
ayyushrk wants to merge 3 commits into
Open
Fix pricing bug, dashboard N+1 query, and add Duplicate Order feature#5ayyushrk wants to merge 3 commits into
ayyushrk wants to merge 3 commits into
Conversation
…duplicate order feature
…hboard N+1 query, add duplicate order API endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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