feat(api): expose kind, exact amount and split state on transactions

Closed
BH
1mo ago
+0-0

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. amount and amount_cents are rounded to the currency's display precision, but entries store decimal(19,4) and Entry#split! needs the children to add up to the stored value. Split show returns amount_decimal, but only after a transaction is split (#4325, thread).
  • Kind. Budgets count loan_payment and investment_contribution as expenses and leave out Transaction::BUDGET_EXCLUDED_KINDS. Without kind, a client can only use classification, 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.

FieldValue
amount_decimalentry.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.
kindtransaction.kind (one of Transaction.kinds).
excludedentry.excluded.
split_parentWhether the entry has child entries.
  • The index and split show preload entry: :child_entries, so split_parent doesn'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_decimal and excluded after the partial. The partial now emits both, so those two lines are removed. The response is the same.
  • Transaction in spec/swagger_helper.rb lists the new properties as required, with the enum taken from Transaction.kinds.keys. docs/api/openapi.yaml is regenerated, and docs/api/transactions.md gains a table explaining the amount fields, kind and 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_decimal keeps 4 decimals ("12.3456") while amount_cents stays rounded (1235).
    • amount_decimal keeps the sign and isn't localized when sent Accept-Language: de ("-1234.5").
    • kind is returned (loan_payment, standard).
    • split_parent and excluded are 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 send amount_decimal and excluded back still pass with the shared partial.
  • The full test/controllers/api/v1 suite passes.
  • bin/rubocop reports no offenses on the changed files, bin/brakeman finds no warnings, and ruby test/support/verify_api_endpoint_consistency.rb passes.
  • 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 false when unset.