Files
sure/app/models/simplefin_account/processor.rb
Sholom Ber 00cb130ef0 Remove unused SimpleFin account type mapping logic
- Remove map_simplefin_type_to_accountable_type method (no longer needed)
- Remove create_from_simplefin_account method (manual setup only)
- Simplify account type selection UI to not pre-select defaults
- Update processor to log error if account missing (safety check)
- All account creation now goes through manual user selection flow
2025-08-07 15:38:26 -04:00

86 lines
2.4 KiB
Ruby

class SimplefinAccount::Processor
attr_reader :simplefin_account
def initialize(simplefin_account)
@simplefin_account = simplefin_account
end
def process
ensure_account_exists
process_transactions
end
private
def ensure_account_exists
return if simplefin_account.account.present?
# This should not happen in normal flow since accounts are created manually
# during setup, but keeping as safety check
Rails.logger.error("SimpleFin account #{simplefin_account.id} has no associated Account - this should not happen after manual setup")
end
def process_transactions
return unless simplefin_account.raw_transactions_payload.present?
account = simplefin_account.account
transactions_data = simplefin_account.raw_transactions_payload
transactions_data.each do |transaction_data|
process_transaction(account, transaction_data)
end
end
def process_transaction(account, transaction_data)
# Convert SimpleFin transaction to internal Transaction format
amount_cents = parse_amount(transaction_data[:amount], account.currency)
posted_date = parse_date(transaction_data[:posted])
transaction_attributes = {
account: account,
name: transaction_data[:description] || "Unknown transaction",
amount: Money.new(amount_cents, account.currency),
date: posted_date,
currency: account.currency,
raw_data: transaction_data
}
# Use external ID to prevent duplicates
external_id = "simplefin_#{transaction_data[:id]}"
Transaction.find_or_create_by(external_id: external_id) do |transaction|
transaction.assign_attributes(transaction_attributes)
end
rescue => e
Rails.logger.error("Failed to process SimpleFin transaction #{transaction_data[:id]}: #{e.message}")
# Don't fail the entire sync for one bad transaction
end
def parse_amount(amount_value, currency)
case amount_value
when String
(BigDecimal(amount_value) * 100).to_i
when Numeric
(amount_value * 100).to_i
else
0
end
rescue ArgumentError
0
end
def parse_date(date_value)
case date_value
when String
Date.parse(date_value)
when Integer
# Unix timestamp
Time.at(date_value).to_date
else
Date.current
end
rescue ArgumentError, TypeError
Date.current
end
end