Fix crash when order total is between 500-1000 credits (no matching discount tier)#8
Draft
kaushik94 wants to merge 1 commit into
Draft
Conversation
Fix crash when order total is between 500-1000 credits (no matching 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
At line 225 of server.ts, inside the POST /api/orders handler, when rawTotal is between 500 and 1000, the code does
DISCOUNT_TIERS.find(t => rawTotal >= t.threshold)which looks for a tier where rawTotal >= threshold. For orders between 500 and 999, rawTotal is less than the first threshold of 1000, sofind()returnsundefined. The code then accessestier!.percentageon undefined, causing the crash. The fix is to add a guard: only apply the discount if a matching tier is found.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 neither tier's threshold (1000, 2000) is met. The original code usedtier!.percentage(non-null assertion) on the potentially-undefined result, causing the crash. The fix wraps the discount application in anif (tier)guard, sotier.percentageis only accessed when a matching tier actually exists. When no tier matches,totalremains equal torawTotal— no discount is applied, which is the correct behavior for orders below the lowest threshold. The change is minimal: only lines 225–228 are modified by (1) addingif (tier) {after thefind(), (2) removing the!non-null assertions fromtier!.percentage, and (3) closing the block with}. No new abstractions, patterns, or behaviors are introduced. The coding style (variable naming,console.logformat, numeric formatting with+(...).toFixed(2)) is consistent with the rest of the file. No other code paths are affected.Log Sample