Skip to content
Open
Show file tree
Hide file tree
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: 9 additions & 0 deletions src/summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { applyDiscount, subtotal } from "./cart.js";
import { salesTax } from "./tax.js";

export function orderSummary(items, { discountPercent = 0, taxRatePercent = 0 } = {}) {
const itemsTotal = subtotal(items);
const discounted = applyDiscount(itemsTotal, discountPercent);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 Important

applyDiscount(itemsTotal, discountPercent) returns Math.round(amount * (100 - percent)) / 100...

applyDiscount(itemsTotal, discountPercent) returns Math.round(amount * (100 - percent)) / 100, which is not an integer cent value when the product is not a multiple of 100. For example, an items total of 2001 cents with a 10% discount gives discounted = 1800.9, and total becomes fractional (1980.9) as well. This happens for any cart whose total times (100 - percent) is not divisible by 100, so it is common. Since the README uses integer cents, either round to whole cents here or fix applyDiscount to round after dividing (Math.round(amount * (100 - percent) / 100)). The added test only covers a round-number case and would not catch this.

🤖 Prompt for AI agents
Pull request mergestorm/sample-app#4: https://github.com/mergestorm/sample-app/pull/4 (head branch `order-summary`)

Verify each finding against current code. Fix only still-valid concrete bugs, skip the
rest with a brief reason, keep changes minimal, and validate. Skip Decision required,
policy forks, and "consider X" alternatives. Do not add new features, refactors, or
architecture beyond the fix; prefer the smallest diff.

In `@src/summary.js` (line 6, Important):
`applyDiscount(itemsTotal, discountPercent)` returns `Math.round(amount * (100 - percent)) / 100`, which is not an integer cent value when the product is not a multiple of 100. For example, an items total of 2001 cents with a 10% discount gives `discounted` = 1800.9, and `total` becomes fractional (1980.9) as well. This happens for any cart whose total times (100 - percent) is not divisible by 100, so it is common. Since the README uses integer cents, either round to whole cents here or fix `applyDiscount` to round after dividing (`Math.round(amount * (100 - percent) / 100)`). The added test only covers a round-number case and would not catch this.

const tax = salesTax(discounted, taxRatePercent);
return { itemsTotal, discounted, tax, total: discounted + tax };
}
8 changes: 8 additions & 0 deletions src/summary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { orderSummary } from "./summary.js";

test("orderSummary applies the discount before tax", () => {
const summary = orderSummary([{ price: 1000, quantity: 2 }], { discountPercent: 10, taxRatePercent: 10 });
assert.deepEqual(summary, { itemsTotal: 2000, discounted: 1800, tax: 180, total: 1980 });
});
Loading