feat(api): expose kind, exact amount and split state on transactions
Merged
BH
1mo ago+237+237+012345678901234567890123456789-20-20-01234567890123456789
Problem
API clients can't get three things from the transaction JSON:
- Exact amount.
amountandamount_centsare rounded to the currency's display precision, but entries storedecimal(19,4)andEntry#split!needs the children to add up to the stored value. Split show returnsamount_decimal, but only once a transaction is split. - Kind. Budgets count
loan_payment(and, by default,investment_contribution) as expenses and leave outTransaction::BUDGET_EXCLUDED_KINDS. Withoutkinda client only hasclassification, which follows the amount's sign, so it can't tell these rows from income. - Split and exclusion state. A list doesn't say which rows are split parents or excluded, so a client that sums it counts a split twice.
What changed
Every response that renders the transaction partial (index, show, create, update, and the children in split show) gets four fields. Existing fields don't change.
| Field | Value |
|---|---|
amount_decimal | entry.amount.to_s("F"): the stored amount, unrounded, fixed-point and locale-independent, in the entry's sign convention (positive = outflow). Same field split show already returned. |
kind | transaction.kind (one of Transaction.kinds). |
excluded | entry.excluded?, so a NULL stored flag is false. |
split_parent | Whether the entry has split children. |
split_parentwithout N+1: the index looks up which entries on the page have children in one query (Entry.split_parent_ids_among) and passes the set to the partial; it never loads the children. The web transactions list and the account activity feed already ran the same query inline, and now call this method. A single transaction (show/create/update) falls back toEntry#split_parent?(one EXISTS). Split show passes an empty set for its children, which can never be split parents, so it runs no lookup.- Split show's children used to add
amount_decimalandexcludedafter the partial; the partial now emits both, so the response is unchanged. Transactioninspec/swagger_helper.rblists the new properties as required (enum fromTransaction.kinds.keys);docs/api/openapi.yamlis regenerated, anddocs/api/transactions.mdgains a table explaining the amount fields,kind(including the family's investment-contributions setting) and the two flags.
No schema changes, and no behaviour changes for current clients.
How it was tested
- Minitest in
test/controllers/api/v1/transactions_controller_test.rb:amount_decimalkeeps 4 decimals ("12.3456") whileamount_centsstays rounded (1235), keeps its sign, and isn't localized withAccept-Language: de("-1234.5").kindis returned (loan_payment,standard).split_parent/excludedare right for a plain row, a split parent, a counted child and an excluded child, on index and on show (the fallback path).- The index query count stays flat as split parents are added, and it never loads the children.
- A NULL
excludedis returned asfalseon show and index.
- Targeted: API transactions + splits +
Entrymodel (110 runs), web transactions + accounts controllers (203 runs): 0 failures. - Full
bin/rails test: 13,234 runs, 0 failures, 0 errors, 36 skips - rswag: regenerating
docs/api/openapi.yamlproduces no diff; the request specs fail only where they fail identically onmainin my environment. bin/rubocop(changed files) clean,bin/brakeman0 warnings,ruby test/support/verify_api_endpoint_consistency.rbpasses.
This replaces #4329, which GitHub auto-closed after an accidental force-push; its review threads are there. The companion PR #4423 adds the list filters; the two are independent and merge in either order.
Summary by CodeRabbit
-
New Features
- Transaction API responses now include exact decimal amounts, transaction kinds, exclusion status, and split-parent indicators.
- Exact amounts preserve stored values and sign conventions independently of display rounding or locale.
- Transaction kinds describe how transactions are treated in budgets and reports.
- Split transactions expose parent and child relationships consistently; null exclusion values are returned as
false.
-
Documentation
- Updated API documentation and OpenAPI schemas explain amount rounding, sign conventions, transaction kinds, exclusions, and split behavior.