Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Carry holding locks forward without an external_id (#4260)

jaysbeekay8d7e8e09538a
diff --git a/app/models/account/provider_import_adapter.rb b/app/models/account/provider_import_adapter.rb
index 1485cf9cbb..6aec221c3d 100644
--- a/app/models/account/provider_import_adapter.rb
+++ b/app/models/account/provider_import_adapter.rb
@@ -489,7 +489,15 @@ class Account::ProviderImportAdapter
account_provider_id: account_provider_id
)
else
- holding = account.holdings.find_or_initialize_by(
+ # A remapped row sits on the user's chosen security, so a lookup by the
+ # incoming security alone misses it and the save hits the unique index.
+ # Match it by provider_security first, as fallback 1a does, scoped by
+ # account_provider_id so another provider's row is not claimed.
+ remapped_attrs = { provider_security: security, date: date, currency: currency }
+ remapped_attrs[:account_provider_id] = account_provider_id if account_provider_id.present?
+ holding = account.holdings.find_by(remapped_attrs)
+
+ holding ||= account.holdings.find_or_initialize_by(
security: security,
date: date,
currency: currency
@@ -1065,18 +1073,33 @@ class Account::ProviderImportAdapter
# The latest earlier holding of the security the provider reports. A
# remapped row records that security in provider_security_id; an
- # unremapped one has it as its own security. Only the latest row counts, so
- # a correction the user has since undone is not revived from an older day.
+ # unremapped one has it as its own security. A provider that reports the
+ # user's chosen security instead still finds the row the user locked to
+ # it. Only the latest row counts, so a correction the user has since
+ # undone is not revived from an older day.
+ #
+ # Within that day, a row recording this provider security is the position
+ # it came from, and wins over a correction locked to it: with chained
+ # remaps (A onto B, B onto C) an incoming B matches both the B->C row and
+ # the A->B row on the same date. `id` settles anything left, so the pick
+ # never depends on the query plan.
def previous_holding_for(security:, currency:, date:, account_provider_id:)
scope = account.holdings
.where(currency: currency)
.where("holdings.date < ?", date)
.where(
- "holdings.provider_security_id = :id OR (holdings.provider_security_id IS NULL AND holdings.security_id = :id)",
+ "holdings.provider_security_id = :id OR (holdings.provider_security_id IS NULL AND holdings.security_id = :id) " \
+ "OR (holdings.security_locked AND holdings.security_id = :id)",
id: security.id
)
scope = scope.where(account_provider_id: account_provider_id) if account_provider_id.present?
- scope.order(date: :desc).first
+
+ match_rank = ActiveRecord::Base.sanitize_sql_array([
+ "CASE WHEN holdings.provider_security_id = :id THEN 0 " \
+ "WHEN holdings.provider_security_id IS NULL AND holdings.security_id = :id THEN 1 ELSE 2 END",
+ id: security.id
+ ])
+ scope.order(date: :desc).order(Arel.sql(match_rank)).order(:id).first
end
# Loads the transfer together with both legs, their entries and accounts in
diff --git a/test/models/account/provider_import_adapter_test.rb b/test/models/account/provider_import_adapter_test.rb
index decdc32a18..6b263c810b 100644
--- a/test/models/account/provider_import_adapter_test.rb
+++ b/test/models/account/provider_import_adapter_test.rb
@@ -775,6 +775,111 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase
assert_not third_day.cost_basis_locked?
end
+ test "a second same-day import of a remapped position's provider row updates today's row" do
+ account, adapter, account_provider = provider_holding_setup
+ original = Security.create!(ticker: "DUPE", name: "Provider Row")
+ chosen = Security.create!(ticker: "DUPE", name: "Chosen Row", exchange_operating_mic: "XNYS")
+ yesterday = Date.current - 1
+
+ import_holding_without_external_id(adapter, account_provider, original, yesterday).remap_security!(chosen)
+ import_holding_without_external_id(adapter, account_provider, original, Date.current)
+
+ assert_nothing_raised do
+ import_holding_without_external_id(adapter, account_provider, original, Date.current, quantity: 12)
+ end
+
+ today = account.holdings.where(security: [ original, chosen ], date: Date.current, currency: "USD")
+ assert_equal [ chosen.id ], today.pluck(:security_id)
+ assert_equal 12, today.first.qty
+ end
+
+ test "a remapped position's chosen row carries the lock and the original provider row forward" do
+ account, adapter, account_provider = provider_holding_setup
+ original = Security.create!(ticker: "PICK", name: "Provider Row")
+ chosen = Security.create!(ticker: "PICK", name: "Chosen Row", exchange_operating_mic: "XNYS")
+ yesterday = Date.current - 1
+
+ earlier = import_holding_without_external_id(adapter, account_provider, original, yesterday)
+ earlier.remap_security!(chosen)
+ earlier.set_manual_cost_basis!(42)
+
+ today = import_holding_without_external_id(adapter, account_provider, chosen, Date.current, cost_basis: 10)
+
+ assert_equal chosen.id, today.security_id
+ assert today.security_locked?
+ assert_equal original.id, today.provider_security_id
+ assert_equal 42, today.cost_basis
+ assert today.cost_basis_locked?
+ assert_equal 1, account.holdings.where(security: [ original, chosen ], date: Date.current).count
+ end
+
+ # Chained remaps: the user moved the provider's A onto B, and the provider's B
+ # onto C. An incoming B matches yesterday's B->C row by provider security and
+ # the A->B row as a correction locked to B, both on the same date. The
+ # provider-security match is the position B came from, so it wins. The A->B
+ # row is created first and has the lower id, so neither insertion order nor
+ # an id tiebreak alone would return the right one.
+ test "a chained remap carries forward the row the provider security came from" do
+ account, adapter, account_provider = provider_holding_setup
+ first = Security.create!(ticker: "CHNA", name: "Provider A")
+ middle = Security.create!(ticker: "CHNB", name: "Provider B, chosen for A")
+ last = Security.create!(ticker: "CHNC", name: "Chosen for B")
+ yesterday = Date.current - 1
+ account.holdings.create!(
+ id: "00000000-0000-4000-8000-000000000001", security: middle, provider_security: first, security_locked: true, account_provider: account_provider,
+ date: yesterday, currency: "USD", qty: 1, price: 10, amount: 10, cost_basis: 11, cost_basis_locked: true
+ )
+ account.holdings.create!(
+ id: "ffffffff-ffff-4fff-bfff-ffffffffffff", security: last, provider_security: middle, security_locked: true, account_provider: account_provider,
+ date: yesterday, currency: "USD", qty: 2, price: 10, amount: 20, cost_basis: 22, cost_basis_locked: true
+ )
+
+ today = import_holding_without_external_id(adapter, account_provider, middle, Date.current)
+
+ assert_equal last.id, today.security_id
+ assert_equal middle.id, today.provider_security_id
+ assert_equal 22, today.cost_basis
+ end
+
+ test "another provider's same-day row with that provider security is not claimed" do
+ account, adapter, account_provider = provider_holding_setup
+ other_provider = AccountProvider.create!(account: account, provider: snaptrade_accounts(:vanguard_ira))
+ original = Security.create!(ticker: "MINE", name: "Provider Row")
+ chosen = Security.create!(ticker: "MINE", name: "Chosen Row", exchange_operating_mic: "XNYS")
+ others = account.holdings.create!(
+ security: chosen, provider_security: original, security_locked: true, account_provider: other_provider,
+ date: Date.current, currency: "USD", qty: 5, price: 100, amount: 500
+ )
+
+ ours = import_holding_without_external_id(adapter, account_provider, original, Date.current)
+
+ assert_not_equal others.id, ours.id
+ assert_equal original.id, ours.security_id
+ assert_equal account_provider.id, ours.account_provider_id
+ others.reload
+ assert_equal 5, others.qty
+ assert_equal other_provider.id, others.account_provider_id
+ end
+
+ test "an unlocked earlier row on the chosen security is not treated as a correction" do
+ account, adapter, account_provider = provider_holding_setup
+ original = Security.create!(ticker: "OPEN", name: "Provider Row")
+ chosen = Security.create!(ticker: "OPEN", name: "Chosen Row", exchange_operating_mic: "XNYS")
+ account.holdings.create!(
+ security: chosen, provider_security: original, security_locked: false, account_provider: account_provider,
+ date: Date.current - 1, currency: "USD", qty: 10, price: 100, amount: 1000,
+ cost_basis: 42, cost_basis_source: "manual", cost_basis_locked: true
+ )
+
+ today = import_holding_without_external_id(adapter, account_provider, chosen, Date.current, cost_basis: 10)
+
+ assert_not today.security_locked?
+ assert_equal chosen.id, today.provider_security_id
+ assert_equal 10, today.cost_basis
+ assert_equal "provider", today.cost_basis_source
+ assert_not today.cost_basis_locked?
+ end
+
test "does not delete future holdings when can_delete_holdings? returns false" do
investment_account = accounts(:investment)
adapter = Account::ProviderImportAdapter.new(investment_account)
@@ -2335,4 +2440,20 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase
account_provider_id: account_provider.id
)
end
+
+ # A provider that sends no external_id (Plaid, Indexa Capital, SnapTrade
+ # securities): the row is keyed by security, date and currency.
+ def import_holding_without_external_id(adapter, account_provider, security, date, quantity: 10, cost_basis: nil)
+ adapter.import_holding(
+ security: security,
+ quantity: quantity,
+ amount: quantity * 100,
+ currency: "USD",
+ date: date,
+ price: 100,
+ cost_basis: cost_basis,
+ source: "plaid",
+ account_provider_id: account_provider.id
+ )
+ end
end