feat(api): expose kind, exact amount and split state on transactions
Closed
BH
1mo ago+0+0+0123456789-0-0-0123456789
Problem
API clients can't get three things from the transaction JSON, so the mobile PRs can't fix some review findings on the client side:
- 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 after a transaction is split (#4325, thread). - Kind. Budgets count
loan_paymentandinvestment_contributionas expenses and leave outTransaction::BUDGET_EXCLUDED_KINDS. Withoutkind, a client can only useclassification, which follows the amount's sign, so it can't tell these rows from income (#4324, thread). - Split and exclusion state. A list has no way to show which rows are split parents or excluded, so a client that sums it counts a split twice (#4326, thread).
What changed
Every response that uses the transaction partial (index, show, create, update, and split show children) gets four new fields. Existing fields don't change.
| Field | Value |
|---|---|
amount_decimal | entry.amount.to_s: the stored amount, unrounded and independent of locale, in the entry's sign convention (positive = outflow). Split show already uses this field. |
kind | transaction.kind (one of Transaction.kinds). |
excluded | entry.excluded. |
split_parent | Whether the entry has child entries. |
- The index and split show preload
entry: :child_entries, sosplit_parentdoesn't add a query per row. A test checks that the query count stays flat as split parents are added. - In split show, the children used to add
amount_decimalandexcludedafter the partial. The partial now emits both, so those two lines are removed. The response is the same. Transactioninspec/swagger_helper.rblists the new properties as required, with the enum taken fromTransaction.kinds.keys.docs/api/openapi.yamlis regenerated, anddocs/api/transactions.mdgains a table explaining the amount fields,kindand the two flags.
There are no schema changes and no behaviour changes for current clients.
How it was tested
- New Minitest coverage in
test/controllers/api/v1/transactions_controller_test.rb:amount_decimalkeeps 4 decimals ("12.3456") whileamount_centsstays rounded (1235).amount_decimalkeeps the sign and isn't localized when sentAccept-Language: de("-1234.5").kindis returned (loan_payment,standard).split_parentandexcludedare correct for a plain row, a split parent, a counted child and an excluded child.- There's no N+1 on
split_parent. I checked that this test fails without the preload.
bin/rails test test/controllers/api/v1/transactions_controller_test.rb test/controllers/api/v1/splits_controller_test.rb: 92 runs, 0 failures. The existing split tests that sendamount_decimalandexcludedback still pass with the shared partial.- The full
test/controllers/api/v1suite passes. bin/rubocopreports no offenses on the changed files,bin/brakemanfinds no warnings, andruby test/support/verify_api_endpoint_consistency.rbpasses.- OpenAPI regenerated with
RAILS_ENV=test bundle exec rake rswag:specs:swaggerize.
This unblocks the remaining review threads on the mobile PRs #4324, #4325 and #4326. The companion PR "feat(api): budget and report filters for the transactions index" adds the list filters. The two PRs are independent, merge cleanly in either order, and pass together.
Summary by CodeRabbit
-
New Features
- Transaction API responses now include exact decimal amounts, transaction kind, exclusion status, and split-parent status.
- Amount signs are preserved consistently, while formatted amounts remain rounded for display.
- Split transactions provide clearer parent and child metadata, including amounts carried by child entries.
-
Documentation
- Updated API schemas and examples describe the new fields, rounding behavior, sign conventions, and transaction kinds.
-
Bug Fixes
- Improved split-transaction detection and loading efficiency.
- Exclusion status is consistently returned as
falsewhen unset.