1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 21:15:19 +02:00

Account:: namespace simplifications and cleanup (#2110)

* Flatten Holding model

* Flatten balance model

* Entries domain renames

* Fix valuations reference

* Fix trades stream

* Fix brakeman warnings

* Fix tests

* Replace existing entryable type references in DB
This commit is contained in:
Zach Gollwitzer 2025-04-14 11:40:34 -04:00 committed by GitHub
parent f181ba941f
commit e657c40d19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
172 changed files with 1297 additions and 1258 deletions

View file

@ -0,0 +1,20 @@
module Transaction::Provided
extend ActiveSupport::Concern
def fetch_enrichment_info
return nil unless provider
response = provider.enrich_transaction(
entry.name,
amount: entry.amount,
date: entry.date
)
response.data
end
private
def provider
Provider::Registry.get_provider(:synth)
end
end

View file

@ -0,0 +1,101 @@
class Transaction::Search
include ActiveModel::Model
include ActiveModel::Attributes
attribute :search, :string
attribute :amount, :string
attribute :amount_operator, :string
attribute :types, array: true
attribute :accounts, array: true
attribute :account_ids, array: true
attribute :start_date, :string
attribute :end_date, :string
attribute :categories, array: true
attribute :merchants, array: true
attribute :tags, array: true
def build_query(scope)
query = scope.joins(entry: :account)
.joins(transfer_join)
query = apply_category_filter(query, categories)
query = apply_type_filter(query, types)
query = apply_merchant_filter(query, merchants)
query = apply_tag_filter(query, tags)
query = EntrySearch.apply_search_filter(query, search)
query = EntrySearch.apply_date_filters(query, start_date, end_date)
query = EntrySearch.apply_amount_filter(query, amount, amount_operator)
query = EntrySearch.apply_accounts_filter(query, accounts, account_ids)
query
end
private
def transfer_join
<<~SQL
LEFT JOIN (
SELECT t.*, t.id as transfer_id, a.accountable_type
FROM transfers t
JOIN entries ae ON ae.entryable_id = t.inflow_transaction_id
AND ae.entryable_type = 'Transaction'
JOIN accounts a ON a.id = ae.account_id
) transfer_info ON (
transfer_info.inflow_transaction_id = transactions.id OR
transfer_info.outflow_transaction_id = transactions.id
)
SQL
end
def apply_category_filter(query, categories)
return query unless categories.present?
query = query.left_joins(:category).where(
"categories.name IN (?) OR (
categories.id IS NULL AND (transfer_info.transfer_id IS NULL OR transfer_info.accountable_type = 'Loan')
)",
categories
)
if categories.exclude?("Uncategorized")
query = query.where.not(category_id: nil)
end
query
end
def apply_type_filter(query, types)
return query unless types.present?
return query if types.sort == [ "expense", "income", "transfer" ]
transfer_condition = "transfer_info.transfer_id IS NOT NULL"
expense_condition = "entries.amount >= 0"
income_condition = "entries.amount <= 0"
condition = case types.sort
when [ "transfer" ]
transfer_condition
when [ "expense" ]
Arel.sql("#{expense_condition} AND NOT (#{transfer_condition})")
when [ "income" ]
Arel.sql("#{income_condition} AND NOT (#{transfer_condition})")
when [ "expense", "transfer" ]
Arel.sql("#{expense_condition} OR #{transfer_condition}")
when [ "income", "transfer" ]
Arel.sql("#{income_condition} OR #{transfer_condition}")
when [ "expense", "income" ]
Arel.sql("NOT (#{transfer_condition})")
end
query.where(condition)
end
def apply_merchant_filter(query, merchants)
return query unless merchants.present?
query.joins(:merchant).where(merchants: { name: merchants })
end
def apply_tag_filter(query, tags)
return query unless tags.present?
query.joins(:tags).where(tags: { name: tags })
end
end

View file

@ -0,0 +1,40 @@
module Transaction::Transferable
extend ActiveSupport::Concern
included do
has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy
has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy
# We keep track of rejected transfers to avoid auto-matching them again
has_one :rejected_transfer_as_inflow, class_name: "RejectedTransfer", foreign_key: "inflow_transaction_id", dependent: :destroy
has_one :rejected_transfer_as_outflow, class_name: "RejectedTransfer", foreign_key: "outflow_transaction_id", dependent: :destroy
end
def transfer
transfer_as_inflow || transfer_as_outflow
end
def transfer?
transfer.present?
end
def transfer_match_candidates
candidates_scope = if self.entry.amount.negative?
family_matches_scope.where("inflow_candidates.entryable_id = ?", self.id)
else
family_matches_scope.where("outflow_candidates.entryable_id = ?", self.id)
end
candidates_scope.map do |match|
Transfer.new(
inflow_transaction_id: match.inflow_transaction_id,
outflow_transaction_id: match.outflow_transaction_id,
)
end
end
private
def family_matches_scope
self.entry.account.family.transfer_match_candidates
end
end