Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Filter get_holdings by ticker through the holding's security (#4492)

jaysbeekay7b4b2a767c45
diff --git a/app/models/assistant/function/get_holdings.rb b/app/models/assistant/function/get_holdings.rb
index 05d7330b90..f349087ec9 100644
--- a/app/models/assistant/function/get_holdings.rb
+++ b/app/models/assistant/function/get_holdings.rb
@@ -140,8 +140,7 @@ class Assistant::Function::GetHoldings < Assistant::Function
)
if params["securities"].present?
- security_ids = family.securities.where(ticker: params["securities"]).pluck(:id)
- holdings = holdings.where(security_id: security_ids)
+ holdings = holdings.joins(:security).where(securities: { ticker: params["securities"] })
end
holdings
diff --git a/test/models/assistant/function/get_holdings_test.rb b/test/models/assistant/function/get_holdings_test.rb
new file mode 100644
index 0000000000..a2dbdcb2ae
--- /dev/null
+++ b/test/models/assistant/function/get_holdings_test.rb
@@ -0,0 +1,42 @@
+require "test_helper"
+
+class Assistant::Function::GetHoldingsTest < ActiveSupport::TestCase
+ setup do
+ @user = users(:family_admin)
+ @fn = Assistant::Function::GetHoldings.new(@user)
+ @account = accounts(:investment)
+ # The fixtures hold only AAPL, so a second ticker on the same account is
+ # what tells a filter from no filter.
+ @account.holdings.create!(security: securities(:msft), date: Date.current, qty: 3, price: 400, amount: 1200, currency: "USD")
+ end
+
+ test "get_holdings filters by security ticker" do
+ assert_equal [ "AAPL" ], tickers("securities" => [ "AAPL" ])
+ assert_equal [ "MSFT" ], tickers("securities" => [ "MSFT" ])
+ assert_equal 1, @fn.call("page" => 1, "securities" => [ "MSFT" ])[:total_results]
+ end
+
+ test "get_holdings security filter for a ticker nobody holds returns nothing" do
+ assert_empty tickers("securities" => [ "NOPE" ])
+ end
+
+ test "get_holdings security filter does not reach another user's private account" do
+ private_account = @user.family.accounts.create!(name: "Member Brokerage", accountable: Investment.new, balance: 0, currency: "USD", owner: users(:family_member))
+ hidden = private_account.holdings.create!(security: securities(:aapl), date: Date.current, qty: 9, price: 200, amount: 1800, currency: "USD")
+
+ rows = @fn.call("page" => 1, "securities" => [ "AAPL" ])[:holdings]
+
+ assert_includes rows.map { |row| row[:account] }, @account.name, "the user's own AAPL went too"
+ assert_not_includes rows.map { |row| row[:account] }, hidden.account.name
+ end
+
+ test "get_holdings without a security filter is unchanged" do
+ assert_equal [ "AAPL", "MSFT" ], tickers({})
+ end
+
+ private
+
+ def tickers(params)
+ @fn.call({ "page" => 1 }.merge(params))[:holdings].map { |row| row[:ticker] }.uniq.sort
+ end
+end