feat(promos): promo codes, admin CRUD and checkout discount (#28) #99

Merged
amiryahaya merged 10 commits from feat/28-promo-codes into main 2026-10-26 12:36:12 +01:00
Owner

Closes #28.

What

  • Admin: /v1/admin/promo-codes (list, create, get, PUT with version, DELETE soft) behind admin_portal.administration.manage. Reads are no-store and include used.
  • Checkout: optional promo_code on POST /v1/me/checkout. Inside the sale transaction: look up (case-insensitive, trimmed), lock the code row, check validity window, park, product, currency (fixed codes) and the use limit, then totals does the maths. The sale records promo_code_id (immutable).
  • Usage limit: a use is a non-cancelled sale carrying the code, counted under a FOR UPDATE row lock (taken only for limited codes); lock order cart -> promo -> sale; 3 s lock_timeout so a hot code gives a fast retryable 503. Cancel/expiry give the use back; an idempotent replay never takes one.
  • 100 % codes make a free sale (GrandTotal == 0 && SubTotal == Discount). A total that only rounds to 0 is still a 422.
  • Every refusal is one 422 on promo_code: "this promo code cannot be used" (no oracle for which codes exist).
  • Migration 0020, ADR 0014, plan docs/superpowers/plans/2026-10-26-promo-codes.md (12 rulings), OpenAPI + live contract tests, CLAUDE.md and ADR 0005/0011 amendments.

Known limits (ADR 0014)

  • Per-ticket discounts are rounded separately from the sale discount: up to 1 cent per ticket difference (totals is golden-tested against legacy, left alone).
  • No preview endpoint (web checkout #40/#41 will need one), no per-user limit, no auto-apply, no minimum order (legacy fields never enforced).
  • The promo e2e test fakes the caller with a header rather than authtest; a 99.9 % code that rounds the total to 0 reports on lines.

Deploy

Migration 0020 only. No worker change, no new environment variables.

Verification

  • make cover: 89.3 % total (minimum 80 %), re-run just before push.
  • make lint 0 issues, make contract ok, go test -race ./... green (concurrency test with -race -count=5).
  • Dual-pass review: Pass 1 (ponytail) no Must fix; Pass 2 found a NUL-byte 500, fixed; Pass 1 re-check of the fixes: Ship.

🤖 Generated with Claude Code

Closes #28. ## What - **Admin:** `/v1/admin/promo-codes` (list, create, get, PUT with `version`, DELETE soft) behind `admin_portal.administration.manage`. Reads are `no-store` and include `used`. - **Checkout:** optional `promo_code` on `POST /v1/me/checkout`. Inside the sale transaction: look up (case-insensitive, trimmed), lock the code row, check validity window, park, product, currency (fixed codes) and the use limit, then `totals` does the maths. The sale records `promo_code_id` (immutable). - **Usage limit:** a use is a non-cancelled sale carrying the code, counted under a `FOR UPDATE` row lock (taken only for limited codes); lock order cart -> promo -> sale; 3 s `lock_timeout` so a hot code gives a fast retryable 503. Cancel/expiry give the use back; an idempotent replay never takes one. - **100 % codes make a free sale** (`GrandTotal == 0 && SubTotal == Discount`). A total that only rounds to 0 is still a 422. - Every refusal is one 422 on `promo_code`: "this promo code cannot be used" (no oracle for which codes exist). - Migration 0020, ADR 0014, plan `docs/superpowers/plans/2026-10-26-promo-codes.md` (12 rulings), OpenAPI + live contract tests, CLAUDE.md and ADR 0005/0011 amendments. ## Known limits (ADR 0014) - Per-ticket discounts are rounded separately from the sale discount: up to 1 cent per ticket difference (`totals` is golden-tested against legacy, left alone). - No preview endpoint (web checkout #40/#41 will need one), no per-user limit, no auto-apply, no minimum order (legacy fields never enforced). - The promo e2e test fakes the caller with a header rather than `authtest`; a 99.9 % code that rounds the total to 0 reports on `lines`. ## Deploy Migration 0020 only. No worker change, no new environment variables. ## Verification - `make cover`: **89.3 %** total (minimum 80 %), re-run just before push. - `make lint` 0 issues, `make contract` ok, `go test -race ./...` green (concurrency test with `-race -count=5`). - Dual-pass review: Pass 1 (ponytail) no Must fix; Pass 2 found a NUL-byte 500, fixed; Pass 1 re-check of the fixes: Ship. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Place takes the code after the cart lock and the idempotency check (cart ->
promo row), refuses every unusable code with one ErrPromo (422 on promo_code),
discounts the covered lines through totals, counts uses under the row lock and
records transactions.promo_code_id. A sale is free when nothing is left to
charge (grand total 0 and sub total = discount).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
test(promos): pin the default 3s lock wait of the promo lookup (#28)
All checks were successful
ci / changes (pull_request) Successful in 33s
ci / clients (pull_request) Successful in 1m17s
ci / api (pull_request) Successful in 22m8s
ci / ci-gate (pull_request) Successful in 3s
662464af35
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!99
No description provided.