feat(api): invoice, check-in and itinerary number sequences (#25) #93

Merged
amiryahaya merged 1 commit from feat/25-number-sequences into main 2026-10-25 05:51:12 +01:00
Owner

Closes #25. Implements R-TOUR-1 for all three counters. Nothing calls it yet; #26 will.

What

  • Migration 0013: number_sequences, one row per (kind, year), unique on that pair.
  • internal/numbering: the legacy formats (invoice YY + 8 digits, check-in C + YY + 8 digits, itinerary I + YY + 8 digits), the Kenya-time year, ErrUnknownKind, ErrYearOutOfRange (2000-2099).
  • numbering/pgstore: Next(ctx, kind, now) takes a number with one atomic INSERT ... ON CONFLICT DO UPDATE ... RETURNING in its own short transaction. Concurrent callers on any node never get the same number. A number is skipped if the sale that asked for it rolls back (gaps are allowed).

Decisions (agreed on the issue)

  • Legacy formats kept exactly; each counter restarts at 1 every January.
  • The year is the Kenya (UTC+3) calendar year. The legacy code used UTC, so its year switched 3 hours late.
  • Why the legacy code was unsafe: it read, added 1 and saved with no row lock and no unique index on the year, and the concurrency column is NULL in production data.

Checks

  • make lint: 0 issues. make contract: pass.
  • make cover: total 94.4% (minimum 80%); numbering 100%, numbering/pgstore 91.7%.
  • Tests: legacy format strings, 50 concurrent callers get exactly 1..50, the 31 Dec / 1 Jan boundary, independent kinds, unknown kind and out-of-range year rejected without writing.

Review

Dual pass per the review rule. Ponytail: no Must fix (two Should-fix items applied). Independent reviewer: "with fixes", nothing Critical; the year-range guard and a doc line were applied.

Follow-ups (not in this PR)

  • Cutover: the counters start at 1, so go-live mid-year could re-issue numbers legacy already issued. Seed them from the legacy maximum in #64.
  • #26: call Next before opening the sale transaction (it needs its own pool connection), and add a unique index on the invoice number column. A sequential number must never be used as an access token for public lookup.

🤖 Generated with Claude Code

Closes #25. Implements R-TOUR-1 for all three counters. Nothing calls it yet; #26 will. ## What - Migration 0013: `number_sequences`, one row per (kind, year), unique on that pair. - `internal/numbering`: the legacy formats (invoice `YY` + 8 digits, check-in `C` + `YY` + 8 digits, itinerary `I` + `YY` + 8 digits), the Kenya-time year, `ErrUnknownKind`, `ErrYearOutOfRange` (2000-2099). - `numbering/pgstore`: `Next(ctx, kind, now)` takes a number with one atomic `INSERT ... ON CONFLICT DO UPDATE ... RETURNING` in its own short transaction. Concurrent callers on any node never get the same number. A number is skipped if the sale that asked for it rolls back (gaps are allowed). ## Decisions (agreed on the issue) - Legacy formats kept exactly; each counter restarts at 1 every January. - The year is the Kenya (UTC+3) calendar year. The legacy code used UTC, so its year switched 3 hours late. - Why the legacy code was unsafe: it read, added 1 and saved with no row lock and no unique index on the year, and the concurrency column is NULL in production data. ## Checks - `make lint`: 0 issues. `make contract`: pass. - `make cover`: total 94.4% (minimum 80%); `numbering` 100%, `numbering/pgstore` 91.7%. - Tests: legacy format strings, 50 concurrent callers get exactly 1..50, the 31 Dec / 1 Jan boundary, independent kinds, unknown kind and out-of-range year rejected without writing. ## Review Dual pass per the review rule. Ponytail: no Must fix (two Should-fix items applied). Independent reviewer: "with fixes", nothing Critical; the year-range guard and a doc line were applied. ## Follow-ups (not in this PR) - Cutover: the counters start at 1, so go-live mid-year could re-issue numbers legacy already issued. Seed them from the legacy maximum in #64. - #26: call `Next` before opening the sale transaction (it needs its own pool connection), and add a unique index on the invoice number column. A sequential number must never be used as an access token for public lookup. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feat(api): invoice, check-in and itinerary number sequences (#25)
All checks were successful
ci / changes (pull_request) Successful in 10s
ci / clients (pull_request) Has been skipped
ci / api (pull_request) Successful in 6m21s
ci / ci-gate (pull_request) Successful in 3s
c1c477959c
One counter row per (kind, year), taken with a single atomic upsert in its own
short transaction: never MAX()+1, unique across nodes, gaps allowed (R-TOUR-1).
Keeps the legacy formats (YY+8 digits, C-prefix check-in, I-prefix itinerary),
restarts every January, and takes the year in Kenya time instead of UTC.
Years outside 2000-2099 are rejected. Nothing calls it yet; #26 will.

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!93
No description provided.