mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-26 00:29:40 +02:00
Refactor Transfer model to use safe navigation for entry associations and improve error messages for account validation
This commit is contained in:
parent
d8e34cf791
commit
ae41b3de46
1 changed files with 25 additions and 21 deletions
|
@ -56,8 +56,8 @@ class Transfer < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync_account_later
|
def sync_account_later
|
||||||
inflow_transaction.entry.sync_account_later
|
inflow_transaction&.entry&.sync_account_later
|
||||||
outflow_transaction.entry.sync_account_later
|
outflow_transaction&.entry&.sync_account_later
|
||||||
end
|
end
|
||||||
|
|
||||||
def belongs_to_family?(family)
|
def belongs_to_family?(family)
|
||||||
|
@ -65,63 +65,67 @@ class Transfer < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_account
|
def to_account
|
||||||
inflow_transaction.entry.account
|
inflow_transaction&.entry&.account
|
||||||
end
|
end
|
||||||
|
|
||||||
def from_account
|
def from_account
|
||||||
outflow_transaction.entry.account
|
outflow_transaction&.entry&.account
|
||||||
end
|
end
|
||||||
|
|
||||||
def amount_abs
|
def amount_abs
|
||||||
inflow_transaction.entry.amount_money.abs
|
inflow_transaction&.entry&.amount_money&.abs
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
acc = to_account
|
||||||
if payment?
|
if payment?
|
||||||
I18n.t("transfer.payment_name", to_account: to_account.name)
|
acc ? "Payment to #{acc.name}" : "Payment"
|
||||||
else
|
else
|
||||||
I18n.t("transfer.name", to_account: to_account.name)
|
acc ? "Transfer to #{acc.name}" : "Transfer"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def payment?
|
def payment?
|
||||||
to_account.liability?
|
to_account&.liability?
|
||||||
end
|
end
|
||||||
|
|
||||||
def categorizable?
|
def categorizable?
|
||||||
to_account.accountable_type == "Loan"
|
to_account&.accountable_type == "Loan"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def transfer_has_different_accounts
|
def transfer_has_different_accounts
|
||||||
return unless inflow_transaction.present? && outflow_transaction.present?
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
||||||
errors.add(:base, :must_be_from_different_accounts) if inflow_transaction.entry.account == outflow_transaction.entry.account
|
errors.add(:base, "Must be from different accounts") if to_account == from_account
|
||||||
end
|
end
|
||||||
|
|
||||||
def transfer_has_same_family
|
def transfer_has_same_family
|
||||||
return unless inflow_transaction.present? && outflow_transaction.present?
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
||||||
errors.add(:base, :must_be_from_same_family) unless inflow_transaction.entry.account.family == outflow_transaction.entry.account.family
|
errors.add(:base, "Must be from same family") unless to_account&.family == from_account&.family
|
||||||
end
|
end
|
||||||
|
|
||||||
def transfer_has_opposite_amounts
|
def transfer_has_opposite_amounts
|
||||||
return unless inflow_transaction.present? && outflow_transaction.present?
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
||||||
|
|
||||||
inflow_amount = inflow_transaction.entry.amount
|
inflow_entry = inflow_transaction.entry
|
||||||
outflow_amount = outflow_transaction.entry.amount
|
outflow_entry = outflow_transaction.entry
|
||||||
|
|
||||||
if inflow_transaction.entry.currency == outflow_transaction.entry.currency
|
inflow_amount = inflow_entry.amount
|
||||||
|
outflow_amount = outflow_entry.amount
|
||||||
|
|
||||||
|
if inflow_entry.currency == outflow_entry.currency
|
||||||
# For same currency, amounts must be exactly opposite
|
# For same currency, amounts must be exactly opposite
|
||||||
errors.add(:base, :must_have_opposite_amounts) if inflow_amount + outflow_amount != 0
|
errors.add(:base, "Must have opposite amounts") if inflow_amount + outflow_amount != 0
|
||||||
else
|
else
|
||||||
# For different currencies, just check the signs are opposite
|
# For different currencies, just check the signs are opposite
|
||||||
errors.add(:base, :must_have_opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?
|
errors.add(:base, "Must have opposite amounts") unless inflow_amount.negative? && outflow_amount.positive?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def transfer_within_date_range
|
def transfer_within_date_range
|
||||||
return unless inflow_transaction.present? && outflow_transaction.present?
|
return unless inflow_transaction&.entry && outflow_transaction&.entry
|
||||||
|
|
||||||
date_diff = (inflow_transaction.entry.date - outflow_transaction.entry.date).abs
|
date_diff = (inflow_transaction.entry.date - outflow_transaction.entry.date).abs
|
||||||
errors.add(:base, :must_be_within_date_range) if date_diff > 4
|
errors.add(:base, "Must be within 4 days") if date_diff > 4
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue