Files
sure/app/models/simplefin_account.rb
LPW c12c585a0e Harden SimpleFin sync: retries, safer imports, manual relinking, and data-quality reconciliation (#544)
* Add tests and enhance logic for SimpleFin account synchronization and reconciliation

- Added retry logic with exponential backoff for network errors in `Provider::Simplefin`.
- Introduced tests to verify retry functionality and error handling for rate-limit, server errors, and stale data.
- Updated `SimplefinItem` to detect stale sync status and reconciliation issues.
- Enhanced UI to display stale sync warnings and data integrity notices.
- Improved SimpleFin account matching during updates with multi-tier strategy (ID, fingerprint, fuzzy match).
- Added transaction reconciliation logic to detect data gaps, transaction count drops, and duplicate transaction IDs.

* Introduce `SimplefinConnectionUpdateJob` for asynchronous SimpleFin connection updates

- Moved SimpleFin connection update logic to `SimplefinConnectionUpdateJob` to improve response times by offloading network retries, data fetching, and reconciliation tasks.
- Enhanced SimpleFin account matching with a multi-tier strategy (ID, fingerprint, fuzzy name match).
- Added retry logic and bounded latency for token claim requests in `Provider::Simplefin`.
- Updated tests to cover the new job flow and ensure correct account reconciliation during updates.

* Remove unused SimpleFin account matching logic and improve error handling in `SimplefinConnectionUpdateJob`

- Deleted the multi-tier account matching logic from `SimplefinItemsController` as it is no longer used.
- Enhanced error handling in `SimplefinConnectionUpdateJob` to gracefully handle import failures, ensuring orphaned items can be manually resolved.
- Updated job flow to conditionally set item status based on the success of import operations.

* Fix SimpleFin sync: check both legacy FK and AccountProvider for linked accounts

* Add crypto, checking, savings, and cash account detection; refine subtype selection and linking

- Enhanced `Simplefin::AccountTypeMapper` to include detection for crypto, checking, savings, and standalone cash accounts.
- Improved subtype selection UI with validation and warning indicators for missing selections.
- Updated SimpleFin account linking to handle both legacy FK and `AccountProvider` associations consistently.
- Refined job flow and importer logic for better handling of linked accounts and subtype inference.

* Improve `SimplefinConnectionUpdateJob` and holdings processing logic

- Fixed race condition in `SimplefinConnectionUpdateJob` by moving `destroy_later` calls outside of transactions.
- Updated fuzzy name match logic to use Levenshtein distance for better accuracy.
- Enhanced synthetic ticker generation in holdings processor with hash suffix for uniqueness.

* Refine SimpleFin entry processing logic and ensure `extra` data persistence

- Simplified pending flag determination to rely solely on provider-supplied values.
- Fixed potential stale values in `extra` by ensuring deep merge overwrite with `entry.transaction.save!`.

* Replace hardcoded fallback transaction description with localized string

* Refine pending flag logic in SimpleFin processor tests

- Adjust test to prevent falsely inferring pending status from missing posted dates.
- Ensure provider explicitly sets pending flag for transactions.

* Add `has_many :holdings` association to `AccountProvider` with `dependent: :nullify`

---------

Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
2026-01-05 22:11:47 +01:00

122 lines
3.8 KiB
Ruby

class SimplefinAccount < ApplicationRecord
belongs_to :simplefin_item
# Legacy association via foreign key (will be removed after migration)
has_one :account, dependent: :nullify, foreign_key: :simplefin_account_id
# New association through account_providers
has_one :account_provider, as: :provider, dependent: :destroy
has_one :linked_account, through: :account_provider, source: :account
validates :name, :account_type, :currency, presence: true
validates :account_id, uniqueness: { scope: :simplefin_item_id, allow_nil: true }
validate :has_balance
# Helper to get account using new system first, falling back to legacy
def current_account
linked_account || account
end
# Ensure there is an AccountProvider link for this SimpleFin account and its current Account.
# Safe and idempotent; returns the AccountProvider or nil if no account is associated yet.
def ensure_account_provider!
acct = current_account
return nil unless acct
AccountProvider
.find_or_initialize_by(provider_type: "SimplefinAccount", provider_id: id)
.tap do |provider|
provider.account = acct
provider.save!
end
rescue => e
Rails.logger.warn("SimplefinAccount##{id}: failed to ensure AccountProvider link: #{e.class} - #{e.message}")
nil
end
def upsert_simplefin_snapshot!(account_snapshot)
# Convert to symbol keys or handle both string and symbol keys
snapshot = account_snapshot.with_indifferent_access
# Map SimpleFin field names to our field names
update!(
current_balance: parse_balance(snapshot[:balance]),
available_balance: parse_balance(snapshot[:"available-balance"]),
currency: parse_currency(snapshot[:currency]),
account_type: snapshot["type"] || "unknown",
account_subtype: snapshot["subtype"],
name: snapshot[:name],
account_id: snapshot[:id],
balance_date: parse_balance_date(snapshot[:"balance-date"]),
extra: snapshot[:extra],
org_data: snapshot[:org],
raw_payload: account_snapshot
)
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.blank?
# 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
begin
URI.parse(currency_value).path.split("/").last.upcase
rescue URI::InvalidURIError => e
Rails.logger.warn("Invalid currency URI for SimpleFin account: #{currency_value}, error: #{e.message}")
"USD"
end
else
currency_value.upcase
end
end
def parse_balance_date(balance_date_value)
return nil if balance_date_value.nil?
case balance_date_value
when String
Time.parse(balance_date_value)
when Numeric
Time.at(balance_date_value)
when Time, DateTime
balance_date_value
else
nil
end
rescue ArgumentError, TypeError
Rails.logger.warn("Invalid balance date for SimpleFin account: #{balance_date_value}")
nil
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