The personal finance app for everyone (by everyone)
Ruby
Keep attachments, tags and notes when merging a pending duplicate (#4247)
alexcastgonz38011119bd29
diff --git a/app/models/transaction.rb b/app/models/transaction.rbindex 1667320ee0..4faf687130 100644--- a/app/models/transaction.rb+++ b/app/models/transaction.rb@@ -455,6 +455,15 @@ class Transaction < ApplicationRecord# cannot race on reading/writing extra (e.g., the manual_merge array).posted_tx = posted_entry.entryableposted_tx.lock! if posted_tx.is_a?(Transaction)++ # Moving the attachments bypasses validation, so refuse a merge that would+ # leave the posted transaction over the limit instead of dropping files.+ if posted_tx.is_a?(Transaction) && pending_entry.entryable.is_a?(Transaction) &&+ attachment_count_after_merge(from: pending_entry.entryable, to: posted_tx) > MAX_ATTACHMENTS_PER_TRANSACTION+ Rails.logger.info("merge_with_duplicate! rejected: merging pending entry #{pending_entry_id} would exceed #{MAX_ATTACHMENTS_PER_TRANSACTION} attachments")+ raise ActiveRecord::Rollback+ end+if posted_tx.is_a?(Transaction)tx_attrs = {}@@ -487,15 +496,27 @@ class Transaction < ApplicationRecordendposted_tx.update!(tx_attrs) if tx_attrs.any?++ # Keep what the user added to the pending transaction (e.g. a receipt attached+ # before the bank booked it); destroying the pending entry would purge it.+ pending_transaction = pending_entry.entryable+ carry_over_user_additions(from: pending_transaction, to: posted_tx) if pending_transaction.is_a?(Transaction)end# Date inheritance on the Entry row — separate from the Transaction update above.+ entry_attrs = {}unless posted_entry.protected_from_sync?# Date: pending dates reflect actual transaction initiation time- posted_entry.update!(date: pending_entry_date) if posted_entry.date != pending_entry_date+ entry_attrs[:date] = pending_entry_date if posted_entry.date != pending_entry_date# Name + Merchant intentionally NOT inherited — booked values are canonicalend+ # Notes are the user's own text, so they are kept even on a protected entry.+ notes = merged_notes(posted_entry.notes, pending_entry.notes)+ entry_attrs[:notes] = notes if notes != posted_entry.notes++ posted_entry.update!(entry_attrs) if entry_attrs.any?+# Lock the posted entry so future syncs cannot overwrite the merged stateposted_entry.mark_user_modified!@@ -550,6 +571,37 @@ class Transaction < ApplicationRecordendprivate+ # Moves the pending transaction's attachments (the attachment rows themselves, so the+ # blobs are not purged with the pending entry) and adds its tags to the posted one.+ def carry_over_user_additions(from:, to:)+ if from.attachments.attached?+ # A blob already attached to the posted transaction would collide with the+ # unique index on attachments once re-pointed, and is kept there anyway.+ from.attachments_attachments.where(blob_id: to.attachments_attachments.select(:blob_id)).delete_all+ from.attachments_attachments.update_all(record_id: to.id)+ end++ new_tags = from.tags.where.not(id: to.tags.select(:id))+ to.tags << new_tags if new_tags.any?+ end++ def attachment_count_after_merge(from:, to:)+ to_blob_ids = to.attachments_attachments.pluck(:blob_id)+ to_blob_ids.size + from.attachments_attachments.where.not(blob_id: to_blob_ids).count+ end++ # Notes merged earlier are joined by blank lines, so look for the pending+ # note's paragraphs, in order, among the posted note's: "Order 123" is not+ # already in "Order 1234", and a multi-paragraph note is not split apart.+ def merged_notes(posted_notes, pending_notes)+ return posted_notes if pending_notes.blank?+ return pending_notes if posted_notes.blank?+ posted_parts = posted_notes.split(/\n{2,}/).map(&:strip)+ pending_parts = pending_notes.split(/\n{2,}/).map(&:strip)+ return posted_notes if posted_parts.each_cons(pending_parts.size).include?(pending_parts)++ "#{posted_notes}\n\n#{pending_notes}"+ enddef validate_attachments# Check attachment count limitdiff --git a/test/models/transaction/merge_with_duplicate_test.rb b/test/models/transaction/merge_with_duplicate_test.rbindex 8149d0973a..84242c852a 100644--- a/test/models/transaction/merge_with_duplicate_test.rb+++ b/test/models/transaction/merge_with_duplicate_test.rb@@ -2,6 +2,7 @@ require "test_helper"class Transaction::MergeWithDuplicateTest < ActiveSupport::TestCaseinclude EntriesTestHelper+ include ActiveJob::TestHelpersetup do@account = accounts(:depository)@@ -305,4 +306,127 @@ class Transaction::MergeWithDuplicateTest < ActiveSupport::TestCase@pending_entry.transaction.update!(extra: {})assert_equal [], @pending_entry.transaction.pending_duplicate_candidates.to_aend++ test "moves the pending transaction's attachments to the posted transaction" do+ pending_tx = @pending_entry.transaction+ pending_tx.attachments.attach(io: StringIO.new("receipt"), filename: "receipt.pdf", content_type: "application/pdf")+ blob = pending_tx.attachments.first.blob++ perform_enqueued_jobs do+ assert pending_tx.merge_with_duplicate!+ end++ posted_tx = @posted_entry.transaction.reload+ assert_equal [ blob.id ], posted_tx.attachments.map(&:blob_id)+ assert ActiveStorage::Blob.exists?(blob.id), "the receipt must not be purged with the pending entry"+ assert_equal "receipt", posted_tx.attachments.first.download+ end++ test "adds the pending transaction's tags to the posted transaction" do+ shared = @family.tags.create!(name: "Shared")+ reimbursable = @family.tags.create!(name: "Reimbursable")+ @pending_entry.transaction.tags << [ shared, reimbursable ]+ @posted_entry.transaction.tags << shared++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal %w[Reimbursable Shared], @posted_entry.transaction.reload.tags.map(&:name).sort+ end++ test "keeps the pending entry's notes on the posted entry" do+ @pending_entry.update!(notes: "Lunch with client")++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal "Lunch with client", @posted_entry.reload.notes+ end++ test "appends the pending entry's notes to existing notes on the posted entry" do+ @pending_entry.update!(notes: "Lunch with client")+ @posted_entry.update!(notes: "Booked")++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal "Booked\n\nLunch with client", @posted_entry.reload.notes+ end++ test "appends a pending note that is only a substring of the posted note" do+ @pending_entry.update!(notes: "Order 123")+ @posted_entry.update!(notes: "Order 1234")++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal "Order 1234\n\nOrder 123", @posted_entry.reload.notes+ end++ test "does not repeat a pending note already merged into the posted note" do+ @pending_entry.update!(notes: "Lunch with client")+ @posted_entry.update!(notes: "Booked\n\nLunch with client")++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal "Booked\n\nLunch with client", @posted_entry.reload.notes+ end++ test "does not repeat a multi-paragraph pending note identical to the posted note" do+ @pending_entry.update!(notes: "Lunch with client\n\nPaid by card")+ @posted_entry.update!(notes: "Lunch with client\n\nPaid by card")++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal "Lunch with client\n\nPaid by card", @posted_entry.reload.notes+ end++ test "does not repeat a multi-paragraph pending note already merged into the posted note" do+ @pending_entry.update!(notes: "Lunch with client\n\nPaid by card")+ @posted_entry.update!(notes: "Booked\n\nLunch with client\n\nPaid by card")++ @pending_entry.transaction.merge_with_duplicate!++ assert_equal "Booked\n\nLunch with client\n\nPaid by card", @posted_entry.reload.notes+ end++ test "keeps one attachment when both transactions have the same blob" do+ pending_tx = @pending_entry.transaction+ posted_tx = @posted_entry.transaction+ blob = ActiveStorage::Blob.create_and_upload!(io: StringIO.new("receipt"), filename: "receipt.pdf", content_type: "application/pdf")+ pending_tx.attachments.attach(blob)+ posted_tx.attachments.attach(blob)++ assert pending_tx.merge_with_duplicate!++ assert_equal [ blob.id ], posted_tx.reload.attachments.map(&:blob_id)+ end++ test "refuses a merge that would leave more than the attachment limit" do+ pending_tx = @pending_entry.transaction+ posted_tx = @posted_entry.transaction+ 6.times { |i| posted_tx.attachments.attach(io: StringIO.new("posted #{i}"), filename: "posted#{i}.pdf", content_type: "application/pdf") }+ 5.times { |i| pending_tx.attachments.attach(io: StringIO.new("pending #{i}"), filename: "pending#{i}.pdf", content_type: "application/pdf") }++ assert_not pending_tx.merge_with_duplicate!++ assert Entry.exists?(@pending_entry.id), "the pending entry and its attachments must be kept"+ assert_equal 5, pending_tx.reload.attachments.count+ assert_equal 6, posted_tx.reload.attachments.count+ end++ test "keeps attachments, tags and notes when the posted entry is protected from sync" do+ pending_tx = @pending_entry.transaction+ pending_tx.attachments.attach(io: StringIO.new("receipt"), filename: "receipt.pdf", content_type: "application/pdf")+ blob = pending_tx.attachments.first.blob+ reimbursable = @family.tags.create!(name: "Reimbursable")+ pending_tx.tags << reimbursable+ @pending_entry.update!(notes: "Lunch with client")+ @posted_entry.mark_user_modified!++ perform_enqueued_jobs do+ assert pending_tx.merge_with_duplicate!+ end++ posted_tx = @posted_entry.transaction.reload+ assert_equal [ blob.id ], posted_tx.attachments.map(&:blob_id)+ assert_includes posted_tx.tags, reimbursable+ assert_equal "Lunch with client", @posted_entry.reload.notes+ endend