Repository navigation
Apply per-line discounts in the cart total #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| export function cartTotal(items: { price: number; qty: number }[]): number { | ||
| export type CartItem = { price: number; qty: number; discountCents?: number }; | ||
|
|
||
| export function cartTotal(items: CartItem[]): number { | ||
| let total = 0; | ||
| for (let i = 0; i < items.length; i++) { | ||
| if (items[i].qty < 0) { | ||
| throw new RangeError(`Negative quantity for item ${i}`); | ||
| } | ||
| total += items[i].price * items[i].qty; | ||
| const line = items[i].price * items[i].qty; | ||
| total += line - (items[i].discountCents ?? 0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixAdd validation to ensure const line = items[i].price * items[i].qty;
if ((items[i].discountCents ?? 0) < 0) {
throw new RangeError(`Negative discount for item ${i}`);
}
if ((items[i].discountCents ?? 0) > line) {
throw new RangeError(`Discount exceeds line total for item ${i}`);
}
total += line - (items[i].discountCents ?? 0);Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| } | ||
| return total; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discount can make cart total negative
High Severity
discountCentsis subtracted from the line in full, socartTotalcan return a negative amount when the discount exceedsprice * qtyor when quantity is zero. Zero-quantity items previously contributed nothing to the total.Reviewed by Cursor Bugbot for commit e5c70d3. Configure here.