The personal finance app for everyone (by everyone)
Ruby
Label Plaid dividends, interest and reinvestments by subtype (#5314)
jaysbeekay203ccaf056f6
diff --git a/app/models/plaid_account/investments/transactions_processor.rb b/app/models/plaid_account/investments/transactions_processor.rbindex d683d4ec72..02c4031418 100644--- a/app/models/plaid_account/investments/transactions_processor.rb+++ b/app/models/plaid_account/investments/transactions_processor.rb@@ -19,6 +19,24 @@ class PlaidAccount::Investments::TransactionsProcessor"split" => "Other"}.freeze+ # Plaid reports dividends, reinvestments and interest as subtypes of a broader+ # type (a cash dividend is type "cash"), so these are checked before the type.+ # Keyed by type because the same subtypes appear under other types where the+ # type's label is right: "fee" with subtype "interest" can be margin interest+ # charged, and "buy" with subtype "contribution" is still a buy. Those types+ # have no entry here and fall back to PLAID_TYPE_TO_LABEL.+ PLAID_SUBTYPE_TO_LABEL = {+ "cash" => {+ "dividend" => "Dividend",+ "qualified dividend" => "Dividend",+ "non-qualified dividend" => "Dividend",+ "interest" => "Interest"+ },+ "buy" => {+ "dividend reinvestment" => "Reinvestment"+ }+ }.freeze+def initialize(plaid_account, security_resolver:)@plaid_account = plaid_account@security_resolver = security_resolver@@ -94,7 +112,8 @@ class PlaidAccount::Investments::TransactionsProcessordef label_from_plaid_type(transaction)plaid_type = transaction["type"]&.downcase- PLAID_TYPE_TO_LABEL[plaid_type] || "Other"+ PLAID_SUBTYPE_TO_LABEL.dig(plaid_type, transaction["subtype"]&.downcase) ||+ PLAID_TYPE_TO_LABEL[plaid_type] || "Other"enddef transactionsdiff --git a/test/models/plaid_account/investments/transactions_processor_test.rb b/test/models/plaid_account/investments/transactions_processor_test.rbindex e54518fd1f..060fe470af 100644--- a/test/models/plaid_account/investments/transactions_processor_test.rb+++ b/test/models/plaid_account/investments/transactions_processor_test.rb@@ -245,4 +245,167 @@ class PlaidAccount::Investments::TransactionsProcessorTest < ActiveSupport::Testassert_equal "Bank Transfer", entry.nameassert_instance_of Transaction, entry.entryableend++ test "a Plaid cash dividend is labelled Dividend and classified as income" do+ entry = process_plaid_transaction(cash_row("div_1", "dividend", "Dividend AAPL"))++ assert_equal "Dividend", entry.transaction.investment_activity_label+ assert_equal :income, classify(entry)+ end++ test "a qualified dividend is labelled Dividend" do+ entry = process_plaid_transaction(cash_row("qdiv_1", "qualified dividend", "Qualified dividend AAPL"))++ assert_equal "Dividend", entry.transaction.investment_activity_label+ end++ test "a subtype in mixed case is matched case-insensitively" do+ entry = process_plaid_transaction(cash_row("div_case_1", "Qualified Dividend", "Qualified dividend AAPL"))++ assert_equal "Dividend", entry.transaction.investment_activity_label+ end++ test "a non-qualified dividend is labelled Dividend" do+ entry = process_plaid_transaction(cash_row("nqdiv_1", "non-qualified dividend", "Non-qualified dividend AAPL"))++ assert_equal "Dividend", entry.transaction.investment_activity_label+ end++ test "a Plaid interest payment is labelled Interest and classified as income" do+ entry = process_plaid_transaction(cash_row("int_1", "interest", "Interest earned"))++ assert_equal "Interest", entry.transaction.investment_activity_label+ assert_equal :income, classify(entry)+ end++ test "a Plaid dividend reinvestment trade is labelled Reinvestment" do+ entry = process_plaid_transaction(trade_row("drip_1", "buy", "dividend reinvestment"))++ assert_equal "Reinvestment", entry.trade.investment_activity_label+ assert_equal :internal, classify(entry)+ end++ test "re-syncing a DRIP stored as Buy relabels it Reinvestment" do+ entry = process_plaid_transaction(trade_row("drip_2", "buy", "dividend reinvestment"))+ entry.trade.update_columns(investment_activity_label: "Buy")++ assert_changes -> { entry.trade.reload.investment_activity_label }, from: "Buy", to: "Reinvestment" do+ PlaidAccount::Investments::TransactionsProcessor.new(@plaid_account, security_resolver: @security_resolver).process+ end+ end++ test "a buy with subtype contribution stays a Buy" do+ entry = process_plaid_transaction(trade_row("buy_contrib_1", "buy", "contribution"))++ assert_equal "Buy", entry.trade.investment_activity_label+ assert_equal :internal, classify(entry)+ end++ test "a cash contribution keeps its label and kind" do+ entry = process_plaid_transaction(cash_row("cash_contrib_1", "contribution", "401k Contribution"))++ assert_equal [ "Other", "standard" ], [ entry.transaction.investment_activity_label, entry.transaction.kind ]++ PlaidAccount::Investments::TransactionsProcessor.new(@plaid_account, security_resolver: @security_resolver).process++ entry.transaction.reload+ assert_equal [ "Other", "standard" ], [ entry.transaction.investment_activity_label, entry.transaction.kind ]+ end++ test "a split under transfer stays Transfer" do+ row = cash_row("split_1", "split", "Stock split")+ row["type"] = "transfer"+ entry = process_plaid_transaction(row)++ assert_equal "Transfer", entry.transaction.investment_activity_label+ assert_equal "funds_movement", entry.transaction.kind+ end++ test "an unmapped subtype falls back to the type" do+ fee = cash_row("fee_2", "miscellaneous fee", "Miscellaneous fee")+ fee["type"] = "fee"+ fee["amount"] = 10.25++ assert_equal "Fee", process_plaid_transaction(fee).transaction.investment_activity_label+ assert_equal "Sell", process_plaid_transaction(trade_row("sell_2", "sell", "sell")).trade.investment_activity_label+ end++ test "a fee row with subtype interest stays Fee" do+ entry = process_plaid_transaction(fee_row("fee_int_1", "interest", "Margin interest"))++ assert_equal "Fee", entry.transaction.investment_activity_label+ assert_equal :fee, classify(entry)+ end++ test "a fee row with subtype dividend stays Fee" do+ entry = process_plaid_transaction(fee_row("fee_div_1", "dividend", "Dividend charge"))++ assert_equal "Fee", entry.transaction.investment_activity_label+ assert_equal :fee, classify(entry)+ end++ test "a missing subtype falls back to the type" do+ entry = process_plaid_transaction(trade_row("buy_2", "buy", nil))++ assert_equal "Buy", entry.trade.investment_activity_label+ end++ test "a top-level dividend type without a subtype is still labelled Dividend" do+ entry = process_plaid_transaction(trade_row("top_div_1", "dividend", nil))++ assert_equal "Dividend", entry.trade.investment_activity_label+ end++ test "a top-level dividend reinvestment type without a subtype is still labelled Reinvestment" do+ entry = process_plaid_transaction(trade_row("top_drip_1", "dividend reinvestment", nil))++ assert_equal "Reinvestment", entry.trade.investment_activity_label+ end++ private++ def cash_row(id, subtype, name)+ {+ "investment_transaction_id" => id,+ "type" => "cash",+ "subtype" => subtype,+ "amount" => -12.5, # Negative, so money arriving in the account+ "iso_currency_code" => "USD",+ "date" => Date.current,+ "name" => name+ }+ end++ def fee_row(id, subtype, name)+ cash_row(id, subtype, name).merge("type" => "fee", "amount" => 10.25) # Positive, so money leaving the account+ end++ def trade_row(id, type, subtype)+ sell = type == "sell"+ {+ "investment_transaction_id" => id,+ "security_id" => "123",+ "type" => type,+ "subtype" => subtype,+ "quantity" => sell ? -0.1 : 0.1,+ "price" => 125,+ "amount" => sell ? -12.5 : 12.5,+ "iso_currency_code" => "USD",+ "date" => Date.current,+ "name" => "#{type} AAPL"+ }+ end++ def process_plaid_transaction(row)+ @plaid_account.update!(raw_holdings_payload: { transactions: [ row ] })+ @security_resolver.stubs(:resolve).returns(OpenStruct.new(security: securities(:aapl)))++ PlaidAccount::Investments::TransactionsProcessor.new(@plaid_account, security_resolver: @security_resolver).process++ Entry.find_by!(external_id: row["investment_transaction_id"], source: "plaid")+ end++ def classify(entry)+ Portfolio::FlowClassifier.new(scope_account_ids: [ entry.account_id ]).classify(entry)+ endend