Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep Plaid securities for accounts with holdings but no investment transactions (#4256)

alexcastgonzeee0aa2c0665
diff --git a/app/models/plaid_item/accounts_snapshot.rb b/app/models/plaid_item/accounts_snapshot.rb
index b086544bb2..a3e3f86c3a 100644
--- a/app/models/plaid_item/accounts_snapshot.rb
+++ b/app/models/plaid_item/accounts_snapshot.rb
@@ -48,7 +48,10 @@ class PlaidItem::AccountsSnapshot
transactions = investments_data.transactions.select { |t| t.account_id == account_id }
holdings = investments_data.holdings.select { |h| h.account_id == account_id }
- securities = transactions.count > 0 && holdings.count > 0 ? investments_data.securities : []
+ # Holdings and investment transactions each resolve their security_id
+ # through this metadata, so either one needs it; an account with holdings
+ # but no investment transactions is common.
+ securities = transactions.any? || holdings.any? ? investments_data.securities : []
InvestmentsData.new(
transactions: transactions,
diff --git a/test/models/plaid_item/accounts_snapshot_test.rb b/test/models/plaid_item/accounts_snapshot_test.rb
index 433b3fe3c7..108a6c4529 100644
--- a/test/models/plaid_item/accounts_snapshot_test.rb
+++ b/test/models/plaid_item/accounts_snapshot_test.rb
@@ -94,6 +94,40 @@ class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
@snapshot.get_account_data("123")
end
+ test "keeps securities for an investment account with holdings but no investment transactions" do
+ @plaid_item.update!(available_products: [ "investments" ], billed_products: [])
+ @snapshot.stubs(:accounts).returns([ OpenStruct.new(account_id: "123", type: "investment") ])
+
+ security = OpenStruct.new(security_id: "sec_1", ticker_symbol: "VTI")
+ @plaid_provider.expects(:get_item_investments).with(@plaid_item.access_token).returns(
+ OpenStruct.new(
+ holdings: [ OpenStruct.new(account_id: "123", security_id: "sec_1") ],
+ transactions: [],
+ securities: [ security ]
+ )
+ )
+
+ investments = @snapshot.get_account_data("123").investments_data
+
+ assert_equal [ "sec_1" ], investments.holdings.map(&:security_id)
+ assert_equal [ security ], investments.securities
+ end
+
+ test "has no securities for an investment account with neither holdings nor transactions" do
+ @plaid_item.update!(available_products: [ "investments" ], billed_products: [])
+ @snapshot.stubs(:accounts).returns([ OpenStruct.new(account_id: "123", type: "investment") ])
+
+ @plaid_provider.expects(:get_item_investments).with(@plaid_item.access_token).returns(
+ OpenStruct.new(
+ holdings: [ OpenStruct.new(account_id: "other", security_id: "sec_1") ],
+ transactions: [],
+ securities: [ OpenStruct.new(security_id: "sec_1") ]
+ )
+ )
+
+ assert_empty @snapshot.get_account_data("123").investments_data.securities
+ end
+
test "does not fetch investments if no investment accounts" do
@plaid_item.update!(available_products: [ "investments" ], billed_products: [])