fix(recurring): include amount in manual recurring duplicate check (#2972)

* fix(recurring): include amount in manual recurring duplicate check

TransactionsController#mark_as_recurring blocked a second manual
recurring transaction whenever an existing one shared the same
account + payee name/merchant + currency, even when the amount
differed -- stricter than the DB unique indexes
(idx_recurring_txns_acct_name / idx_recurring_txns_acct_merchant),
RecurringTransaction::Identifier's own grouping key, and the
equivalent check already used in TransfersController#mark_as_recurring.

Add amount to the duplicate lookup so two distinct recurring payments
to the same payee at different amounts are both allowed, while an
exact duplicate is still blocked. Also rescue
ActiveRecord::RecordNotUnique around the create call so a race between
the pre-check and the DB constraint (e.g. a double-submit) surfaces
the same friendly "already exists" message instead of a generic
error, mirroring the existing race-handling pattern in
RecurringTransaction::Identifier.

Fixes #2936

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(recurring): don't blend distinct charge amounts into variance band

Once two manual recurring rows with the same payee/different amounts
can coexist (this PR), RecurringTransaction.create_from_transaction's
variance-band discovery still matched historical entries only by
account/payee/currency/day-window -- never by amount -- so it could
blend genuinely unrelated charges (e.g. a fee + a due from the same
merchant, same day) into one row's expected_amount_min/max/avg.
Flagged by Codex review on this PR.

Confirmed this is not hypothetical: two real production transactions
(3.00 and 19.68, same merchant, same day) got blended into a single
recurring row showing a fabricated "11.34" projected amount that
matches neither real transaction.

The same unfiltered matching independently exists in
RecurringTransaction::Identifier#manual_recurring_matches_entry?,
which periodically re-derives every manual recurring row's variance
after each sync (via IdentifyRecurringTransactionsJob). Both call
sites needed the fix together, or the job would silently re-blend
amounts on the next sync.

Add RecurringTransaction.amount_within_variance_band?(candidate,
anchor, ratio: 2) -- a candidate only counts as "the same fluctuating
payment" if it's within 2x (double/half) of the anchor. Anchored on
the target amount (not pairwise) so unrelated charges can't chain
together; ratio-based (not %-of-target-with-floor) so it's
scale-invariant and handles signed (expense) amounts correctly.
Threshold checked against real data: existing variance test fixtures
sit at ~1.2-1.3x (must stay included), the real corrupted case sits
at ~6.6x (must be excluded) -- 2x leaves comfortable margin on both
sides.

Wire this into find_matching_transaction_entries/
find_matching_transaction_amounts (SQL-level filter, same pattern as
the existing day-of-month bounds) and into
manual_recurring_matches_entry?. amount_window_scope/
matching_transactions and create_from_transfer need no changes --
confirmed by reading: the former only consumes an already-computed
band, the latter never does variance discovery at all.

Does not touch any already-corrupted production data -- deliberately
out of scope, discussed separately.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
GFR
2026-08-16 09:30:44 +02:00
committed by GitHub
co-authored by Claude Sonnet 5
parent fa5c544431
commit d0bb1a31e8
5 changed files with 250 additions and 3 deletions
@@ -400,6 +400,115 @@ end
assert_equal "A manual recurring transaction already exists for this pattern", flash[:alert]
end
test "mark_as_recurring allows a second manual recurring transaction with same merchant but different amount" do
family = families(:empty)
sign_in users(:empty)
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
merchant = family.merchants.create! name: "Test Merchant"
entry = create_transaction(account: account, amount: 34, merchant: merchant)
transaction = entry.entryable
# Existing manual recurring row for the same merchant, but a different amount
family.recurring_transactions.create!(
account: account,
merchant: merchant,
amount: 12,
currency: entry.currency,
expected_day_of_month: entry.date.day,
last_occurrence_date: entry.date,
next_expected_date: 1.month.from_now,
status: "active",
manual: true,
occurrence_count: 1
)
assert_difference "family.recurring_transactions.count", 1 do
post mark_as_recurring_transaction_path(transaction)
end
assert_redirected_to transactions_path
assert_equal "Transaction marked as recurring", flash[:notice]
end
test "mark_as_recurring allows a second manual recurring transaction with same name but different amount" do
family = families(:empty)
sign_in users(:empty)
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
entry = create_transaction(account: account, name: "Example Payee", amount: 34)
transaction = entry.entryable
# Existing manual recurring row for the same payee name, but a different amount
family.recurring_transactions.create!(
account: account,
name: "Example Payee",
amount: 12,
currency: entry.currency,
expected_day_of_month: entry.date.day,
last_occurrence_date: entry.date,
next_expected_date: 1.month.from_now,
status: "active",
manual: true,
occurrence_count: 1
)
assert_difference "family.recurring_transactions.count", 1 do
post mark_as_recurring_transaction_path(transaction)
end
assert_redirected_to transactions_path
assert_equal "Transaction marked as recurring", flash[:notice]
end
test "mark_as_recurring shows alert if recurring transaction with same name and amount already exists" do
family = families(:empty)
sign_in users(:empty)
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
entry = create_transaction(account: account, name: "Example Payee", amount: 100)
transaction = entry.entryable
family.recurring_transactions.create!(
account: account,
name: "Example Payee",
amount: entry.amount,
currency: entry.currency,
expected_day_of_month: entry.date.day,
last_occurrence_date: entry.date,
next_expected_date: 1.month.from_now,
status: "active",
manual: true,
occurrence_count: 1
)
assert_no_difference "RecurringTransaction.count" do
post mark_as_recurring_transaction_path(transaction)
end
assert_redirected_to transactions_path
assert_equal "A manual recurring transaction already exists for this pattern", flash[:alert]
end
test "mark_as_recurring shows already-exists alert when a concurrent request wins the race" do
family = families(:empty)
sign_in users(:empty)
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
merchant = family.merchants.create! name: "Test Merchant"
entry = create_transaction(account: account, amount: 100, merchant: merchant)
transaction = entry.entryable
# Simulate another request creating the identical pattern between our
# pre-check and our create call.
RecurringTransaction.expects(:create_from_transaction).raises(
ActiveRecord::RecordNotUnique.new("duplicate key value violates unique constraint")
)
assert_no_difference "RecurringTransaction.count" do
post mark_as_recurring_transaction_path(transaction)
end
assert_redirected_to transactions_path
assert_equal "A manual recurring transaction already exists for this pattern", flash[:alert]
end
test "mark_as_recurring handles validation errors gracefully" do
family = families(:empty)
sign_in users(:empty)