Skip to content
Back to snapcart-api report
Code QualityMedium

Duplicated Price Logic

src/services/invoice.ts:41

What's going on

Discount computation is copied across checkout and invoice modules and has already drifted by a rounding rule in one of them.

Why it matters

Duplicated business logic guarantees eventual drift, and drift in pricing produces the least trusted kind of bug: invisible and financial.

src/services/invoice.ts:41

Recommended Fix

Replace the inline query with a parameterized statement and validate the id before it ever reaches the database. A regression test is generated alongside the patch.

Before
1// rounded differently than checkout.ts2const total = Math.round(subtotal * 0.9);
After
1const total = applyDiscount(subtotal, tenPercentDiscount()); // one source of truth
  1. Uploaddone
  2. Analyzedone
  3. Finddone
  4. Explainactive
  5. Fixupcoming
  6. Testupcoming
  7. Verifyupcoming