~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/checkout-demo

A demo of knotless's review agent

0
0

Orders take a discount code

Open
Open chadtmiller.com wants to merge into main from chadtmiller.com/checkout-demo:discount-codes
1 reviewer 1 asked for changes

What changes

place_order takes an optional discount code. A code takes a percentage off the subtotal, or a fixed number of cents, until it expires.

chadtmiller.com added 1 commit
Asked for changes to an earlier version

This adds an optional discount code to place_order. It has bugs that charge customers the wrong amount, and it breaks two rules in AGENTS.md.

  1. Expiry check is inverted. Valid codes are ignored, and expired codes are applied.
  2. Percentage discount is a float. amount * value / 100 returns a float. That puts a float into orders.total and breaks the integer-cents rule.
  3. Total can go negative. A fixed discount larger than the subtotal makes the total negative. Cap the discount at the subtotal.
  4. SQL injection. The code is formatted into the SQL string. AGENTS.md requires parameters.
  5. No tests. AGENTS.md requires a test in test_checkout.py for any change to checkout.py. Cover a percent code, a fixed code, an expired code, an unknown code, and a discount larger than the subtotal.
checkout.pyline 22Outdated
return 0
kind, value, expires = row
if date.fromisoformat(expires) > date.today():
return 0
review.knotless.dev

Inverted. This returns 0 when expires is after today, so a valid code gives no discount. An expired code passes through and is applied. It should be expires < today, or <= depending on whether the expiry day is inclusive. The docstring says "until the day it expires".

checkout.pyline 24Outdated
if date.fromisoformat(expires) > date.today():
return 0
if kind == "percent":
return amount * value / 100
review.knotless.dev

amount * value / 100 is a float, so the order total becomes a float. Use integer math, e.g. amount * value // 100, and decide on the rounding. Also cap the result with min(..., amount), since the fixed branch below can exceed the subtotal and give a negative total.

checkout.pyline 16Outdated
"""What a discount code takes off `amount`: a percentage, or a fixed
number of cents, until the day it expires."""
row = db.execute(
f"SELECT kind, value, expires FROM discounts WHERE code = '{code}'"
review.knotless.dev

SQL injection: code is formatted into the query. Use WHERE code = ? with (code,).

Asked for changes

Most of round 1 is fixed. The expiry check is no longer inverted, the percentage uses integer division, the fixed discount is capped at the subtotal, and the query is parameterized. One item is still open, and I found one new edge case.

  1. Still no tests. The diff only touches checkout.py. AGENTS.md requires a test in test_checkout.py for any change to checkout.py. Please add tests for a percent code, a fixed code, an expired code, an unknown code, and a fixed discount larger than the subtotal.
  2. A percent value over 100 makes the total negative. The percent branch isn't capped like the fixed branch is. If value is 150, the discount is 1.5× the subtotal, so the order total goes below zero. Return min(amount * value // 100, amount), or reject such rows. Add a test for it.
checkout.pyline 24
if date.fromisoformat(expires) < date.today():
return 0
if kind == "percent":
return amount * value // 100
review.knotless.dev

No cap here: a percent value above 100 gives a discount larger than amount, and the total goes negative. Wrap it in min(..., amount).

Checking that the page updates live.

Checking again that the page updates live.

A third live check.

Browser live check 1793377303410

Browser live check 1793377367844

A compressed live check.

Browser live check 1793377468992

A zstd live check.

Another zstd live check.

Changes requested

1 review asking for changes