The personal finance app for everyone (by everyone)
Ruby
Treat consented Plaid products as supported (#3537)
Brian Espinoza76143d00e5ad
diff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rbindex 68a3a16f19..b95f3a32b9 100644--- a/app/models/plaid_item.rb+++ b/app/models/plaid_item.rb@@ -198,10 +198,22 @@ class PlaidItem < ApplicationRecordendend- # Plaid returns mutually exclusive arrays here. If the item has made a request for a product,- # it is put in the billed_products array. If it is supported, but not yet used, it goes in the- # available_products array.+ # Plaid splits an item's products across three arrays. If the item has made a+ # request for a product, it is put in the billed_products array. If it is+ # supported but not yet used, it goes in available_products. Products granted+ # through Link's `additional_consented_products` appear in neither -- they are+ # reported in consented_products until the first call actually bills them.+ #+ # That third array matters because it is how we request `transactions` for+ # liability accounts: Provider::Plaid#get_primary_product returns "liabilities"+ # for CreditCard/Loan, so `transactions` can only arrive as an additionally+ # consented product. Omitting it made supports_product?("transactions") false+ # for every Plaid-linked credit card, so PlaidItem::AccountsSnapshot skipped+ # the transactions fetch entirely and those accounts imported balances and+ # liabilities but no transactions at all.def supported_products- available_products + billed_products+ consented = raw_payload.is_a?(Hash) ? Array(raw_payload["consented_products"]) : []++ (available_products + billed_products + consented).map(&:to_s).uniqendenddiff --git a/test/models/plaid_item_test.rb b/test/models/plaid_item_test.rbindex 969bd1c8aa..0f57b26039 100644--- a/test/models/plaid_item_test.rb+++ b/test/models/plaid_item_test.rb@@ -9,6 +9,34 @@ class PlaidItemTest < ActiveSupport::TestCaseProvider::Registry.stubs(:plaid_provider_for_region).returns(@plaid_provider)end+ test "supports products granted through additional_consented_products" do+ # Plaid reports products consented to at Link time -- but not yet billed --+ # in consented_products, which is neither available_products nor+ # billed_products. A credit-card item looks exactly like this: `liabilities`+ # is billed, while `transactions` is only consented.+ @plaid_item.update!(+ available_products: [ "balance" ],+ billed_products: [ "liabilities" ],+ raw_payload: { "consented_products" => [ "transactions", "liabilities", "investments" ] }+ )++ assert @plaid_item.supports_product?("transactions")+ assert @plaid_item.supports_product?("liabilities")+ assert @plaid_item.supports_product?("balance")+ assert_not @plaid_item.supports_product?("assets")+ end++ test "supported products tolerate a missing or non-hash raw payload" do+ @plaid_item.update!(+ available_products: [ "balance" ],+ billed_products: [ "transactions" ],+ raw_payload: nil+ )++ assert @plaid_item.supports_product?("transactions")+ assert_not @plaid_item.supports_product?("liabilities")+ end+test "removes plaid item when destroyed" do@plaid_provider.expects(:remove_item).with(@plaid_item.access_token).once