Files
sure/app/models/simplefin_account.rb
Sholom Ber 8497703518 Implement SimpleFin API client and data models
- Add SimplefinItem model with sync capabilities and encryption
- Add SimplefinAccount model for account data mapping
- Implement Provider::Simplefin API client with token exchange
- Add SimpleFin protocol support with proper error handling
- Include sync jobs, importers, and processors for data flow
- Add family SimpleFin connectivity mixin
2025-08-07 12:39:29 -04:00

67 lines
1.9 KiB
Ruby

class SimplefinAccount < ApplicationRecord
belongs_to :simplefin_item
has_one :account, dependent: :destroy
validates :name, :account_type, :currency, presence: true
validate :has_balance
def upsert_simplefin_snapshot!(account_snapshot)
# Map SimpleFin field names to our field names
assign_attributes(
current_balance: parse_balance(account_snapshot[:balance]),
available_balance: parse_balance(account_snapshot[:"available-balance"]),
currency: parse_currency(account_snapshot[:currency]),
account_type: account_snapshot[:type] || "unknown",
account_subtype: account_snapshot[:subtype],
name: account_snapshot[:name],
account_id: account_snapshot[:id],
raw_payload: account_snapshot
)
save!
end
def upsert_simplefin_transactions_snapshot!(transactions_snapshot)
assign_attributes(
raw_transactions_payload: transactions_snapshot
)
save!
end
private
def parse_balance(balance_value)
return nil if balance_value.nil?
case balance_value
when String
BigDecimal(balance_value)
when Numeric
BigDecimal(balance_value.to_s)
else
nil
end
rescue ArgumentError
nil
end
def parse_currency(currency_value)
return "USD" if currency_value.nil?
# SimpleFin currency can be a 3-letter code or a URL for custom currencies
if currency_value.start_with?("http")
# For custom currency URLs, we'll just use the last part as currency code
# This is a simplification - in production you might want to fetch the currency info
URI.parse(currency_value).path.split("/").last.upcase rescue "USD"
else
currency_value.upcase
end
end
def has_balance
return if current_balance.present? || available_balance.present?
errors.add(:base, "SimpleFin account must have either current or available balance")
end
end