Skip to content

Fix crash when order total is between 500-1000: guard against undefined discount tier#5

Draft
kaushik94 wants to merge 1 commit into
mainfrom
fix/auto-fix-crash-when-order-total-is-between-50-1777989842
Draft

Fix crash when order total is between 500-1000: guard against undefined discount tier#5
kaushik94 wants to merge 1 commit into
mainfrom
fix/auto-fix-crash-when-order-total-is-between-50-1777989842

Conversation

@kaushik94
Copy link
Copy Markdown
Contributor

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 and find() returns undefined. The code then accesses tier!.percentage on undefined, causing the crash. The fix is to add a guard checking that tier is defined before applying the discount.

Reviewer Sign-off

Confidence: 9/10
The fix directly addresses the root cause: when rawTotal is between 500 and 1000, DISCOUNT_TIERS.find(t => rawTotal >= t.threshold) returns undefined because no tier has a threshold ≤ rawTotal. The original code used tier!.percentage (non-null assertion) which crashed. The proposed fix wraps the discount application in an if (tier) guard, so that when no matching tier is found, the discount block is simply skipped and total remains equal to rawTotal. This is the minimal edit: only lines ~224-227 changed — the tier! non-null assertions are replaced with a conditional check, and the savings calculation and log statement are moved inside the if (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 in GET /api/orders/:userId (accessing topOrder when userOrders is empty) is not touched by this fix, which is correct — the stated root cause is only about the discount tier crash.

Log Sample

Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)
Unhandled error: Cannot read properties of undefined (reading 'percentage')
TypeError: Cannot read properties of undefined (reading 'percentage')
    at <anonymous> (/Users/kaushik/Desktop/starwars-shop/backend/server.ts:224:41)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/route.js:119:3)
    at patched (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:210:37)
    at Layer.handle [as handle_request] (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/layer.js:95:5)
    at /Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/express/lib/router/index.js:280:10)
    at arguments.<computed> (/Users/kaushik/Desktop/starwars-shop/backend/node_modules/@opentelemetry/instrumentation-express/build/src/instrumentation.js:206:41)

Opened automatically by the RocketGraph AI-SRE agent.
Reviewer agent signed off before this PR was created.

Fix crash when order total is between 500-1000: guard against undefined discount tier
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