The personal finance app for everyone (by everyone)
Ruby
fix(transfers): reset the kept leg's kind when a transfer leg is deleted (#4158)
scragnog25b5f4849849
diff --git a/app/models/transfer.rb b/app/models/transfer.rbindex 9d4f9c48fc..ddb8524c74 100644--- a/app/models/transfer.rb+++ b/app/models/transfer.rb@@ -16,6 +16,11 @@ class Transfer < ApplicationRecordvalidate :transfer_within_date_rangevalidate :transfer_has_same_family+ # Runs on every destroy, including the cascade when one leg's transaction+ # (or its entry) is deleted, so the surviving leg never keeps a transfer kind+ # without a transfer.+ before_destroy :release_transactions+class << self# The outflow leg's kind for a transfer into this account. Transfer::Creator,# Family::DataImporter, auto-matching, rules and manual matches all call@@ -126,31 +131,6 @@ class Transfer < ApplicationRecordendend- def destroy!- Transfer.transaction do- [ inflow_transaction, outflow_transaction ].each do |transaction|- next if transaction.nil?- next unless Transaction.exists?(transaction.id)- begin- # 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,- # finds no Transfer anymore, and would otherwise hit the unique- # index on the stale entry and raise instead of creating a new- # transfer (see Transfer::Creator#find_existing_transfer).- transaction.entry.update!(idempotency_key: nil) if transaction.entry&.idempotency_key.present?- rescue ActiveRecord::RecordNotFound- rescue NoMethodError- next- end- end- super- end- end-# 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@@ -192,6 +172,33 @@ class Transfer < ApplicationRecordendprivate+ # Gives each surviving transaction back its standalone state. When the+ # transfer goes because one leg is being deleted (dependent: :destroy on+ # Transaction), that leg is skipped.+ def release_transactions+ deleted_leg_key = destroyed_by_association&.foreign_key&.to_s++ { "inflow_transaction_id" => inflow_transaction, "outflow_transaction_id" => outflow_transaction }.each do |key, transaction|+ next if transaction.nil? || key == deleted_leg_key+ next unless Transaction.exists?(transaction.id)+ begin+ # 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,+ # finds no Transfer anymore, and would otherwise hit the unique+ # index on the stale entry and raise instead of creating a new+ # transfer (see Transfer::Creator#find_existing_transfer).+ transaction.entry.update!(idempotency_key: nil) if transaction.entry&.idempotency_key.present?+ rescue ActiveRecord::RecordNotFound+ rescue NoMethodError+ next+ end+ end+ end+def transfer_has_different_accountsreturn unless inflow_transaction&.entry && outflow_transaction&.entryerrors.add(:base, :different_accounts) if to_account == from_accountdiff --git a/test/models/transfer_test.rb b/test/models/transfer_test.rbindex ab81214580..ac6db73ccd 100644--- a/test/models/transfer_test.rb+++ b/test/models/transfer_test.rb@@ -14,6 +14,38 @@ class TransferTest < ActiveSupport::TestCaseendend+ test "deleting one leg gives the other leg back the standard kind" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))+ transfer.confirm!+ outflow = transfer.outflow_transaction+ assert_equal "cc_payment", outflow.reload.kind++ assert_difference "Transfer.count", -1 do+ transfer.inflow_transaction.entry.destroy!+ end++ assert_equal "standard", outflow.reload.kind+ end++ test "deleting a leg of a pending transfer leaves the other leg's kind alone" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))+ transfer.inflow_transaction.update!(kind: "one_time")++ transfer.outflow_transaction.entry.destroy!++ assert_equal "one_time", transfer.inflow_transaction.reload.kind+ end++ test "destroy resets the transfer kinds of a confirmed transfer, like destroy!" do+ transfer = create_pending_transfer(from: accounts(:depository), to: accounts(:credit_card))+ transfer.confirm!++ transfer.destroy++ assert_equal "standard", transfer.outflow_transaction.reload.kind+ assert_equal "standard", transfer.inflow_transaction.reload.kind+ end+test "destroy! clears the idempotency key so a retried request can create a new transfer" doidempotency_key = SecureRandom.uuid