Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions apps/pr-reviewer/fixtures/demo-pr/cart-total.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { applyDiscount } from './discount.ts';
import { shippingCost } from './shipping.ts';

export function cartTotal(items: { price: number; qty: number }[]): number {
export function cartTotal(
items: { price: number; qty: number }[],
discountPercent = 0,
): 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;
}
return total;
return applyDiscount(total, discountPercent);
}

export type Cart = {
Expand All @@ -25,7 +28,7 @@ function roundCents(amount: number): number {
}

export function checkoutTotal(cart: Cart): number {
const subtotal = cartTotal(cart.items);
const subtotal = cartTotal(cart.items, cart.discount?.percent);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discount applied twice at checkout

High Severity

checkoutTotal now passes the cart's discount into cartTotal, which applies it to the item subtotal, but still applies the same percent again to the final amount. One code therefore compounds, so checkout charges less than the intended discounted price.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 849746d. Configure here.

const shipping = shippingCost(cart.weightKg, cart.express);
const freeShipping = subtotal >= 100;
const shippingCharge = freeShipping ? 0 : shipping;
Expand Down
Loading