1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 13:19:39 +02:00

Add scope to filter transactions from active accounts (#1810)

* Add scope to filter transactions from active accounts

* Add test for transfer match candidates with active accounts

* Refactor active account filtering for transactions and entries
This commit is contained in:
Josh Pigford 2025-02-05 11:52:44 -06:00 committed by GitHub
parent cf014bc24f
commit 4aba9d1c0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 69 additions and 1 deletions

View file

@ -7,7 +7,7 @@ class TransactionsController < ApplicationController
def index def index
@q = search_params @q = search_params
search_query = Current.family.transactions.search(@q) search_query = Current.family.transactions.search(@q).active
set_focused_record(search_query, params[:focused_record_id], default_per_page: 50) set_focused_record(search_query, params[:focused_record_id], default_per_page: 50)

View file

@ -184,6 +184,8 @@ class Account < ApplicationRecord
.joins("JOIN accounts inflow_accounts ON inflow_accounts.id = inflow_candidates.account_id") .joins("JOIN accounts inflow_accounts ON inflow_accounts.id = inflow_candidates.account_id")
.joins("JOIN accounts outflow_accounts ON outflow_accounts.id = outflow_candidates.account_id") .joins("JOIN accounts outflow_accounts ON outflow_accounts.id = outflow_candidates.account_id")
.where("inflow_accounts.family_id = ? AND outflow_accounts.family_id = ?", self.family_id, self.family_id) .where("inflow_accounts.family_id = ? AND outflow_accounts.family_id = ?", self.family_id, self.family_id)
.where("inflow_accounts.is_active = true AND inflow_accounts.scheduled_for_deletion = false")
.where("outflow_accounts.is_active = true AND outflow_accounts.scheduled_for_deletion = false")
.where("inflow_candidates.entryable_type = 'Account::Transaction' AND outflow_candidates.entryable_type = 'Account::Transaction'") .where("inflow_candidates.entryable_type = 'Account::Transaction' AND outflow_candidates.entryable_type = 'Account::Transaction'")
.where(existing_transfers: { id: nil }) .where(existing_transfers: { id: nil })
.order("date_diff ASC") # Closest matches first .order("date_diff ASC") # Closest matches first

View file

@ -24,6 +24,10 @@ class Account::Entry < ApplicationRecord
) )
} }
scope :active, -> {
joins(:account).where(accounts: { is_active: true, scheduled_for_deletion: false })
}
scope :reverse_chronological, -> { scope :reverse_chronological, -> {
order( order(
date: :desc, date: :desc,

View file

@ -81,4 +81,27 @@ class Account::EntryTest < ActiveSupport::TestCase
assert_equal 200, totals.expense_total assert_equal 200, totals.expense_total
assert_equal "USD", totals.currency assert_equal "USD", totals.currency
end end
test "active scope only returns entries from active, non-scheduled-for-deletion accounts" do
# Create transactions for all account types
active_transaction = create_transaction(account: accounts(:depository), name: "Active transaction")
inactive_transaction = create_transaction(account: accounts(:credit_card), name: "Inactive transaction")
deletion_transaction = create_transaction(account: accounts(:investment), name: "Scheduled for deletion transaction")
# Update account statuses
accounts(:credit_card).update!(is_active: false)
accounts(:investment).update!(scheduled_for_deletion: true)
# Test the scope
active_entries = Account::Entry.active
# Should include entry from active account
assert_includes active_entries, active_transaction
# Should not include entry from inactive account
assert_not_includes active_entries, inactive_transaction
# Should not include entry from account scheduled for deletion
assert_not_includes active_entries, deletion_transaction
end
end end

View file

@ -99,4 +99,43 @@ class AccountTest < ActiveSupport::TestCase
@account.auto_match_transfers! @account.auto_match_transfers!
end end
end end
test "transfer_match_candidates only matches between active accounts" do
active_account = accounts(:depository)
another_active_account = accounts(:credit_card)
inactive_account = accounts(:investment)
inactive_account.update!(is_active: false)
# Create matching transactions
active_inflow = active_account.entries.create!(
date: Date.current,
amount: -100,
currency: "USD",
name: "Test transfer",
entryable: Account::Transaction.new
)
active_outflow = another_active_account.entries.create!(
date: Date.current,
amount: 100,
currency: "USD",
name: "Test transfer",
entryable: Account::Transaction.new
)
inactive_outflow = inactive_account.entries.create!(
date: Date.current,
amount: 100,
currency: "USD",
name: "Test transfer",
entryable: Account::Transaction.new
)
# Should find matches between active accounts
candidates = active_account.transfer_match_candidates
assert_includes candidates.map(&:outflow_transaction_id), active_outflow.entryable_id
# Should not match with inactive account
assert_not_includes candidates.map(&:outflow_transaction_id), inactive_outflow.entryable_id
end
end end