The personal finance app for everyone (by everyone)
Ruby
fix(transactions): include enable_banking in pending/confirmed status filter (#1885)
web-dev05213311759ec2c1
diff --git a/app/models/entry_search.rb b/app/models/entry_search.rbindex 0c67bd5465..6cb6fec3bb 100644--- a/app/models/entry_search.rb+++ b/app/models/entry_search.rb@@ -62,16 +62,15 @@ class EntrySearchreturn scope unless statuses.present?return scope if statuses.uniq.sort == %w[confirmed pending] # Both selected = no filter+ # Source the pending check from Transaction::PENDING_CHECK_SQL (aliased to+ # "t") so every provider in PENDING_PROVIDERS is covered. Previously this+ # hardcoded only simplefin/plaid/lunchflow, dropping enable_banking.pending_condition = <<~SQL.squishentries.entryable_type = 'Transaction'AND EXISTS (SELECT 1 FROM transactions tWHERE t.id = entries.entryable_id- AND (- (t.extra -> 'simplefin' ->> 'pending')::boolean = true- OR (t.extra -> 'plaid' ->> 'pending')::boolean = true- OR (t.extra -> 'lunchflow' ->> 'pending')::boolean = true- )+ AND (#{Transaction::PENDING_CHECK_SQL}))SQL@@ -80,11 +79,7 @@ class EntrySearchOR NOT EXISTS (SELECT 1 FROM transactions tWHERE t.id = entries.entryable_id- AND (- (t.extra -> 'simplefin' ->> 'pending')::boolean = true- OR (t.extra -> 'plaid' ->> 'pending')::boolean = true- OR (t.extra -> 'lunchflow' ->> 'pending')::boolean = true- )+ AND (#{Transaction::PENDING_CHECK_SQL}))SQLdiff --git a/app/models/transaction/search.rb b/app/models/transaction/search.rbindex d9a87c18e7..d4bc9bfcca 100644--- a/app/models/transaction/search.rb+++ b/app/models/transaction/search.rb@@ -196,23 +196,14 @@ class Transaction::Searchreturn query unless statuses.present?return query if statuses.uniq.sort == [ "confirmed", "pending" ] # Both selected = no filter- pending_condition = <<~SQL.squish- (transactions.extra -> 'simplefin' ->> 'pending')::boolean = true- OR (transactions.extra -> 'plaid' ->> 'pending')::boolean = true- OR (transactions.extra -> 'lunchflow' ->> 'pending')::boolean = true- SQL-- confirmed_condition = <<~SQL.squish- (transactions.extra -> 'simplefin' ->> 'pending')::boolean IS DISTINCT FROM true- AND (transactions.extra -> 'plaid' ->> 'pending')::boolean IS DISTINCT FROM true- AND (transactions.extra -> 'lunchflow' ->> 'pending')::boolean IS DISTINCT FROM true- SQL-+ # Delegate to the model scopes so the provider list stays sourced from+ # Transaction::PENDING_PROVIDERS. Previously this method hardcoded only+ # simplefin/plaid/lunchflow, silently dropping enable_banking transactions.case statuses.sortwhen [ "pending" ]- query.where(pending_condition)+ query.pendingwhen [ "confirmed" ]- query.where(confirmed_condition)+ query.excluding_pendingelsequeryenddiff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rbindex dba43ba9bc..5569f4c9aa 100644--- a/test/models/account/entry_test.rb+++ b/test/models/account/entry_test.rb@@ -67,6 +67,30 @@ class EntryTest < ActiveSupport::TestCaseassert_equal 0, family.entries.search(params).sizeend+ test "status filter matches pending transactions for every supported provider via EntrySearch" do+ family = families(:empty)+ account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new++ confirmed = create_transaction(account: account, amount: 50, kind: "standard")++ pending_by_provider = Transaction::PENDING_PROVIDERS.index_with do |provider|+ entry = create_transaction(account: account, amount: 50, kind: "standard")+ entry.entryable.update!(extra: { provider => { "pending" => true } })+ entry.id+ end++ pending_ids = family.entries.search(status: [ "pending" ]).pluck(:id)+ confirmed_ids = family.entries.search(status: [ "confirmed" ]).pluck(:id)++ pending_by_provider.each do |provider, entry_id|+ assert_includes pending_ids, entry_id, "#{provider} pending entry should match the pending filter"+ assert_not_includes confirmed_ids, entry_id, "#{provider} pending entry should be excluded from the confirmed filter"+ end++ assert_includes confirmed_ids, confirmed.id+ assert_not_includes pending_ids, confirmed.id+ end+test "visible scope only returns entries from visible accounts" do# Create transactions for all account typesvisible_transaction = create_transaction(account: accounts(:depository), name: "Visible transaction")diff --git a/test/models/transaction/search_test.rb b/test/models/transaction/search_test.rbindex e3e899929a..69f3885cc8 100644--- a/test/models/transaction/search_test.rb+++ b/test/models/transaction/search_test.rb@@ -646,4 +646,28 @@ class Transaction::SearchTest < ActiveSupport::TestCaseassert_equal Money.new(0, @family.currency), totals.transfer_inflow_moneyassert_equal Money.new(0, @family.currency), totals.transfer_outflow_moneyend++ test "status filter matches pending transactions for every supported provider" do+ confirmed = create_transaction(account: @checking_account, amount: 100, kind: "standard")++ # One pending transaction per provider in PENDING_PROVIDERS. Regression for the+ # status filter only checking simplefin/plaid/lunchflow and silently dropping+ # enable_banking pending transactions.+ pending_by_provider = Transaction::PENDING_PROVIDERS.index_with do |provider|+ entry = create_transaction(account: @checking_account, amount: 100, kind: "standard")+ entry.entryable.update!(extra: { provider => { "pending" => true } })+ entry.entryable.id+ end++ pending_ids = Transaction::Search.new(@family, filters: { status: [ "pending" ] }).transactions_scope.pluck(:id)+ confirmed_ids = Transaction::Search.new(@family, filters: { status: [ "confirmed" ] }).transactions_scope.pluck(:id)++ pending_by_provider.each do |provider, txn_id|+ assert_includes pending_ids, txn_id, "#{provider} pending txn should match the pending filter"+ assert_not_includes confirmed_ids, txn_id, "#{provider} pending txn should be excluded from the confirmed filter"+ end++ assert_includes confirmed_ids, confirmed.entryable.id+ assert_not_includes pending_ids, confirmed.entryable.id+ endend