Fix missing SimpleFIN investment account transactions (#562)

* Add tests and update logic for processing SimpleFIN investment transactions

- Added `SimplefinAccount::Transactions::ProcessorInvestmentTest` to validate dividend transaction processing, transaction linking, and stale linkage repairs.
- Enhanced `SimplefinItem#process_accounts` with stale linkage repair logic and detailed logging for unlinked accounts with transactions.
- Updated `SimplefinAccount::Transactions::Processor` for improved logging and error handling during transaction processing.
- Adjusted `SimplefinItem::Importer` to log detailed account and transaction information and use extended sync windows for investment accounts.

* Refactor `SimplefinItem#process_accounts` to use direct queries for fresh data and streamline stale linkage repair logic; update tests for improved coverage and clarity.

* Improve stale linkage repair logic in `SimplefinItem#repair_stale_linkages`

- Updated to handle multiple linked accounts matching the same unlinked account by selecting the first match.
- Added detailed logging to warn about multiple matches for easier debugging.

* Include `:linked_account` in `SimplefinItem#process_accounts` queries for more comprehensive account data processing.

* Expand `merge_transactions` logic with composite key fallback for deduplication; document edge cases.

---------

Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
This commit is contained in:
LPW
2026-01-07 10:15:28 -05:00
committed by GitHub
parent 3b4ab735b0
commit 3f97f316e0
5 changed files with 528 additions and 87 deletions

View File

@@ -6,18 +6,40 @@ class SimplefinAccount::Transactions::Processor
end
def process
return unless simplefin_account.raw_transactions_payload.present?
transactions = simplefin_account.raw_transactions_payload.to_a
acct = simplefin_account.current_account
acct_info = acct ? "Account id=#{acct.id} name='#{acct.name}' type=#{acct.accountable_type}" : "NO LINKED ACCOUNT"
if transactions.empty?
Rails.logger.info "SimplefinAccount::Transactions::Processor - No transactions in raw_transactions_payload for simplefin_account #{simplefin_account.id} (#{simplefin_account.name}) - #{acct_info}"
return
end
Rails.logger.info "SimplefinAccount::Transactions::Processor - Processing #{transactions.count} transactions for simplefin_account #{simplefin_account.id} (#{simplefin_account.name}) - #{acct_info}"
# Log first few transaction IDs for debugging
sample_ids = transactions.first(3).map { |t| t.is_a?(Hash) ? (t[:id] || t["id"]) : nil }.compact
Rails.logger.info "SimplefinAccount::Transactions::Processor - Sample transaction IDs: #{sample_ids.inspect}"
processed_count = 0
error_count = 0
# Each entry is processed inside a transaction, but to avoid locking up the DB when
# there are hundreds or thousands of transactions, we process them individually.
simplefin_account.raw_transactions_payload.each do |transaction_data|
transactions.each do |transaction_data|
SimplefinEntry::Processor.new(
transaction_data,
simplefin_account: simplefin_account
).process
processed_count += 1
rescue => e
Rails.logger.error "Error processing SimpleFin transaction: #{e.message}"
error_count += 1
tx_id = transaction_data.is_a?(Hash) ? (transaction_data[:id] || transaction_data["id"]) : nil
Rails.logger.error "SimplefinAccount::Transactions::Processor - Error processing transaction #{tx_id}: #{e.class} - #{e.message}"
Rails.logger.error e.backtrace.first(5).join("\n") if e.backtrace
end
Rails.logger.info "SimplefinAccount::Transactions::Processor - Completed for simplefin_account #{simplefin_account.id}: #{processed_count} processed, #{error_count} errors"
end
private