Skip to content
Closed
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 still runs applyDiscount on the post-tax total after already passing cart.discount.percent into cartTotal, so the same code is taken off twice and customers are undercharged.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 22aaa44. Configure here.

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