feat(topup): ticket top-up (upgrade) sales (#36) #106

Merged
amiryahaya merged 7 commits from feat/36-ticket-topup into main 2026-10-27 09:05:20 +01:00
Owner

Ticket top-up (upgrade) slice of #36, the last one. Closes the remaining part of #36 (refunds #100, overstay #104 and vehicle add-on #105 already merged).

What it does

  • Gate staff upgrade unused visitor tickets to a dearer product of the same park: POST /v1/admin/gates/{id}/ticket-top-ups (Idempotency-Key required, permission admin_portal.tickets.scan). It creates a child sale type = 'topup_ticket' for the difference (every day of the stay in the parent's currency, minus what the old ticket was paid; no promo, tax or fee). The customer pays (wallet or M-Pesa) or Finance waives it.
  • On completion the old tickets are cancelled (checkin.Service.CancelReplaced, inside Completer.Complete, before the status change) and the new tickets are issued into the parent sale under the same master with replaces_ticket_id, so check-in, overstay and add-ons work on them unchanged. A ticket used or expired before payment cancels the sale and the payment becomes refund_due.
  • Nothing changes on the tickets until the top-up is paid (legacy deactivated the old ticket at creation, so an unpaid top-up left a dead ticket).
  • Migration 0024, ADR 0020, OpenAPI operation createTicketTopup plus live contract test, CLAUDE.md "Ticket top-up" section.
  • LockSale is now FOR NO KEY UPDATE: found in review, a wallet-paid top-up and a Finance refund of the parent's payment could deadlock (FK key-share on the parent vs FOR UPDATE). A race test forces the interleaving; it failed with 40P01 before the change.

Owner decisions (approved defaults)

Credit = what was paid (after promo); whole stay priced (deliberate fix: legacy charged the last day only; the 1-day legacy golden case still matches); cheaper or equal refused (422); no upgrade after check-in; staff only (no customer self-upgrade); old ticket becomes cancelled; limited-seat target products refused and an old limited seat is not released; vehicle tickets not upgradable.

Known limits (ADR 0020)

No staff-side payment (#48); no downgrade or refund of a difference; form answers not carried (only holder name; not built yet); an expired-but-pending top-up reads as pending for up to a minute until sales.expire runs; a ticket's transaction_id is the visit's sale, so reports (#38) must count top-up lines rather than ticket prices.

Review (dual pass)

  • Pass 1 (ponytail): no Must fix. One Should fix (purchase-email query could not use an index; fixed). Re-checked after the fix wave: no Must fix.
  • Pass 2 (correctness): NOT APPROVED on first read, two Important (the email query seq-scan, confirmed with EXPLAIN; the lock cycle above) plus minors (expired old ticket could complete an M-Pesa top-up; ticket code in a trigger error text; duplicate line). All fixed with tests in d99a31d-equivalent commit.
  • Skipped on purpose: an extra SELECT type in InsertTickets and one unreachable guard.

Verification

go test -race -count=1 ./... green (47 packages), make lint 0 issues, make contract ok, clients generated, migration 0024 down/up round trip passes. make cover: total coverage 90.4% (minimum 80%).

Deploy

Run migration 0024 (KWS_MODE=migrate). No new environment variables. The worker must be running (as for checkout); API and worker both wire the check-in service into the completer.

🤖 Generated with Claude Code

Ticket top-up (upgrade) slice of #36, the last one. Closes the remaining part of #36 (refunds #100, overstay #104 and vehicle add-on #105 already merged). ## What it does - Gate staff upgrade unused visitor tickets to a dearer product of the same park: `POST /v1/admin/gates/{id}/ticket-top-ups` (`Idempotency-Key` required, permission `admin_portal.tickets.scan`). It creates a child sale `type = 'topup_ticket'` for the difference (every day of the stay in the parent's currency, minus what the old ticket was paid; no promo, tax or fee). The customer pays (wallet or M-Pesa) or Finance waives it. - On completion the old tickets are cancelled (`checkin.Service.CancelReplaced`, inside `Completer.Complete`, before the status change) and the new tickets are issued into the **parent sale** under the same master with `replaces_ticket_id`, so check-in, overstay and add-ons work on them unchanged. A ticket used or expired before payment cancels the sale and the payment becomes `refund_due`. - Nothing changes on the tickets until the top-up is paid (legacy deactivated the old ticket at creation, so an unpaid top-up left a dead ticket). - Migration 0024, ADR 0020, OpenAPI operation `createTicketTopup` plus live contract test, CLAUDE.md "Ticket top-up" section. - `LockSale` is now `FOR NO KEY UPDATE`: found in review, a wallet-paid top-up and a Finance refund of the parent's payment could deadlock (FK key-share on the parent vs `FOR UPDATE`). A race test forces the interleaving; it failed with `40P01` before the change. ## Owner decisions (approved defaults) Credit = what was paid (after promo); whole stay priced (deliberate fix: legacy charged the last day only; the 1-day legacy golden case still matches); cheaper or equal refused (422); no upgrade after check-in; staff only (no customer self-upgrade); old ticket becomes `cancelled`; limited-seat target products refused and an old limited seat is not released; vehicle tickets not upgradable. ## Known limits (ADR 0020) No staff-side payment (#48); no downgrade or refund of a difference; form answers not carried (only holder name; not built yet); an expired-but-pending top-up reads as pending for up to a minute until `sales.expire` runs; a ticket's `transaction_id` is the visit's sale, so reports (#38) must count top-up lines rather than ticket prices. ## Review (dual pass) - Pass 1 (ponytail): no Must fix. One Should fix (purchase-email query could not use an index; fixed). Re-checked after the fix wave: no Must fix. - Pass 2 (correctness): NOT APPROVED on first read, two Important (the email query seq-scan, confirmed with EXPLAIN; the lock cycle above) plus minors (expired old ticket could complete an M-Pesa top-up; ticket code in a trigger error text; duplicate line). All fixed with tests in d99a31d-equivalent commit. - Skipped on purpose: an extra `SELECT type` in `InsertTickets` and one unreachable guard. ## Verification `go test -race -count=1 ./...` green (47 packages), `make lint` 0 issues, `make contract` ok, clients generated, migration 0024 down/up round trip passes. **`make cover`: total coverage 90.4% (minimum 80%).** ## Deploy Run migration 0024 (`KWS_MODE=migrate`). No new environment variables. The worker must be running (as for checkout); API and worker both wire the check-in service into the completer. 🤖 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>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
docs(topup): CLAUDE.md rules for ticket top-ups (#36)
All checks were successful
ci / changes (pull_request) Successful in 10s
ci / clients (pull_request) Successful in 2m50s
ci / api (pull_request) Successful in 16m12s
ci / ci-gate (pull_request) Successful in 3s
e92f98c313
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!106
No description provided.