fix(sync): record synced dividends and interest as trades

Draft
JS
20d ago
+1093-105

Problem

#1311 made manually entered dividends and interest in investment accounts into trades: a Trade with qty: 0 and price: 0, with the cash value on the entry. Provider syncs were not updated and kept importing the same events as Transactions labelled Dividend or Interest. So a dividend looked different depending on whether it was entered by hand or synced.

Synced income was also missing from investment figures. InvestmentStatement::Totals aggregates over trades, and its dividend and interest totals were hardcoded to 0 because synced income arrived as transactions.

Changes

  • Providers. SnapTrade, Questrade, IBKR, Trading212, Trade Republic, Indexa Capital and Plaid now import dividends and interest through import_trade with qty: 0, price: 0 and an activity label of Dividend or Interest. The trade uses the paying security when the provider names one, and falls back to the account's cash security (Security.cash_for) when it doesn't, as manual interest entry does.
  • Trade Republic accounts can be linked to a Depository account. Interest paid into one stays a Transaction, since the account can't hold trades. This matches the manual rule that income is a trade in an investment account.
  • Plaid dividends and interest used to go through the regular trade path, which calculates the amount as quantity Ɨ price. Plaid reports both as 0 for income, so every synced dividend was stored with an amount of 0. They now use the provider's amount, and existing zero-amount rows are corrected on the next sync.
  • InvestmentStatement::Totals now sums dividends and interest from zero-quantity trades by label, and leaves them out of trades_count.
  • Trade::INCOME_LABELS replaces the %w[Dividend Interest] lists that were hardcoded in the trades controller, the trade header and the activity quick-edit badge.
  • Provider guide. Added a section on importing dividends and interest.

Existing dividend and interest transactions

This change only affects dividends and interest synced from now on. Existing Transaction rows are left as they are.

The two collide when a provider re-delivers an old dividend under the same external_id, now as a trade. import_trade raises on an entry type mismatch. It now makes one exception: when an income trade arrives for an entry that already exists as a Transaction, it keeps the existing row, records a skip and writes a log entry. Every other type mismatch still raises.

Why the old rows aren't converted. A Transaction can have a merchant, a transfer, tags and attachments, and a Trade has none of those, so converting would drop them. The sync protection flags can't be relied on to show whether a user has touched a row either. Rules and auto-categorization don't set them, and attaching a file marks nothing.

Why skip instead of raising. Most providers catch errors per entry, so for them a raise only drops that one entry. Plaid's investment processor doesn't: a raise there would stop the rest of the investment sync, holdings included, on every sync.

Logging. Each skip writes a DebugLogEntry (category sync, level warn). The message says the dividend or interest couldn't be synced as a trade because it was already synced as a transaction, and that deleting the transaction lets the next sync import it as a trade. Logging is limited to once per account per provider per day, to prevent log spam from large syncs. Old rows outside a provider's sync window never collide, so they're never logged and stay as they are.

The skip is also recorded in skipped_entries.

Testing

Tested by hand with SnapTrade. New dividends are synced as Trades, and old Transaction dividends are logged and skipped.

Open questions

  1. Should old dividend and interest transactions be skipped, or updated? This PR leaves them as they are. One alternative is to convert only rows that carry nothing but provider data: no tags, attachments, merchant, transfer, locked attributes or protection flags. Another is to keep updating them as transactions. Skipping is the simplest option and can't lose data, but it leaves those rows as transactions permanently.
  2. Should synced dividends require a security? Manual entry requires one for dividends (interest is optional). Synced dividends currently fall back to the cash security when the security can't be resolved. The alternatives are to skip those dividends, as IBKR already does when the security ID is missing, or to import them as transactions. Skipping matches manual entry but removes that cash from the user's records.

šŸ¤– Generated with Claude Code