1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-10 16:05:22 +02:00

Merge branch 'main' of github.com:maybe-finance/maybe into zachgoll/design-audit

This commit is contained in:
Zach Gollwitzer 2025-04-30 17:59:17 -04:00
commit 1a64336905
2 changed files with 29 additions and 25 deletions

View file

@ -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

View file

@ -39,7 +39,7 @@ class TransferTest < ActiveSupport::TestCase
transfer.save transfer.save
end end
assert_equal "Transfer must have different accounts", transfer.errors.full_messages.first assert_equal "Must be from different accounts", transfer.errors.full_messages.first
end end
test "Transfer transactions must have opposite amounts" do test "Transfer transactions must have opposite amounts" do
@ -55,7 +55,7 @@ class TransferTest < ActiveSupport::TestCase
transfer.save transfer.save
end end
assert_equal "Transfer transactions must have opposite amounts", transfer.errors.full_messages.first assert_equal "Must have opposite amounts", transfer.errors.full_messages.first
end end
test "transfer dates must be within 4 days of each other" do test "transfer dates must be within 4 days of each other" do
@ -71,7 +71,7 @@ class TransferTest < ActiveSupport::TestCase
transfer.save transfer.save
end end
assert_equal "Transfer transaction dates must be within 4 days of each other", transfer.errors.full_messages.first assert_equal "Must be within 4 days", transfer.errors.full_messages.first
end end
test "transfer must be from the same family" do test "transfer must be from the same family" do
@ -90,7 +90,7 @@ class TransferTest < ActiveSupport::TestCase
) )
assert transfer.invalid? assert transfer.invalid?
assert_equal "Transfer must be from the same family", transfer.errors.full_messages.first assert_equal "Must be from same family", transfer.errors.full_messages.first
end end
test "from_accounts converts amounts to the to_account's currency" do test "from_accounts converts amounts to the to_account's currency" do