Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Add support for linking E*TRADE and other investment-only institutions (#3271)

terafinb7931e29bccf
diff --git a/app/models/provider/plaid.rb b/app/models/provider/plaid.rb
index a9a13fef94..17d5ddc5ab 100644
--- a/app/models/provider/plaid.rb
+++ b/app/models/provider/plaid.rb
@@ -185,6 +185,9 @@ class Provider::Plaid
[ transactions, securities ]
end
+ # The primary Plaid product to request for a given Sure account type.
+ # Investments map to the `investments` product, liability accounts
+ # (CreditCard/Loan) to `liabilities`, everything else to `transactions`.
def get_primary_product(accountable_type)
return "transactions" if eu?
@@ -198,10 +201,22 @@ class Provider::Plaid
end
end
+ # Additional Plaid products to consent to alongside the primary product.
+ #
+ # We only request the `liabilities` product when the account being linked is
+ # itself a liability (CreditCard/Loan). Plaid's Link filters out any
+ # institution that does not support every requested product, so asking for
+ # `liabilities` on e.g. an Investment link silently hides investment-only
+ # brokerages like E*TRADE that don't offer it.
def get_additional_consented_products(accountable_type)
return [] if eu?
- SUPPORTED_PLAID_PRODUCTS - [ get_primary_product(accountable_type) ]
+ case get_primary_product(accountable_type)
+ when "liabilities"
+ SUPPORTED_PLAID_PRODUCTS - [ "liabilities" ]
+ else
+ SUPPORTED_PLAID_PRODUCTS - [ get_primary_product(accountable_type), "liabilities" ]
+ end
end
def eu?
diff --git a/test/models/provider/plaid_test.rb b/test/models/provider/plaid_test.rb
index 7e71c6d4c6..48136723cf 100644
--- a/test/models/provider/plaid_test.rb
+++ b/test/models/provider/plaid_test.rb
@@ -32,6 +32,54 @@ class Provider::PlaidTest < ActiveSupport::TestCase
end
end
+ test "requests liability products only for liability account types" do
+ request = mock
+ Plaid::LinkTokenCreateRequest.expects(:new).with do |params|
+ params[:products] == [ "investments" ] &&
+ params[:additional_consented_products] == [ "transactions" ]
+ end.returns(request)
+ @plaid.client.expects(:link_token_create).with(request).returns("response")
+
+ @plaid.get_link_token(
+ user_id: "test-user-id",
+ webhooks_url: "https://example.com/webhooks",
+ redirect_url: @redirect_url,
+ accountable_type: "Investment"
+ )
+ end
+
+ test "does not request liabilities for a depository link" do
+ request = mock
+ Plaid::LinkTokenCreateRequest.expects(:new).with do |params|
+ params[:products] == [ "transactions" ] &&
+ params[:additional_consented_products] == [ "investments" ]
+ end.returns(request)
+ @plaid.client.expects(:link_token_create).with(request).returns("response")
+
+ @plaid.get_link_token(
+ user_id: "test-user-id",
+ webhooks_url: "https://example.com/webhooks",
+ redirect_url: @redirect_url,
+ accountable_type: "Depository"
+ )
+ end
+
+ test "requests liabilities as primary for a credit card link" do
+ request = mock
+ Plaid::LinkTokenCreateRequest.expects(:new).with do |params|
+ params[:products] == [ "liabilities" ] &&
+ params[:additional_consented_products] == [ "transactions", "investments" ]
+ end.returns(request)
+ @plaid.client.expects(:link_token_create).with(request).returns("response")
+
+ @plaid.get_link_token(
+ user_id: "test-user-id",
+ webhooks_url: "https://example.com/webhooks",
+ redirect_url: @redirect_url,
+ accountable_type: "CreditCard"
+ )
+ end
+
test "enables account selection for an update link token" do
request = mock
Plaid::LinkTokenCreateRequest.expects(:new).with do |params|