Fix crash when order total is between 500-1000: guard against undefined discount tier#5
Draft
kaushik94 wants to merge 1 commit into
Draft
Conversation
Fix crash when order total is between 500-1000: guard against undefined discount tier
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.
Root Cause
In the POST /api/orders endpoint (server.ts line ~224), when rawTotal is between 500 and 1000, the code tries to find a discount tier with
DISCOUNT_TIERS.find(t => rawTotal >= t.threshold). The DISCOUNT_TIERS array has thresholds at 1000 and 2000, so for orders between 500 and 1000 credits, no tier matches andfind()returnsundefined. The code then accessestier!.percentageonundefined, causing the crash. The fix is to add a guard checking thattieris defined before applying the discount.Reviewer Sign-off
Confidence: 9/10
The fix directly addresses the root cause: when
rawTotalis between 500 and 1000,DISCOUNT_TIERS.find(t => rawTotal >= t.threshold)returnsundefinedbecause no tier has a threshold ≤ rawTotal. The original code usedtier!.percentage(non-null assertion) which crashed. The proposed fix wraps the discount application in anif (tier)guard, so that when no matching tier is found, the discount block is simply skipped andtotalremains equal torawTotal. This is the minimal edit: only lines ~224-227 changed — thetier!non-null assertions are replaced with a conditional check, and thesavingscalculation and log statement are moved inside theif (tier)block. No new abstractions, features, helpers, or behavioral changes are introduced. The coding style (variable naming, log format, use of+(...).toFixed(2)) is fully consistent with the surrounding code. No other code paths are affected. Note: the separate bug inGET /api/orders/:userId(accessingtopOrderwhenuserOrdersis empty) is not touched by this fix, which is correct — the stated root cause is only about the discount tier crash.Log Sample