The personal finance app for everyone (by everyone)
Ruby
Apply transfer kinds only when an auto-match is confirmed (#3762)
Andrei Shkarind55eac22043a
diff --git a/app/controllers/transfer_matches_controller.rb b/app/controllers/transfer_matches_controller.rbindex 43edbcbbc2..b56a36c4e5 100644--- a/app/controllers/transfer_matches_controller.rb+++ b/app/controllers/transfer_matches_controller.rb@@ -15,20 +15,7 @@ class TransferMatchesController < ApplicationController@transfer = build_transferTransfer.transaction do@transfer.save!-- # Use DESTINATION (inflow) account for kind, matching Transfer::Creator logic- destination_account = @transfer.inflow_transaction.entry.account- source_account = @transfer.outflow_transaction.entry.account- outflow_kind = Transfer.kind_for_account(destination_account, from_account: source_account)- outflow_attrs = { kind: outflow_kind }-- if outflow_kind == "investment_contribution"- category = destination_account.family.investment_contributions_category- outflow_attrs[:category] = category if category.present? && @transfer.outflow_transaction.category_id.blank?- end-- @transfer.outflow_transaction.update!(outflow_attrs)- @transfer.inflow_transaction.update!(kind: "funds_movement")+ @transfer.confirm!end@transfer.sync_account_laterdiff --git a/app/controllers/transfers_controller.rb b/app/controllers/transfers_controller.rbindex ab8446a2f6..bd443d8c3b 100644--- a/app/controllers/transfers_controller.rb+++ b/app/controllers/transfers_controller.rb@@ -90,7 +90,10 @@ class TransfersController < ApplicationControllerreturn reject_transfer if rejecting?outflow_account = @transfer.outflow_transaction.entry.account+ inflow_account = @transfer.inflow_transaction.entry.accountreturn unless require_account_permission!(outflow_account, redirect_path: transactions_url)+ # Confirming or rejecting rewrites the kind of both legs+ return unless require_account_permission!(inflow_account, redirect_path: transactions_url)Transfer.transaction doupdate_transfer_status@@ -295,8 +298,14 @@ class TransfersController < ApplicationControllerenddef update_transfer_details- @transfer.outflow_transaction.update!(category_id: transfer_update_params[:category_id])- @transfer.update!(notes: transfer_update_params[:notes])+ # Confirming from the transaction list sends only the status; leave the+ # category and notes alone unless the form actually submitted them.+ if transfer_update_params.key?(:category_id)+ @transfer.outflow_transaction.update!(category_id: transfer_update_params[:category_id])+ end+ if transfer_update_params.key?(:notes)+ @transfer.update!(notes: transfer_update_params[:notes])+ endenddef update_transfer_fees_and_amountdiff --git a/app/models/account/provider_import_adapter.rb b/app/models/account/provider_import_adapter.rbindex 581a19e290..f8439e51fa 100644--- a/app/models/account/provider_import_adapter.rb+++ b/app/models/account/provider_import_adapter.rb@@ -264,9 +264,11 @@ class Account::ProviderImportAdapter# amount on a Loan turning the matched inflow back into loan_payment,# makes budgets and reports count the same payment as an expense on# both legs. Deriving it from the transfer also repairs legs an earlier- # sync already overwrote.+ # sync already overwrote. An unconfirmed auto-match gets its kinds only+ # in Transfer#confirm!, so until then the leg keeps its own kind.if auto_kind.present?transfer = matched_transfer_for(entry)+ transfer = nil if transfer&.pending?entry.transaction.assign_attributes(kind: transfer ? transfer.kind_for_leg(entry.transaction) : auto_kind)enddiff --git a/app/models/demo/financekit_generator.rb b/app/models/demo/financekit_generator.rbindex 80422b9357..891038642d 100644--- a/app/models/demo/financekit_generator.rb+++ b/app/models/demo/financekit_generator.rb@@ -161,7 +161,7 @@ class Demo::FinancekitGeneratorfrom = funding_accountoutflow = from.entries.create!(name: entry.name, date: entry.date, amount: -entry.amount,currency: entry.currency, entryable: Transaction.new(kind: Transfer.kind_for_account(entry.account)))- Transfer.create!(inflow_transaction: inflow, outflow_transaction: outflow.entryable)+ Transfer.create!(inflow_transaction: inflow, outflow_transaction: outflow.entryable, status: "confirmed")@funding_accounts_to_sync << fromendinflow.update!(kind: "funds_movement", category: nil)@@ -220,7 +220,8 @@ class Demo::FinancekitGeneratorreceived = transaction!(nancy, -amount, "Received from Dad", last_date - days,source_id: received_id, transaction_type: "transfer")[ sent, received ].each { |identity| identity.entry.entryable.update!(kind: "funds_movement") }- Transfer.create!(outflow_transaction: sent.entry.entryable, inflow_transaction: received.entry.entryable)+ Transfer.create!(outflow_transaction: sent.entry.entryable, inflow_transaction: received.entry.entryable,+ status: "confirmed")changed.concat([ cash, nancy ])endchanged << nancy if generate_nancy_purchases!(nancy)diff --git a/app/models/family/auto_transfer_matchable.rb b/app/models/family/auto_transfer_matchable.rbindex 273fd14cee..e74bd9c580 100644--- a/app/models/family/auto_transfer_matchable.rb+++ b/app/models/family/auto_transfer_matchable.rb@@ -77,15 +77,9 @@ module Family::AutoTransferMatchable# someone can always confirm or reject the match.require_common_writer: true)- transaction_ids = candidates_scope.flat_map do |match|- [ match.inflow_transaction_id, match.outflow_transaction_id ]- end.uniq- transactions_by_id = Transaction.includes(entry: :account).where(id: transaction_ids).index_by(&:id)# Track which transactions we've already matched to avoid duplicatesused_transaction_ids = Set.new- investment_category = nil- investment_category_loaded = falseTransfer.transaction docandidates_scope.each do |match|@@ -93,32 +87,11 @@ module Family::AutoTransferMatchableused_transaction_ids.include?(match.outflow_transaction_id)# Skip this candidate when the transfer for this exact pair was not created- # (a concurrent sync claimed one of the transactions for a different pairing);- # marking it matched here would leave a transaction matched with no Transfer.+ # (a concurrent sync claimed one of the transactions for a different pairing).+ # A created transfer stays pending and leaves the transaction kinds untouched+ # until the user confirms it (Transfer#confirm!).next unless find_or_create_transfer!(match)- inflow_transaction = transactions_by_id.fetch(match.inflow_transaction_id)- outflow_transaction = transactions_by_id.fetch(match.outflow_transaction_id)- destination_account = inflow_transaction.entry.account- source_account = outflow_transaction.entry.account- transfer_kind = Transfer.kind_for_account(destination_account, from_account: source_account)-- # The kind is determined by the DESTINATION account (inflow), matching Transfer::Creator logic- inflow_transaction.update!(kind: "funds_movement")- outflow_transaction.update!(kind: transfer_kind)-- # Assign Investment Contributions category for transfers to investment accounts- if transfer_kind == "investment_contribution"- outflow_txn = outflow_transaction- if outflow_txn.category_id.blank?- unless investment_category_loaded- investment_category = investment_contributions_category- investment_category_loaded = true- end- outflow_txn.update!(category: investment_category) if investment_category.present?- end- end-used_transaction_ids << match.inflow_transaction_idused_transaction_ids << match.outflow_transaction_idenddiff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rbindex 99bc56233d..001de5c9fc 100644--- a/app/models/family/data_importer.rb+++ b/app/models/family/data_importer.rb@@ -1046,7 +1046,8 @@ class Family::DataImportertransfer.status = transfer_status_for(data["status"])transfer.notes = data["notes"]end- apply_transfer_transaction_kinds!(transfer)+ # Transfer kinds only apply once a transfer is confirmed (see Transfer#confirm!)+ apply_transfer_transaction_kinds!(transfer) if transfer.confirmed?increment_summary("Transfer", transfer.previously_new_record? ? :created : :updated)endenddiff --git a/app/models/rule/action_executor/set_as_transfer_or_payment.rb b/app/models/rule/action_executor/set_as_transfer_or_payment.rbindex 6845e53ade..bcef339a50 100644--- a/app/models/rule/action_executor/set_as_transfer_or_payment.rb+++ b/app/models/rule/action_executor/set_as_transfer_or_payment.rb@@ -18,20 +18,7 @@ class Rule::ActionExecutor::SetAsTransferOrPayment < Rule::ActionExecutortransfer = build_transfer(target_account, entry)Transfer.transaction dotransfer.save!-- # Use DESTINATION (inflow) account for kind, matching Transfer::Creator logic- destination_account = transfer.inflow_transaction.entry.account- source_account = transfer.outflow_transaction.entry.account- outflow_kind = Transfer.kind_for_account(destination_account, from_account: source_account)- outflow_attrs = { kind: outflow_kind }-- if outflow_kind == "investment_contribution"- category = destination_account.family.investment_contributions_category- outflow_attrs[:category] = category if category.present? && transfer.outflow_transaction.category_id.blank?- end-- transfer.outflow_transaction.update!(outflow_attrs)- transfer.inflow_transaction.update!(kind: "funds_movement")+ transfer.confirm!endtransfer.sync_account_laterdiff --git a/app/models/transfer.rb b/app/models/transfer.rbindex e3bd6df273..9d4f9c48fc 100644--- a/app/models/transfer.rb+++ b/app/models/transfer.rb@@ -132,7 +132,9 @@ class Transfer < ApplicationRecordnext if transaction.nil?next unless Transaction.exists?(transaction.id)begin- transaction.update!(kind: "standard")+ # A pending auto-match never changed the kind (see #confirm!), so a+ # transfer kind on its transactions predates the match and stays.+ transaction.update!(kind: "standard") if transaction.transfer? && !pending?# The entry survives this destroy (only the Transfer join row and# fee transactions go away), but its idempotency_key must not: a# later retry of the original create request looks up that key,@@ -149,8 +151,26 @@ class Transfer < ApplicationRecordendend+ # Auto-matched transfers start out pending and leave both transactions as+ # they were, so an unconfirmed suggestion never moves budgets or reports.+ # The transfer kinds (and the Investment Contributions category) only apply+ # once the user confirms the match.def confirm!- update!(status: "confirmed")+ Transfer.transaction do+ update!(status: "confirmed")++ # The kind is determined by the DESTINATION account (inflow), matching Transfer::Creator logic+ outflow_kind = Transfer.kind_for_account(to_account, from_account: from_account)+ outflow_attrs = { kind: outflow_kind }++ if outflow_kind == "investment_contribution" && outflow_transaction.category_id.blank?+ category = to_account.family.investment_contributions_category+ outflow_attrs[:category] = category if category.present?+ end++ inflow_transaction.update!(kind: "funds_movement")+ outflow_transaction.update!(outflow_attrs)+ endenddef datediff --git a/db/migrate/20260927200000_reset_kinds_of_pending_transfer_matches.rb b/db/migrate/20260927200000_reset_kinds_of_pending_transfer_matches.rbnew file mode 100644index 0000000000..fb98b09542--- /dev/null+++ b/db/migrate/20260927200000_reset_kinds_of_pending_transfer_matches.rb@@ -0,0 +1,24 @@+# Auto-matched transfers used to set the transfer kinds (and the Investment+# Contributions category) as soon as they were suggested. They now stay+# untouched until the user confirms the match, and rejecting a pending match+# no longer resets kinds, so pending matches left over from before would keep+# their transfer kinds forever. Put their transactions back to standard;+# Transfer#confirm! sets the kinds again when the match is confirmed.+class ResetKindsOfPendingTransferMatches < ActiveRecord::Migration[8.1]+ def up+ execute <<~SQL+ UPDATE transactions+ SET kind = 'standard'+ WHERE kind IN ('funds_movement', 'cc_payment', 'loan_payment', 'investment_contribution')+ AND id IN (+ SELECT inflow_transaction_id FROM transfers WHERE status = 'pending'+ UNION+ SELECT outflow_transaction_id FROM transfers WHERE status = 'pending'+ )+ SQL+ end++ # The kinds are set again by Transfer#confirm!.+ def down+ end+enddiff --git a/test/controllers/budget_categories_controller_test.rb b/test/controllers/budget_categories_controller_test.rbindex dbd581c5cb..3959715e40 100644--- a/test/controllers/budget_categories_controller_test.rb+++ b/test/controllers/budget_categories_controller_test.rb@@ -200,6 +200,7 @@ class BudgetCategoriesControllerTest < ActionDispatch::IntegrationTestname: "BUG_1059_REPRO_INFLOW")@family.auto_match_transfers!+ Transfer.pending.find_each(&:confirm!)get budget_budget_category_path(@budget, BudgetCategory.uncategorized.id)assert_response :success@@ -244,6 +245,7 @@ class BudgetCategoriesControllerTest < ActionDispatch::IntegrationTestname: "MORTGAGE_REPRO_INFLOW")@family.auto_match_transfers!+ Transfer.pending.find_each(&:confirm!)get budget_budget_category_path(@budget, BudgetCategory.uncategorized.id)assert_response :successdiff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex ffe35baf77..9f6cceb3a3 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -1699,6 +1699,17 @@ endRails.cache = original_cacheend+ test "an unconfirmed auto-matched transfer keeps its category menu in the list" do+ outflow = create_transaction(account: accounts(:depository), amount: 500, name: "PENDING_MATCH_OUT").transaction+ inflow = create_transaction(account: accounts(:credit_card), amount: -500).transaction+ Transfer.create!(outflow_transaction: outflow, inflow_transaction: inflow)++ get transactions_url++ assert_response :success+ assert_select "##{dom_id(outflow, "category_menu_desktop")} turbo-frame#category_dropdown"+ end+test "index with ai_status=current renders the AI filter badge" do@entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "ai")diff --git a/test/controllers/transfers_controller_test.rb b/test/controllers/transfers_controller_test.rbindex 97e3ecba6f..1c602b4301 100644--- a/test/controllers/transfers_controller_test.rb+++ b/test/controllers/transfers_controller_test.rb@@ -638,6 +638,19 @@ class TransfersControllerTest < ActionDispatch::IntegrationTestassert_equal original_inflow_tags, transfer.inflow_transaction.reload.tag_idsend+ test "confirming requires write permission on both transfer sides" do+ # family_member: full_control on depository (outflow), read_only on credit_card (inflow)+ sign_in users(:family_member)+ transfer = transfers(:one)+ original_inflow_kind = transfer.inflow_transaction.kind++ patch transfer_url(transfer), params: { transfer: { status: "confirmed" } }++ assert_redirected_to transactions_url+ assert transfer.reload.pending?+ assert_equal original_inflow_kind, transfer.inflow_transaction.reload.kind+ end+test "can add notes to transfer" dotransfer = transfers(:one)assert_nil transfer.notes@@ -649,6 +662,28 @@ class TransfersControllerTest < ActionDispatch::IntegrationTestassert_equal "Test notes", transfer.reload.notesend+ test "confirming a transfer keeps the outflow category" do+ transfer = transfers(:one)+ transfer.outflow_transaction.update!(category: categories(:food_and_drink))++ patch transfer_url(transfer), params: { transfer: { status: "confirmed" } }++ assert_redirected_to transactions_url+ assert transfer.reload.confirmed?+ assert_equal categories(:food_and_drink), transfer.outflow_transaction.reload.category+ end++ test "confirming a transfer keeps its notes" do+ transfer = transfers(:one)+ transfer.update!(notes: "Rent split")++ patch transfer_url(transfer), params: { transfer: { status: "confirmed" } }++ assert_redirected_to transactions_url+ assert transfer.reload.confirmed?+ assert_equal "Rent split", transfer.notes+ end+test "handles rejection without FrozenError" dotransfer = transfers(:one)@@ -670,7 +705,8 @@ class TransfersControllerTest < ActionDispatch::IntegrationTestendtest "can reject a match when only the outflow account is accessible" do- transfer = cross_owner_transfer(outflow_account: accounts(:depository), inflow_account: member_loan)+ # Confirmed, so the legs carry transfer kinds that rejecting must reset+ transfer = cross_owner_transfer(outflow_account: accounts(:depository), inflow_account: member_loan, status: "confirmed")assert_difference -> { Transfer.count } => -1, -> { RejectedTransfer.count } => 1 dopatch transfer_url(transfer), params: { transfer: { status: "rejected" } }@@ -810,11 +846,11 @@ class TransfersControllerTest < ActionDispatch::IntegrationTestaccounts(:loan).tap { |loan| loan.update!(owner: users(:family_member)) }end- def cross_owner_transfer(outflow_account:, inflow_account:)+ def cross_owner_transfer(outflow_account:, inflow_account:, status: "pending")outflow = create_transaction(account: outflow_account, amount: 250, kind: "funds_movement")inflow = create_transaction(account: inflow_account, amount: -250, kind: "funds_movement")- Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")+ Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: status)enddef attach_fee(transfer, account:)diff --git a/test/models/account/provider_import_adapter_test.rb b/test/models/account/provider_import_adapter_test.rbindex de422d819c..b2a005af9b 100644--- a/test/models/account/provider_import_adapter_test.rb+++ b/test/models/account/provider_import_adapter_test.rb@@ -110,6 +110,7 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCasetransfer = loan_entry.transaction.reload.transferassert transfer.present?, "expected the two legs to be auto-matched"+ transfer.confirm!assert_equal "funds_movement", loan_entry.transaction.kindassert_equal "loan_payment", checking_entry.transaction.reload.kind@@ -129,6 +130,30 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCaseassert_equal "loan_payment", checking_entry.transaction.reload.kindend+ test "re-importing an unconfirmed auto-matched leg does not give it a transfer kind" do+ loan_entry = Account::ProviderImportAdapter.new(accounts(:loan)).import_transaction(+ external_id: "plaid_loan_inflow_pending", amount: -200.00, currency: "USD",+ date: Date.current, name: "Loan Repayment Received", source: "plaid"+ )+ checking_entry = @adapter.import_transaction(+ external_id: "plaid_checking_outflow_pending", amount: 200.00, currency: "USD",+ date: Date.current, name: "Loan Repayment", source: "plaid"+ )++ @family.auto_match_transfers!+ transfer = checking_entry.transaction.reload.transfer+ assert transfer.present?, "expected the two legs to be auto-matched"+ assert transfer.pending?++ @adapter.import_transaction(+ external_id: "plaid_checking_outflow_pending", amount: 200.00, currency: "USD",+ date: Date.current, name: "Loan Repayment", source: "plaid", kind: "funds_movement"+ )++ assert_equal "funds_movement", checking_entry.transaction.reload.kind+ assert_not_equal "loan_payment", checking_entry.transaction.kind+ end+test "a provider transfer hint does not overwrite the kind of a matched payment leg" doloan_entry = Account::ProviderImportAdapter.new(accounts(:loan)).import_transaction(external_id: "up_loan_inflow_1",@@ -148,8 +173,10 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase)@family.auto_match_transfers!- assert checking_entry.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"- assert_equal "loan_payment", checking_entry.transaction.kind+ transfer = checking_entry.transaction.reload.transfer+ assert transfer.present?, "expected the two legs to be auto-matched"+ transfer.confirm!+ assert_equal "loan_payment", checking_entry.transaction.reload.kind# Up flags the outgoing leg as an internal transfer (transferAccount).@adapter.import_transaction(@@ -185,6 +212,7 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase)@family.auto_match_transfers!assert loan_entry.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"+ loan_entry.transaction.transfer.confirm!# State left behind by syncs before the fix: the matched inflow was# turned back into loan_payment.@@ -273,6 +301,7 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase)end@family.auto_match_transfers!+ Transfer.pending.find_each(&:confirm!)# Up flags the outgoing legs as internal transfers, so the kind is derived# from each leg's transfer; without the hint the adapter derives nothing.diff --git a/test/models/demo/financekit_generator_test.rb b/test/models/demo/financekit_generator_test.rbindex 2989f2f877..03bc564c2a 100644--- a/test/models/demo/financekit_generator_test.rb+++ b/test/models/demo/financekit_generator_test.rb@@ -57,6 +57,7 @@ class Demo::FinancekitGeneratorTest < ActiveSupport::TestCaseassert_equal 12, payments.countpayments.each do |entry|transfer = entry.entryable.transfer+ assert_predicate transfer, :confirmed?assert_equal @checking, transfer.from_accountassert_equal account, transfer.to_accountassert_equal -entry.amount, transfer.outflow_transaction.entry.amount@@ -206,6 +207,7 @@ class Demo::FinancekitGeneratorTest < ActiveSupport::TestCaseassert_equal 6, gifts.countgifts.each do |entry|transfer = entry.entryable.transfer+ assert_predicate transfer, :confirmed?assert_equal cash, transfer.from_accountassert_equal nancy, transfer.to_accountassert_equal entry.date, transfer.outflow_transaction.entry.datediff --git a/test/models/family/auto_transfer_matchable_test.rb b/test/models/family/auto_transfer_matchable_test.rbindex 52844281cf..301e3df7ae 100644--- a/test/models/family/auto_transfer_matchable_test.rb+++ b/test/models/family/auto_transfer_matchable_test.rb@@ -19,6 +19,19 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseendend+ test "auto-matched transfers wait for confirmation before changing transaction kinds" do+ outflow_entry = create_transaction(date: Date.current, account: @depository, amount: 500)+ inflow_entry = create_transaction(date: Date.current, account: accounts(:investment), amount: -500)++ @family.auto_match_transfers!++ transfer = Transfer.find_by!(inflow_transaction_id: inflow_entry.entryable_id)+ assert transfer.pending?+ assert_equal "standard", outflow_entry.reload.entryable.kind+ assert_equal "standard", inflow_entry.reload.entryable.kind+ assert_nil outflow_entry.entryable.category+ end+test "concurrent unique-index race does not abort the surrounding transaction" dooutflow_entry = create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 500)inflow_entry = create_transaction(date: Date.current, account: @credit_card, amount: -500)@@ -62,12 +75,8 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaserefute_equal "funds_movement", inflow_entry.entryable.kind# ...and matching did not stop at the skip: the non-conflicting candidate was- # still created and both its entries marked.- good_in.reload- good_out.reload+ # still created.assert Transfer.exists?(inflow_transaction_id: good_in.entryable_id, outflow_transaction_id: good_out.entryable_id)- assert_equal "funds_movement", good_in.entryable.kind- assert_equal "cc_payment", good_out.entryable.kindendtest "a :taken on one column from a different pairing is skipped, not marked" do@@ -605,12 +614,12 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseendend- test "auto-matched cash to investment assigns investment contribution category" do+ test "confirmed auto-match from cash to investment assigns investment contribution category" doinvestment = accounts(:investment)outflow_entry = create_transaction(date: Date.current, account: @depository, amount: 500)inflow_entry = create_transaction(date: Date.current, account: investment, amount: -500)- @family.auto_match_transfers!+ auto_match_and_confirm_transfers!outflow_entry.reload@@ -618,22 +627,6 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseassert_equal category, outflow_entry.entryable.categoryend- test "auto-matched investment transfers reuse contribution category lookup" do- investment = accounts(:investment)- category = @family.investment_contributions_category-- create_transaction(date: Date.current, account: @depository, amount: 500)- create_transaction(date: Date.current, account: investment, amount: -500)- create_transaction(date: Date.current, account: @depository, amount: 700)- create_transaction(date: Date.current, account: investment, amount: -700)-- @family.expects(:investment_contributions_category).once.returns(category)-- assert_difference -> { Transfer.count }, 2 do- @family.auto_match_transfers!- end- end-test "does not match multi-currency transfer with missing exchange rate" docreate_transaction(date: Date.current, account: @depository, amount: 500)create_transaction(date: Date.current, account: @credit_card, amount: -700, currency: "GBP")@@ -704,7 +697,7 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseoutflow_entry = create_transaction(date: Date.current, account: @depository, amount: 500)inflow_entry = create_transaction(date: Date.current, account: @loan, amount: -500)- @family.auto_match_transfers!+ auto_match_and_confirm_transfers!outflow_entry.reloadinflow_entry.reload@@ -719,7 +712,7 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseoutflow_entry = create_transaction(date: Date.current, account: @loan, amount: 500)inflow_entry = create_transaction(date: Date.current, account: @depository, amount: -500)- @family.auto_match_transfers!+ auto_match_and_confirm_transfers!outflow_entry.reloadinflow_entry.reload@@ -735,7 +728,7 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseoutflow_entry = create_transaction(date: Date.current, account: @depository, amount: 500)inflow_entry = create_transaction(date: Date.current, account: @credit_card, amount: -500)- @family.auto_match_transfers!+ auto_match_and_confirm_transfers!outflow_entry.reloadinflow_entry.reload@@ -746,6 +739,11 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCaseendprivate+ def auto_match_and_confirm_transfers!+ @family.auto_match_transfers!+ Transfer.pending.find_each(&:confirm!)+ end+# Simulates a live provider connection so `Account#manual?` (and the SQL# query's equivalent check) treats the account as linked. `AccountProvider`# validates its polymorphic `provider` association is present, so this needsdiff --git a/test/models/family/data_importer_test.rb b/test/models/family/data_importer_test.rbindex 351ad91bfe..ea4408ad63 100644--- a/test/models/family/data_importer_test.rb+++ b/test/models/family/data_importer_test.rb@@ -1638,6 +1638,23 @@ class Family::DataImporterTest < ActiveSupport::TestCaseassert_equal "Candidate outflow", rejected_transfer.outflow_transaction.entry.nameend+ test "imports a pending transfer without applying transfer kinds" do+ ndjson = build_ndjson([+ { type: "Account", data: { id: "checking", name: "Checking", balance: "1000", currency: "USD", accountable_type: "Depository" } },+ { type: "Account", data: { id: "savings", name: "Savings", balance: "2500", currency: "USD", accountable_type: "Depository" } },+ { type: "Transaction", data: { id: "pending-outflow", account_id: "checking", date: "2024-01-15", amount: "100.00", name: "Pending outflow", currency: "USD", kind: "standard" } },+ { type: "Transaction", data: { id: "pending-inflow", account_id: "savings", date: "2024-01-15", amount: "-100.00", name: "Pending inflow", currency: "USD", kind: "standard" } },+ { type: "Transfer", data: { id: "transfer-1", inflow_transaction_id: "pending-inflow", outflow_transaction_id: "pending-outflow", status: "pending" } }+ ])++ Family::DataImporter.new(@family, ndjson).import!++ transfer = Transfer.joins(inflow_transaction: :entry).find_by!(entries: { name: "Pending inflow" })+ assert transfer.pending?+ assert_equal "standard", transfer.inflow_transaction.kind+ assert_equal "standard", transfer.outflow_transaction.kind+ end+test "skips rejected transfers with missing transactions under strict import" dosession = @family.import_sessions.create!(expected_chunks: 1)diff --git a/test/models/investment_flow_statement_test.rb b/test/models/investment_flow_statement_test.rbindex cd17839adb..4127bf281b 100644--- a/test/models/investment_flow_statement_test.rb+++ b/test/models/investment_flow_statement_test.rb@@ -59,6 +59,7 @@ class InvestmentFlowStatementTest < ActiveSupport::TestCase)family.auto_match_transfers!assert inflow.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"+ inflow.transaction.transfer.confirm!# The next sync replays the brokerage row; the matched inflow keeps funds_movement.brokerage.import_transaction(@@ -90,7 +91,8 @@ class InvestmentFlowStatementTest < ActiveSupport::TestCase)family.auto_match_transfers!assert outflow.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"- assert_equal "funds_movement", outflow.transaction.kind+ outflow.transaction.transfer.confirm!+ assert_equal "funds_movement", outflow.transaction.reload.kindtotals = InvestmentFlowStatement.new(family).period_totals(period: period)assert_equal Money.new(300, "USD"), totals.withdrawals@@ -133,6 +135,7 @@ class InvestmentFlowStatementTest < ActiveSupport::TestCasefamily.auto_match_transfers!assert inflow.transaction.reload.transfer.present?, "expected the two legs to be auto-matched"assert_equal outflow.transaction.id, inflow.transaction.transfer.outflow_transaction_id+ inflow.transaction.transfer.confirm!inflowenddiff --git a/test/models/trade_republic_account_activities_processor_test.rb b/test/models/trade_republic_account_activities_processor_test.rbindex 61fb362885..147ec6fd5f 100644--- a/test/models/trade_republic_account_activities_processor_test.rb+++ b/test/models/trade_republic_account_activities_processor_test.rb@@ -1457,6 +1457,10 @@ class TradeRepublicAccountActivitiesProcessorTest < ActiveSupport::TestCasedeposit = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_funded").transactionassert_equal bank_outflow.transaction, deposit.transfer&.outflow_transaction+ assert_equal "standard", deposit.reload.kind++ deposit.transfer.confirm!+assert_equal "funds_movement", deposit.reload.kindenddiff --git a/test/models/transfer_test.rb b/test/models/transfer_test.rbindex 6f39803db4..ab81214580 100644--- a/test/models/transfer_test.rb+++ b/test/models/transfer_test.rb@@ -137,6 +137,64 @@ class TransferTest < ActiveSupport::TestCaseendend+ test "confirm! marks both legs with the destination account's transfer kinds" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))++ transfer.confirm!++ assert transfer.reload.confirmed?+ assert_equal "cc_payment", transfer.outflow_transaction.reload.kind+ assert_equal "funds_movement", transfer.inflow_transaction.reload.kind+ end++ test "confirm! assigns the investment contributions category to an uncategorized outflow" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:investment))++ transfer.confirm!++ outflow = transfer.outflow_transaction.reload+ assert_equal "investment_contribution", outflow.kind+ assert_equal families(:dylan_family).investment_contributions_category, outflow.category+ end++ test "confirm! keeps an outflow category the user already set" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:investment))+ transfer.outflow_transaction.update!(category: categories(:income))++ transfer.confirm!++ assert_equal categories(:income), transfer.outflow_transaction.reload.category+ end++ test "reject! leaves the kind of a pending transfer's transactions alone" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))+ transfer.outflow_transaction.update!(kind: "one_time")++ transfer.reject!++ assert_equal "one_time", transfer.outflow_transaction.reload.kind+ assert_equal "standard", transfer.inflow_transaction.reload.kind+ end++ test "reject! keeps a transfer kind that predates a pending match" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))+ transfer.outflow_transaction.update!(kind: "funds_movement")++ transfer.reject!++ assert_equal "funds_movement", transfer.outflow_transaction.reload.kind+ end++ test "reject! resets the transfer kinds of a confirmed transfer" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))+ transfer.confirm!++ transfer.reject!++ assert_equal "standard", transfer.outflow_transaction.reload.kind+ assert_equal "standard", transfer.inflow_transaction.reload.kind+ end+test "kind_for_account returns investment_contribution for investment accounts" doassert_equal "investment_contribution", Transfer.kind_for_account(accounts(:investment))end@@ -260,4 +318,12 @@ class TransferTest < ActiveSupport::TestCasetransfer.fee_transactions << entry1.entryable << entry2.entryableassert_equal 5, transfer.total_feeend++ private+ def create_pending_transfer(from:, to:)+ outflow_entry = create_transaction(date: Date.current, account: from, amount: 500)+ inflow_entry = create_transaction(date: Date.current, account: to, amount: -500)++ Transfer.create!(inflow_transaction: inflow_entry.transaction, outflow_transaction: outflow_entry.transaction)+ endenddiff --git a/test/support/entries_test_helper.rb b/test/support/entries_test_helper.rbindex 18d3284a91..326a754812 100644--- a/test/support/entries_test_helper.rb+++ b/test/support/entries_test_helper.rb@@ -60,7 +60,8 @@ module EntriesTestHelpertransfer = Transfer.create!(outflow_transaction: outflow_transaction,inflow_transaction: inflow_transaction,- amount: amount.abs+ amount: amount.abs,+ status: "confirmed" # like Transfer::Creator; only a confirmed transfer carries transfer kinds)from_account.entries.create!(diff --git a/test/system/transactions_test.rb b/test/system/transactions_test.rbindex 6d2ec82630..4186a349cf 100644--- a/test/system/transactions_test.rb+++ b/test/system/transactions_test.rb@@ -274,6 +274,8 @@ class TransactionsTest < ApplicationSystemTestCaseoutflow_entry = create_transaction("outflow", Date.current, 500, account: asset_account)inflow_entry = create_transaction("inflow", 1.day.ago.to_date, -500, account: depository_account)@user.family.auto_match_transfers!+ # Entry.delete_all in setup orphans the fixture transfer, so confirm only this one+ Transfer.find_by!(outflow_transaction_id: outflow_entry.entryable_id).confirm!visit transactions_urlwithin "#entry-group-" + Date.current.to_s + "-totals" do