Files
sure/app/models/simplefin_account/transactions/processor.rb
Mike Lloyd fc0581fba3 Add per-account toggle to disable automatic transaction categorization (#2636)
* Add per-account toggle to disable automatic transaction categorization

Adds an `enable_category_matcher` boolean (default: true) to accounts so
users can opt out of Plaid's automatic category suggestions on a per-account
basis. When disabled, newly synced transactions arrive uncategorized so
rules or manual assignment take precedence.

- New migration adds `enable_category_matcher` column (default true, null: false)
- `PlaidEntry::Processor#matched_category` gates the CategoryMatcher call on the account flag
- Toggle rendered in the account edit modal for linked accounts (saves via main form submit)
- `AccountableResource#account_params` permits the new field
- `AccountsController#toggle_category_matcher` action added for potential API use
- i18n strings added for label and hint text
- Unit test covers the disabled-matcher path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Only show category matcher toggle for Plaid-linked accounts

Only PlaidEntry::Processor honors enable_category_matcher, but the toggle
rendered for every linked account, silently doing nothing for other
providers. Adds Account::Linkable#supports_category_matcher? (covering both
the legacy plaid_account_id link and AccountProvider rows) and gates the
form toggle on it. The SimpleFIN TODO now also points at this helper so the
toggle appears once SimpleFIN matching lands.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Add controller tests for category matcher toggle persistence and rendering

Covers the two paths flagged in review as untested: the flag persisting
through the shared AccountableResource#update action via account_params
(both disable and re-enable), and the edit form rendering the toggle only
for accounts where supports_category_matcher? is true.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Signed-off-by: Mike Lloyd <49411532+mike-lloyd03@users.noreply.github.com>
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-07-17 07:22:50 +02:00

78 lines
3.2 KiB
Ruby

class SimplefinAccount::Transactions::Processor
attr_reader :simplefin_account, :skipped_entries
def initialize(simplefin_account)
@simplefin_account = simplefin_account
@skipped_entries = []
end
def process
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
# Use a shared adapter to accumulate skipped entries across all transactions
adapter = Account::ProviderImportAdapter.new(acct) if acct
# 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.
transactions.each do |transaction_data|
SimplefinEntry::Processor.new(
transaction_data,
simplefin_account: simplefin_account,
import_adapter: adapter
).process
processed_count += 1
rescue => e
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
# Collect skipped entries from shared adapter
@skipped_entries = adapter&.skipped_entries || []
Rails.logger.info "SimplefinAccount::Transactions::Processor - Completed for simplefin_account #{simplefin_account.id}: #{processed_count} processed, #{error_count} errors, #{@skipped_entries.size} skipped (protected)"
end
private
# TODO: When SimpleFIN category matching is wired up (SimplefinAccount::Transactions::CategoryMatcher
# does not exist yet and this method is currently unused), apply the same
# `account&.enable_category_matcher?` guard used in PlaidEntry::Processor#matched_category,
# and include SimpleFIN in Account::Linkable#supports_category_matcher? so the toggle
# appears for SimpleFIN-linked accounts.
def category_matcher
@category_matcher ||= SimplefinAccount::Transactions::CategoryMatcher.new(family_categories)
end
def family_categories
@family_categories ||= begin
if account.family.categories.none?
account.family.categories.bootstrap!
end
account.family.categories
end
end
def account
simplefin_account.current_account
end
end