Add POST /api/v1/accounts to create accounts

CO
10d ago
+1066-4

Problem

The accounts API is read-only (index/show). A client can post transactions and valuations but cannot create the account to post them to, so scripts and integrations have to fall back to the web UI for that one step.

Change

Adds POST /api/v1/accounts (requires the read_write scope).

  • Goes through Account.create_and_sync, the same path the web forms use, so opening balances and loan anchoring (accountable_attributes.initial_balance + start_date) behave identically.
  • accountable_type accepts Loan or loan / credit_card / CreditCard etc., validated against Accountable::TYPES.
  • Type-specific attributes are permitted per type from each web controller's own permitted_accountable_attributes, so the API cannot set anything the UI cannot.
  • The account is owned by the key's user; currency defaults to the family currency.
  • Errors: 401 unauthenticated, 403 insufficient scope, 422 for a missing name/balance, unknown type, invalid opening_balance_date, or model validation errors.

Validation

  • New Minitest coverage in test/controllers/api/v1/accounts_controller_test.rb (auth, read-only key 403, depository, credit card, loan with terms, family-currency default, and the 422 cases).
  • Documentation-only rswag spec added; docs/api/openapi.yaml regenerated.
  • Full bin/rails test: 19,291 runs, 0 failures. 8 errors, all ReleaseNotesWorkflowTest failing with ENOENT: node because the container I ran in had no Node installed.
  • RuboCop, Brakeman and test/support/verify_api_endpoint_consistency.rb clean.
  • Not run: system tests.

Summary by cubic

Adds POST /api/v1/accounts so API clients can create accounts directly instead of through the web UI.

The endpoint requires the read_write scope, is blocked for guests, and routes through Account.create_and_sync (the same path web forms use) so opening balances and loan anchoring behave identically. Only the attributes the matching web form permits are accepted; the account is owned by the key's user, and currency defaults to the family currency.

  • Every field is parsed strictly as the type its column holds before persisting: wrong types, non-finite or out-of-range numbers, oversized exponents like 1e-1000000000, excess decimal places, bad dates, unknown subtypes/enums, and inconsistent loan opening-balance details all return 422 naming the field. null and empty strings mean "not sent", including for whole objects and lists.
  • Returns 401 for missing auth, 403 for read-only keys and guests, 400 for a missing account object, and 422 for any other malformed input.
  • Only the fields the client sent are locked on the new record—including the account's own name, institution and notes and the loan's variable-rate schedule—so a later provider refresh or enrichment can't overwrite them; balance, currency and the rest stay managed by the app. If scheduling the first sync fails after the record is saved, the request still answers 201 and the failure is logged, so a retry won't create a second account.

Summary by CodeRabbit

  • New Features
    • Added API support for creating accounts with required account details and balances, plus optional type-specific attributes. Currency defaults to the family currency when omitted.
    • Account creation requires write authorization and is unavailable to guest keys. Successful requests return the created account; invalid or unsupported input returns a validation error.
  • Documentation
    • Documented the account creation endpoint, required fields, optional attributes, authorization scope, loan opening-balance details, and response codes.