fix(bills): explain why a used-up transaction cannot pay a bill
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:
- With no amount given,
Allocator#resolve_amountsdefaults to what the entry has left, which is0. guard_entry_capacity!compares0 > capacity + CLOSE_EPSILON, which is false, so it lets the zero through.RecurringAllocationvalidatesallocated_amount > 0,create!raisesActiveRecord::RecordInvalid, andRecurringAllocationsControllermaps that to the genericrecurring_allocations.invalidmessage.
Changes
- Two subclasses of
OverAllocationErrorinRecurringTransaction::Allocator.guard_entry_capacity!raises one of them when nothing of the entry is left, before the existing over-allocation comparison:EntryExhaustedErrorwhen other occurrences hold the entry;AlreadyAllocatedErrorwhen this occurrence already holds it, for example after a double submit or from a tab left open after the first link.
RecurringAllocationsController#allocation_error_messagemapsEntryExhaustedErrorto a newrecurring_allocations.entry_exhaustedmessage: "That transaction's full amount is already applied to other payments due".AlreadyAllocatedErrorgets the existingrecurring_allocations.already_allocatedmessage, the same one the unique index produces when the entry still has capacity. Both are checked beforeOverAllocationError, which would otherwise match them.- New keys in
enandfr:recurring_allocations.entry_exhausted, plusrecurring_transactions.allocator.entry_exhaustedandrecurring_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
EntryExhaustedErrorand creates no allocation; - linking it again to the occurrence that used it up raises
AlreadyAllocatedError.
- an entry fully used by one occurrence, linked to another with no amount, raises
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 onmain, where the flash is the genericinvalidmessage; - posting the same entry twice to one cycle flashes
recurring_allocations.already_allocatedthe second time.
- posting an entry another cycle used up leaves no allocation and flashes
- 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 raiseEntryExhaustedErrorand 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:46andtest/models/rule/condition_regex_test.rb:24) fail the same way onmain. 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 latestpostgresimage, andci / test_unitpassed; 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.
- two rule regex tests (
bin/rubocop,erb_lint,npm run lintandbin/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.