Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Auto-match cross-currency transfers on routes the user has confirmed (#3702)

Andrei Shkarin1b4634dd62ea
diff --git a/app/models/family/auto_transfer_matchable.rb b/app/models/family/auto_transfer_matchable.rb
index e74bd9c580..d3de897d96 100644
--- a/app/models/family/auto_transfer_matchable.rb
+++ b/app/models/family/auto_transfer_matchable.rb
@@ -197,6 +197,11 @@ module Family::AutoTransferMatchable
# branch additionally requires both accounts to have a live provider connection
# (Plaid/SimpleFIN/account_providers). Manual accounts always participate in the first
# branch's exact-amount, same-currency matching, which carries no such ambiguity.
+ # A user-confirmed transfer between the same two accounts, in the same direction,
+ # is the exception: it shows money really moves along that route (e.g. a manual
+ # RUB wallet that regularly converts into a manual THB account), so cross-currency
+ # auto-matching is allowed there too. Auto-matched transfers stay pending until the
+ # user confirms them.
#
# The restriction is opt-in (default false) rather than baked into the branch
# unconditionally: Family#auto_match_transfers! passes true because a coincidental match
@@ -300,6 +305,28 @@ module Family::AutoTransferMatchable
inflow_family_rates.from_currency = inflow_candidates.currency AND
inflow_family_rates.to_currency = :family_currency
)
+ LEFT JOIN (
+ SELECT DISTINCT
+ route_outflows.account_id AS outflow_account_id,
+ route_inflows.account_id AS inflow_account_id
+ FROM transfers confirmed_transfers
+ JOIN entries route_outflows ON (
+ route_outflows.entryable_type = 'Transaction' AND
+ route_outflows.entryable_id = confirmed_transfers.outflow_transaction_id
+ )
+ JOIN entries route_inflows ON (
+ route_inflows.entryable_type = 'Transaction' AND
+ route_inflows.entryable_id = confirmed_transfers.inflow_transaction_id
+ )
+ JOIN accounts route_accounts ON route_accounts.id = route_outflows.account_id
+ WHERE
+ :restrict_cross_currency_to_linked_accounts = TRUE AND
+ confirmed_transfers.status = 'confirmed' AND
+ route_accounts.family_id = :family_id
+ ) confirmed_routes ON (
+ confirmed_routes.outflow_account_id = outflow_candidates.account_id AND
+ confirmed_routes.inflow_account_id = inflow_candidates.account_id
+ )
LEFT JOIN transfers existing_transfers ON (
existing_transfers.inflow_transaction_id = inflow_candidates.entryable_id OR
existing_transfers.outflow_transaction_id = outflow_candidates.entryable_id
@@ -329,7 +356,8 @@ module Family::AutoTransferMatchable
(:include_rejected = TRUE OR rejected_transfers.id IS NULL) AND
(
:restrict_cross_currency_to_linked_accounts = FALSE OR
- (#{linked_account_sql("inflow_accounts")} AND #{linked_account_sql("outflow_accounts")})
+ (#{linked_account_sql("inflow_accounts")} AND #{linked_account_sql("outflow_accounts")}) OR
+ confirmed_routes.outflow_account_id IS NOT NULL
) AND
#{common_writer_condition_sql}
) transfer_match_candidates
diff --git a/test/models/family/auto_transfer_matchable_test.rb b/test/models/family/auto_transfer_matchable_test.rb
index 44b54b8fb3..fee0cf8411 100644
--- a/test/models/family/auto_transfer_matchable_test.rb
+++ b/test/models/family/auto_transfer_matchable_test.rb
@@ -8,6 +8,8 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
@depository = accounts(:depository)
@credit_card = accounts(:credit_card)
@loan = accounts(:loan)
+ # A manual account with no fixture transfers to or from @depository
+ @other_asset = accounts(:other_asset)
end
test "auto-matches transfers" do
@@ -177,6 +179,45 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
end
end
+ test "auto-matches cross-currency transfers between manual accounts with a confirmed transfer in the same direction" do
+ load_family_currency_rates
+ confirm_transfer!(from: @depository, to: @other_asset)
+
+ outflow = create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 400, currency: "GBP")
+ inflow = create_transaction(date: Date.current, account: @other_asset, amount: -680, currency: "CAD")
+
+ assert_difference -> { Transfer.count } => 1 do
+ @family.auto_match_transfers!
+ end
+
+ transfer = Transfer.find_by!(inflow_transaction_id: inflow.entryable_id, outflow_transaction_id: outflow.entryable_id)
+ assert_equal "pending", transfer.status
+ end
+
+ test "a confirmed transfer in the opposite direction does not enable cross-currency auto-matching for manual accounts" do
+ load_family_currency_rates
+ confirm_transfer!(from: @other_asset, to: @depository)
+
+ create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 400, currency: "GBP")
+ create_transaction(date: Date.current, account: @other_asset, amount: -680, currency: "CAD")
+
+ assert_no_difference -> { Transfer.count } do
+ @family.auto_match_transfers!
+ end
+ end
+
+ test "a pending transfer between manual accounts does not enable cross-currency auto-matching" do
+ load_family_currency_rates
+ confirm_transfer!(from: @depository, to: @other_asset).update!(status: "pending")
+
+ create_transaction(date: 1.day.ago.to_date, account: @depository, amount: 400, currency: "GBP")
+ create_transaction(date: Date.current, account: @other_asset, amount: -680, currency: "CAD")
+
+ assert_no_difference -> { Transfer.count } do
+ @family.auto_match_transfers!
+ end
+ end
+
test "transfer_match_candidates does not restrict cross-currency matches to linked accounts by default" do
load_exchange_prices
# Neither account is linked. auto_match_transfers! would exclude this pair
@@ -756,6 +797,12 @@ class Family::AutoTransferMatchableTest < ActiveSupport::TestCase
end
end
+ def confirm_transfer!(from:, to:)
+ outflow = create_transaction(date: 20.days.ago.to_date, account: from, amount: 100)
+ inflow = create_transaction(date: 20.days.ago.to_date, account: to, amount: -100)
+ Transfer.create!(inflow_transaction: inflow.transaction, outflow_transaction: outflow.transaction, status: "confirmed")
+ end
+
# Only rates to the family currency (USD) are synced; there is no direct GBP -> CAD rate
def load_family_currency_rates
ExchangeRate.create!(from_currency: "GBP", to_currency: "USD", date: 1.day.ago.to_date, rate: 1.25)