Skip to content

#293 support limit query param in /txns.json#517

Open
edmoffo wants to merge 1 commit into
zold-io:masterfrom
edmoffo:293-txns-limit-param
Open

#293 support limit query param in /txns.json#517
edmoffo wants to merge 1 commit into
zold-io:masterfrom
edmoffo:293-txns-limit-param

Conversation

@edmoffo

@edmoffo edmoffo commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Closes #293.

The /txns.json route serialised the full wallet transaction history on every call, which the linked issue measured at roughly 17 seconds for a 715-transaction wallet when only the most recent few rows were needed.

The change accepts an optional limit query parameter, parses it as an integer after the optional sort=desc reverse, and slices the list down to the first limit entries before serialising. A non-positive value short-circuits with WTS::UserError E207 so a typo never silently returns the entire history.

Locally ruby -c wts.rb reports Syntax OK and bundle exec rubocop wts.rb reports no offences against the project config. The full rake target was not executed in this environment because it requires a live PostgreSQL pool via DATABASE_URL; the existing smoke test in test/test_wts.rb already hits /txns.json without a limit parameter and the new branch leaves that no-parameter path unchanged.

The /txns.json endpoint returns the full wallet transaction
history on every call. Wallets with hundreds of transactions
make this slow when a caller only wants the most recent few.
Accept an optional `limit` parameter and slice the list down
to the first `limit` entries before serialising, mirroring the
shape requested in the issue (`/txns?limit=10`). Reject
non-positive values with a UserError so an empty or malformed
parameter does not silently return everything.

@kreinba kreinba left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes. The reused E207 error code clashes with the cash-out path in front/front_upwork.rb:26 and must be renumbered before merge; the new limit branch also ships without a test for either the happy path or the rejected-zero path. Two inline comments below name the two defects. The two failing CI checks (typos on objects/referrals.rb and yamllint on .github/workflows/actionlint.yml) point at files outside this diff and so are not caused by this change.

Comment thread wts.rb
list.reverse! if params[:sort] && params[:sort] == 'desc'
if params[:limit]
limit = params[:limit].to_i
raise WTS::UserError, "E207: The 'limit' parameter must be a positive integer" if limit <= 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error code E207 is already taken by front/front_upwork.rb:26, which raises WTS::UserError on a different condition (You have to work in Zerocracy in order to cash out to UpWork). Reusing the same code on a new failure path breaks the contract that callers and operators rely on to identify which user-facing error fired. Pick the next free code in the sequence; E227 appears to be the next unused slot after E226 in objects/ops.rb.

Comment thread wts.rb
confirmed_user.wallet do |wallet|
list = wallet.txns
list.reverse! if params[:sort] && params[:sort] == 'desc'
if params[:limit]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new limit branch is not exercised by any test. The smoke test in test/test_wts.rb only hits /txns.json without a parameter, so a regression in the parse, the bound check, or the first(limit) slice would land silently. Add a case that calls /txns.json?limit=1 and asserts the response is a JSON array of length at most one, and a case that asserts a 4xx response for limit=0 or a negative value so the new WTS::UserError is covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/txns API limit param

2 participants