fix(bills): explain why a used-up transaction cannot pay a bill

Merged
HO
1mo ago
+91-1

Problem

On a bill's occurrence dialog, picking a transaction to link sometimes fails with the generic flash "The payment could not be recorded", with no hint of the cause.

The picker deliberately keeps transactions that other occurrences already use, because one charge can pay two bills (see the comment on RecurringOccurrencesController#ranked_candidates). Over-allocation is meant to be refused at write time. But when such a transaction is already used in full, for example auto-matched to the previous cycle of the same bill, the write path fails in the wrong place:

  1. With no amount given, Allocator#resolve_amounts defaults to what the entry has left, which is 0.
  2. guard_entry_capacity! compares 0 > capacity + CLOSE_EPSILON, which is false, so it lets the zero through.
  3. RecurringAllocation validates allocated_amount > 0, create! raises ActiveRecord::RecordInvalid, and RecurringAllocationsController maps that to the generic recurring_allocations.invalid message.

Changes

  • Two subclasses of OverAllocationError in RecurringTransaction::Allocator. guard_entry_capacity! raises one of them when nothing of the entry is left, before the existing over-allocation comparison:
    • EntryExhaustedError when other occurrences hold the entry;
    • AlreadyAllocatedError when this occurrence already holds it, for example after a double submit or from a tab left open after the first link.
  • RecurringAllocationsController#allocation_error_message maps EntryExhaustedError to a new recurring_allocations.entry_exhausted message: "That transaction's full amount is already applied to other payments due". AlreadyAllocatedError gets the existing recurring_allocations.already_allocated message, the same one the unique index produces when the entry still has capacity. Both are checked before OverAllocationError, which would otherwise match them.
  • New keys in en and fr: recurring_allocations.entry_exhausted, plus recurring_transactions.allocator.entry_exhausted and recurring_transactions.allocator.already_allocated (the model-level messages, like the allocator's other errors). Other locales fall back to English.

Because they are subclasses, every existing rescue OverAllocationError still catches them. Matcher#write_allocation is unchanged, and allocate_matched! already returns early on a zero amount, so it never reaches the guard in this state.

One behavior changes slightly: an explicit amount on a fully used transaction was already refused with "That would allocate more than the transaction's amount", and it now gets the more specific exhausted message.

Validation

  • test/models/recurring_transaction/allocator_test.rb:
    • an entry fully used by one occurrence, linked to another with no amount, raises EntryExhaustedError and creates no allocation;
    • linking it again to the occurrence that used it up raises AlreadyAllocatedError.
  • test/controllers/recurring_occurrences_controller_test.rb:
    • posting an entry another cycle used up leaves no allocation and flashes recurring_allocations.entry_exhausted. It fails on main, where the flash is the generic invalid message;
    • posting the same entry twice to one cycle flashes recurring_allocations.already_allocated the second time.
  • On a development database (16 bills, 120 occurrences), I tried linking every candidate transaction to every open occurrence, each attempt inside a transaction rolled back at the end. Before the fix, 24 of 339 attempts failed with Allocated amount must be greater than 0, all on transactions already used in full. After the fix, the same 24 raise EntryExhaustedError and the other 315 succeed as before.
  • bin/rails test: 12,650 runs, 3 failures, 0 errors, 36 skips, none related to this change:
    • two rule regex tests (test/models/rule/safe_regex_test.rb:46 and test/models/rule/condition_regex_test.rb:24) fail the same way on main. Each expects Postgres to reject ^(a{1,255}){1,255}(b) as too complex, and my local Postgres 14.23 accepts it. CI runs the latest postgres image, and ci / test_unit passed;
    • OnchainWalletItem::ChainDetectorTest "a probe that outlives the deadline" uses a 0.2 s deadline and failed once under the parallel run. It passed 3 times out of 3 on its own.
  • bin/rubocop, erb_lint, npm run lint and bin/brakeman (0 warnings) are clean. No ERB or JavaScript changes, so no screenshots or system tests.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved recurring transaction allocation errors with distinct messages when an amount is fully used elsewhere or already linked to the current occurrence.
    • Prevented duplicate allocations and allocations exceeding the remaining transaction amount.
    • Failed allocation attempts now leave the occurrence unchanged.
  • Localization

    • Added English and French translations for the new allocation error messages.