1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 07:39:39 +02:00
Maybe/app/models/transfer.rb
Zach Gollwitzer 297a695d0f
Transaction rules engine V1 (#1900)
* Domain model sketch

* Scaffold out rules domain

* Migrations

* Remove existing data enrichment for clean slate

* Sketch out business logic and basic tests

* Simplify rule scope building and action executions

* Get generator working again

* Basic implementation + tests

* Remove manual merchant management (rules will replace)

* Revert "Remove manual merchant management (rules will replace)"

This reverts commit 83dcbd9ff0.

* Family and Provider merchants model

* Fix brakeman warnings

* Fix notification loader

* Update notification position

* Add Rule action and condition registries

* Rule form with compound conditions and tests

* Split out notification types, add CTA type

* Rules form builder and Stimulus controller

* Clean up rule registry domain

* Clean up rules stimulus controller

* CTA message for rule when user changes transaction category

* Fix tests

* Lint updates

* Centralize notifications in Notifiable concern

* Implement category rule prompts with auto backoff and option to disable

* Fix layout bug caused by merge conflict

* Initialize rule with correct action for category CTA

* Add rule deletions, get rules working

* Complete dynamic rule form, split Stimulus controllers by resource

* Fix failing tests

* Change test password to avoid chromium conflicts

* Update integration tests

* Centralize all test password references

* Add re-apply rule action

* Rule confirm modal

* Run migrations

* Trigger rule notification after inline category updates

* Clean up rule styles

* Basic attribute locking for rules

* Apply attribute locks on user edits

* Log data enrichments, only apply rules to unlocked attributes

* Fix merge errors

* Additional merge conflict fixes

* Form UI improvements, ignore attribute locks on manual rule application

* Batch AI auto-categorization of transactions

* Auto merchant detection, ai enrichment in batches

* Fix Plaid merchant assignments

* Plaid category matching

* Cleanup 1

* Test cleanup

* Remove stale route

* Fix desktop chat UI issues

* Fix mobile nav styling issues
2025-04-18 11:39:58 -04:00

127 lines
3.9 KiB
Ruby

class Transfer < ApplicationRecord
belongs_to :inflow_transaction, class_name: "Transaction"
belongs_to :outflow_transaction, class_name: "Transaction"
enum :status, { pending: "pending", confirmed: "confirmed" }
validates :inflow_transaction_id, uniqueness: true
validates :outflow_transaction_id, uniqueness: true
validate :transfer_has_different_accounts
validate :transfer_has_opposite_amounts
validate :transfer_within_date_range
validate :transfer_has_same_family
class << self
def from_accounts(from_account:, to_account:, date:, amount:)
# Attempt to convert the amount to the to_account's currency.
# If the conversion fails, use the original amount.
converted_amount = begin
Money.new(amount.abs, from_account.currency).exchange_to(to_account.currency)
rescue Money::ConversionError
Money.new(amount.abs, from_account.currency)
end
new(
inflow_transaction: Transaction.new(
entry: to_account.entries.build(
amount: converted_amount.amount.abs * -1,
currency: converted_amount.currency.iso_code,
date: date,
name: "Transfer from #{from_account.name}",
)
),
outflow_transaction: Transaction.new(
entry: from_account.entries.build(
amount: amount.abs,
currency: from_account.currency,
date: date,
name: "Transfer to #{to_account.name}",
)
),
status: "confirmed"
)
end
end
def reject!
Transfer.transaction do
RejectedTransfer.find_or_create_by!(inflow_transaction_id: inflow_transaction_id, outflow_transaction_id: outflow_transaction_id)
destroy!
end
end
def confirm!
update!(status: "confirmed")
end
def sync_account_later
inflow_transaction.entry.sync_account_later
outflow_transaction.entry.sync_account_later
end
def belongs_to_family?(family)
family.transactions.include?(inflow_transaction)
end
def to_account
inflow_transaction.entry.account
end
def from_account
outflow_transaction.entry.account
end
def amount_abs
inflow_transaction.entry.amount_money.abs
end
def name
if payment?
I18n.t("transfer.payment_name", to_account: to_account.name)
else
I18n.t("transfer.name", to_account: to_account.name)
end
end
def payment?
to_account.liability?
end
def categorizable?
to_account.accountable_type == "Loan"
end
private
def transfer_has_different_accounts
return unless inflow_transaction.present? && outflow_transaction.present?
errors.add(:base, :must_be_from_different_accounts) if inflow_transaction.entry.account == outflow_transaction.entry.account
end
def transfer_has_same_family
return unless inflow_transaction.present? && outflow_transaction.present?
errors.add(:base, :must_be_from_same_family) unless inflow_transaction.entry.account.family == outflow_transaction.entry.account.family
end
def transfer_has_opposite_amounts
return unless inflow_transaction.present? && outflow_transaction.present?
inflow_amount = inflow_transaction.entry.amount
outflow_amount = outflow_transaction.entry.amount
if inflow_transaction.entry.currency == outflow_transaction.entry.currency
# For same currency, amounts must be exactly opposite
errors.add(:base, :must_have_opposite_amounts) if inflow_amount + outflow_amount != 0
else
# For different currencies, just check the signs are opposite
errors.add(:base, :must_have_opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?
end
end
def transfer_within_date_range
return unless inflow_transaction.present? && outflow_transaction.present?
date_diff = (inflow_transaction.entry.date - outflow_transaction.entry.date).abs
errors.add(:base, :must_be_within_date_range) if date_diff > 4
end
end