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

Merged
BH
1mo ago
+237-20

Problem

API clients can't get three things from the transaction JSON:

  • 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 once a transaction is split.
  • Kind. Budgets count loan_payment (and, by default, investment_contribution) as expenses and leave out Transaction::BUDGET_EXCLUDED_KINDS. Without kind a client only has classification, 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.

FieldValue
amount_decimalentry.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.
kindtransaction.kind (one of Transaction.kinds).
excludedentry.excluded?, so a NULL stored flag is false.
split_parentWhether the entry has split children.
  • split_parent without 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 to Entry#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_decimal and excluded after the partial; the partial now emits both, so the response is unchanged.
  • Transaction in spec/swagger_helper.rb lists the new properties as required (enum from Transaction.kinds.keys); docs/api/openapi.yaml is regenerated, and docs/api/transactions.md gains 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_decimal keeps 4 decimals ("12.3456") while amount_cents stays rounded (1235), keeps its sign, and isn't localized with Accept-Language: de ("-1234.5").
    • kind is returned (loan_payment, standard).
    • split_parent / excluded are 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 excluded is returned as false on show and index.
  • Targeted: API transactions + splits + Entry model (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.yaml produces no diff; the request specs fail only where they fail identically on main in my environment.
  • bin/rubocop (changed files) clean, bin/brakeman 0 warnings, ruby test/support/verify_api_endpoint_consistency.rb passes.

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.