The personal finance app for everyone (by everyone)
Ruby
Keep pre-existing entries when reverting a deduping transaction import (#4960)
arnaurodondevd74ee9f284d2
diff --git a/app/models/transaction_import.rb b/app/models/transaction_import.rbindex 8679996584..76604052fa 100644--- a/app/models/transaction_import.rb+++ b/app/models/transaction_import.rb@@ -57,11 +57,12 @@ class TransactionImport < Import)if duplicate_entry- # Update existing transaction instead of creating a new one+ # Update existing transaction instead of creating a new one. It keeps+ # its original import (if any): the entry belongs to whoever created+ # it, so reverting or deleting this import must not destroy it.duplicate_entry.transaction.category = category if category.present?duplicate_entry.transaction.tags = tags if tags.any?duplicate_entry.notes = row.notes if row.notes.present?- duplicate_entry.import = selfduplicate_entry.import_locked = true # Protect from provider sync overwritesupdated_entries << duplicate_entryclaimed_entry_ids.add(duplicate_entry.id)diff --git a/test/models/transaction_import_test.rb b/test/models/transaction_import_test.rbindex b715b3d497..6987eb18bc 100644--- a/test/models/transaction_import_test.rb+++ b/test/models/transaction_import_test.rb@@ -331,8 +331,42 @@ class TransactionImportTest < ActiveSupport::TestCaseendend- # The existing entry should now be linked to the import- assert_equal @import.id, existing_entry.reload.import_id+ # The existing entry is updated but stays owned by whoever created it+ assert_nil existing_entry.reload.import_id+ assert existing_entry.import_locked?+ end++ test "revert keeps a pre-existing entry the import matched as a duplicate" do+ account = accounts(:depository)+ existing_entry = account.entries.create!(+ date: Date.new(2024, 1, 1),+ amount: 100,+ currency: "USD",+ name: "Coffee Shop",+ entryable: Transaction.new+ )++ @import.update!(+ account: account,+ raw_file_str: "date,name,amount\n01/01/2024,Coffee Shop,100\n01/02/2024,Bakery,20\n",+ date_col_label: "date",+ amount_col_label: "amount",+ name_col_label: "name",+ date_format: "%m/%d/%Y",+ amount_type_strategy: "signed_amount",+ signage_convention: "inflows_negative"+ )+ @import.generate_rows_from_csv+ @import.reload+ @import.publish+ assert @import.reload.complete?++ assert_difference -> { Entry.count } => -1 do+ @import.revert+ end++ assert Entry.exists?(existing_entry.id), "revert must not destroy an entry the import did not create"+ assert_not account.entries.exists?(name: "Bakery")endtest "creates new transaction when name differs even if date and amount match" do@@ -425,8 +459,8 @@ class TransactionImportTest < ActiveSupport::TestCasename: "Vending Machine").count- # The existing entry should be linked to the import- assert_equal @import.id, existing_entry.reload.import_id+ # The existing entry stays owned by whoever created it+ assert_nil existing_entry.reload.import_idendtest "imports all identical transactions from CSV when none exist in database" do