The personal finance app for everyone (by everyone)
Ruby
Delete imported transactions that Plaid retracts (#3852)
Darrell952ba08179df
diff --git a/app/models/plaid_account/transactions/processor.rb b/app/models/plaid_account/transactions/processor.rbindex a59045a37c..5885d1f659 100644--- a/app/models/plaid_account/transactions/processor.rb+++ b/app/models/plaid_account/transactions/processor.rb@@ -42,8 +42,17 @@ class PlaidAccount::Transactions::Processorplaid_account.current_accountend+ # Entries are imported through Account::ProviderImportAdapter, which keys them+ # on external_id + source and leaves plaid_id empty. Only entries that predate+ # the adapter carry plaid_id, so a removal has to look in both places.def remove_plaid_transaction(raw_transaction)- account.entries.find_by(plaid_id: raw_transaction["transaction_id"])&.destroy+ transaction_id = raw_transaction["transaction_id"]+ return if transaction_id.blank?++ account.entries+ .where(external_id: transaction_id, source: PlaidEntry::Processor::SOURCE)+ .or(account.entries.where(plaid_id: transaction_id))+ .destroy_allend# Since we find_or_create_by transactions, we don't need a distinction between added/modifieddiff --git a/test/models/plaid_account/transactions/processor_test.rb b/test/models/plaid_account/transactions/processor_test.rbindex 82076a1fb0..03fcf65af4 100644--- a/test/models/plaid_account/transactions/processor_test.rb+++ b/test/models/plaid_account/transactions/processor_test.rb@@ -35,6 +35,59 @@ class PlaidAccount::Transactions::ProcessorTest < ActiveSupport::TestCaseprocessor.processend+ test "removes imported transactions that plaid retracts" do+ account = @plaid_account.current_account+ retracted_id = "retracted_by_plaid"++ Account::ProviderImportAdapter.new(account).import_transaction(+ external_id: retracted_id,+ amount: 100,+ currency: "USD",+ date: Date.current,+ name: "Retracted",+ source: PlaidEntry::Processor::SOURCE+ )++ # The import path does not set plaid_id, so a lookup on it alone finds nothing.+ assert_nil account.entries.find_by(external_id: retracted_id).plaid_id++ @plaid_account.update!(raw_transactions_payload: {+ added: [],+ modified: [],+ removed: [ { "transaction_id" => retracted_id } ]+ })++ assert_difference [ "Entry.count", "Transaction.count" ], -1 do+ PlaidAccount::Transactions::Processor.new(@plaid_account).process+ end++ assert_nil account.entries.find_by(external_id: retracted_id)+ end++ test "leaves another provider's entry with the same external id alone" do+ account = @plaid_account.current_account+ shared_id = "shared_external_id"++ Account::ProviderImportAdapter.new(account).import_transaction(+ external_id: shared_id,+ amount: 100,+ currency: "USD",+ date: Date.current,+ name: "Not from Plaid",+ source: "simplefin"+ )++ @plaid_account.update!(raw_transactions_payload: {+ added: [],+ modified: [],+ removed: [ { "transaction_id" => shared_id } ]+ })++ assert_no_difference "Entry.count" do+ PlaidAccount::Transactions::Processor.new(@plaid_account).process+ end+ end+test "removes transactions no longer in plaid" dodestroyable_transaction_id = "destroy_me"@plaid_account.current_account.entries.create!(