The personal finance app for everyone (by everyone)
Ruby
Drop calculated holdings shadowed by a provider snapshot in any currency (#4274)
alexcastgonz50dba9604db0
diff --git a/app/models/holding/materializer.rb b/app/models/holding/materializer.rbindex c6b5a92c77..adaf4c67a7 100644--- a/app/models/holding/materializer.rb+++ b/app/models/holding/materializer.rb@@ -182,7 +182,9 @@ class Holding::Materializerend# Remove only calculated holdings that collide with an authoritative provider snapshot- # on the exact same key. This preserves reverse-calculated history for linked accounts.+ # for the same security and date. This preserves reverse-calculated history for linked accounts.+ # Currency is not part of the match: providers can report a position in its own currency while+ # calculated rows are always in the account currency, and keeping both counts the position twice.def cleanup_shadowed_calculated_holdingsdeleted_count = account.holdings.where(account_provider_id: nil)@@ -193,7 +195,6 @@ class Holding::MaterializerWHERE provider_holdings.account_id = holdings.account_idAND provider_holdings.security_id = holdings.security_idAND provider_holdings.date = holdings.date- AND provider_holdings.currency = holdings.currencyAND provider_holdings.account_provider_id IS NOT NULL)SQLdiff --git a/test/models/holding/materializer_test.rb b/test/models/holding/materializer_test.rbindex a59a77bbbb..87130c6290 100644--- a/test/models/holding/materializer_test.rb+++ b/test/models/holding/materializer_test.rb@@ -267,6 +267,39 @@ class Holding::MaterializerTest < ActiveSupport::TestCaseassert_equal [ account_provider.id ], today_holdings.pluck(:account_provider_id)end+ test "cleans up calculated holdings on earlier provider snapshot dates when the snapshot is in another currency" do+ [ Date.yesterday, Date.current ].each do |date|+ ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", date: date, rate: 1.2)+ end++ coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")+ coinstats_account = coinstats_item.coinstats_accounts.create!(+ name: "Brokerage",+ currency: "USD"+ )+ account_provider = AccountProvider.create!(account: @account, provider: coinstats_account)++ [ Date.yesterday, Date.current ].each do |date|+ Holding.create!(+ account: @account,+ security: @aapl,+ qty: 10,+ price: 200,+ amount: 2000,+ currency: "EUR",+ date: date,+ account_provider: account_provider+ )+ end++ Holding::Materializer.new(@account, strategy: :reverse).materialize_holdings++ yesterday_holdings = @account.holdings.where(security: @aapl, date: Date.yesterday)++ assert_equal [ "EUR" ], yesterday_holdings.pluck(:currency)+ assert_equal [ account_provider.id ], yesterday_holdings.pluck(:account_provider_id)+ end+test "carries forward provider cost_basis to calculated rows past the provider snapshot date" docoinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Brokerage", currency: "USD")