Files
sure/app/models/entry_search.rb
web-dev0521 3311759ec2 fix(transactions): include enable_banking in pending/confirmed status filter (#1885)
* fix(transactions): include enable_banking in pending/confirmed status filter (#1668)

The transaction status filter hardcoded only simplefin/plaid/lunchflow in
its pending/confirmed SQL, even though Transaction::PENDING_PROVIDERS also
includes enable_banking. As a result, Enable Banking pending transactions
returned 0 results under the "Pending" filter and leaked into "Confirmed".

Source the provider list from the existing constant-driven helpers instead:
- Transaction::Search delegates to the `pending` / `excluding_pending` model
  scopes.
- EntrySearch interpolates Transaction::PENDING_CHECK_SQL into its EXISTS
  subqueries.

This keeps every status-filter path in sync with PENDING_PROVIDERS so adding
a future provider can't reintroduce the bug.

Fixes #1668

* test(entry): cover EntrySearch status filter across all pending providers

Adds a regression test for the EntrySearch#apply_status_filter path,
asserting pending transactions for every PENDING_PROVIDERS entry are
matched by the "pending" filter and excluded from "confirmed". Mirrors
the existing Transaction::Search coverage so both filter paths are
exercised.
2026-05-31 22:17:56 +02:00

107 lines
3.3 KiB
Ruby

class EntrySearch
include ActiveModel::Model
include ActiveModel::Attributes
attribute :search, :string
attribute :amount, :string
attribute :amount_operator, :string
attribute :types, :string
attribute :status, array: true
attribute :accounts, array: true
attribute :account_ids, array: true
attribute :start_date, :string
attribute :end_date, :string
class << self
def apply_search_filter(scope, search)
return scope if search.blank?
query = scope
query = query.where("entries.name ILIKE :search OR entries.notes ILIKE :search",
search: "%#{ActiveRecord::Base.sanitize_sql_like(search)}%"
)
query
end
def apply_date_filters(scope, start_date, end_date)
return scope if start_date.blank? && end_date.blank?
query = scope
query = query.where("entries.date >= ?", start_date) if start_date.present?
query = query.where("entries.date <= ?", end_date) if end_date.present?
query
end
def apply_amount_filter(scope, amount, amount_operator)
return scope if amount.blank? || amount_operator.blank?
query = scope
case amount_operator
when "equal"
query = query.where("ABS(ABS(entries.amount) - ?) <= 0.01", amount.to_f.abs)
when "less"
query = query.where("ABS(entries.amount) < ?", amount.to_f.abs)
when "greater"
query = query.where("ABS(entries.amount) > ?", amount.to_f.abs)
end
query
end
def apply_accounts_filter(scope, accounts, account_ids)
return scope if accounts.blank? && account_ids.blank?
query = scope
query = query.where(accounts: { name: accounts }) if accounts.present?
query = query.where(accounts: { id: account_ids }) if account_ids.present?
query
end
def apply_status_filter(scope, statuses)
return 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.squish
entries.entryable_type = 'Transaction'
AND EXISTS (
SELECT 1 FROM transactions t
WHERE t.id = entries.entryable_id
AND (#{Transaction::PENDING_CHECK_SQL})
)
SQL
confirmed_condition = <<~SQL.squish
entries.entryable_type != 'Transaction'
OR NOT EXISTS (
SELECT 1 FROM transactions t
WHERE t.id = entries.entryable_id
AND (#{Transaction::PENDING_CHECK_SQL})
)
SQL
case statuses.sort
when [ "pending" ]
scope.where(pending_condition)
when [ "confirmed" ]
scope.where(confirmed_condition)
else
scope
end
end
end
def build_query(scope)
query = scope.joins(:account)
query = self.class.apply_search_filter(query, search)
query = self.class.apply_date_filters(query, start_date, end_date)
query = self.class.apply_amount_filter(query, amount, amount_operator)
query = self.class.apply_accounts_filter(query, accounts, account_ids)
query = self.class.apply_status_filter(query, status)
query
end
end