Files
sure/app/models/coinstats_item.rb
David Gil 28d99a2b0d Include newer providers in automatic family sync (#934)
* Include newer providers in automatic family sync

Coinbase, CoinStats, Mercury, and SnapTrade all implement Syncable
and have Syncer classes but were not listed in child_syncables,
meaning their data only refreshed on manual sync button clicks.

* refactor(syncer): Open/Closed principle for provider sync

- Adding new providers requires modifying child_syncables (violates O/C)
- plaid_items missing .active scope (bug: syncs deleted items)
- snaptrade_items can exist without user registration → fails on sync
- Scattered knowledge about 'ready to sync' logic

1. **Registry pattern**: SYNCABLE_ITEM_ASSOCIATIONS constant lists all
   provider associations that participate in family sync

2. **Encapsulated sync-readiness**: Each item model defines its own
   `syncable` scope that knows when it's ready for auto-sync:
   - Most providers: `syncable = active` (not scheduled for deletion)
   - SnapTrade: `syncable = active + user_registered` (has API creds)

3. **Single loop**: child_syncables iterates the registry, calling
   `.syncable` on each association

- Adding a provider = add to registry + define syncable scope
- Each model owns its 'ready to sync' business logic
- Fixes plaid_items bug (now uses .active via .syncable)
- Fixes snaptrade auto-sync failures (filters unregistered items)
- Easy to extend with new conditions per provider

- family/syncer.rb: Registry + dynamic collection
- *_item.rb (7 files): Add `scope :syncable, -> { active }`
- snaptrade_item.rb: Add syncable with user_registered filter

* Fix rubocop bracket spacing in SnaptradeItem syncable scope
2026-02-10 23:42:22 +01:00

152 lines
5.4 KiB
Ruby

# Represents a CoinStats API connection for a family.
# Stores credentials and manages associated crypto wallet accounts.
class CoinstatsItem < ApplicationRecord
include Syncable, Provided, Unlinking
enum :status, { good: "good", requires_update: "requires_update" }, default: :good
# Checks if ActiveRecord Encryption is properly configured.
# @return [Boolean] true if encryption keys are available
def self.encryption_ready?
creds_ready = Rails.application.credentials.active_record_encryption.present?
env_ready = ENV["ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY"].present? &&
ENV["ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY"].present? &&
ENV["ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT"].present?
creds_ready || env_ready
end
# Encrypt sensitive credentials if ActiveRecord encryption is configured
encrypts :api_key, deterministic: true if encryption_ready?
validates :name, presence: true
validates :api_key, presence: true
belongs_to :family
has_one_attached :logo, dependent: :purge_later
has_many :coinstats_accounts, dependent: :destroy
has_many :accounts, through: :coinstats_accounts
scope :active, -> { where(scheduled_for_deletion: false) }
scope :syncable, -> { active }
scope :ordered, -> { order(created_at: :desc) }
scope :needs_update, -> { where(status: :requires_update) }
# Schedules this item for async deletion.
def destroy_later
update!(scheduled_for_deletion: true)
DestroyJob.perform_later(self)
end
# Fetches latest wallet data from CoinStats API and updates local records.
# @raise [StandardError] if provider is not configured or import fails
def import_latest_coinstats_data
provider = coinstats_provider
unless provider
Rails.logger.error "CoinstatsItem #{id} - Cannot import: CoinStats provider is not configured"
raise StandardError.new("CoinStats provider is not configured")
end
CoinstatsItem::Importer.new(self, coinstats_provider: provider).import
rescue => e
Rails.logger.error "CoinstatsItem #{id} - Failed to import data: #{e.message}"
raise
end
# Processes holdings for all linked visible accounts.
# @return [Array<Hash>] Results with success status per account
def process_accounts
return [] if coinstats_accounts.empty?
results = []
coinstats_accounts.includes(:account).joins(:account).merge(Account.visible).each do |coinstats_account|
begin
result = CoinstatsAccount::Processor.new(coinstats_account).process
results << { coinstats_account_id: coinstats_account.id, success: true, result: result }
rescue => e
Rails.logger.error "CoinstatsItem #{id} - Failed to process account #{coinstats_account.id}: #{e.message}"
results << { coinstats_account_id: coinstats_account.id, success: false, error: e.message }
end
end
results
end
# Queues balance sync jobs for all visible accounts.
# @param parent_sync [Sync, nil] Parent sync for tracking
# @param window_start_date [Date, nil] Start of sync window
# @param window_end_date [Date, nil] End of sync window
# @return [Array<Hash>] Results with success status per account
def schedule_account_syncs(parent_sync: nil, window_start_date: nil, window_end_date: nil)
return [] if accounts.empty?
results = []
accounts.visible.each do |account|
begin
account.sync_later(
parent_sync: parent_sync,
window_start_date: window_start_date,
window_end_date: window_end_date
)
results << { account_id: account.id, success: true }
rescue => e
Rails.logger.error "CoinstatsItem #{id} - Failed to schedule sync for wallet #{account.id}: #{e.message}"
results << { account_id: account.id, success: false, error: e.message }
end
end
results
end
# Persists raw API response for debugging and reprocessing.
# @param accounts_snapshot [Hash] Raw API response data
def upsert_coinstats_snapshot!(accounts_snapshot)
assign_attributes(raw_payload: accounts_snapshot)
save!
end
# @return [Boolean] true if at least one account has been linked
def has_completed_initial_setup?
accounts.any?
end
# @return [String] Human-readable summary of sync status
def sync_status_summary
total_accounts = total_accounts_count
linked_count = linked_accounts_count
unlinked_count = unlinked_accounts_count
if total_accounts == 0
I18n.t("coinstats_items.coinstats_item.sync_status.no_accounts")
elsif unlinked_count == 0
I18n.t("coinstats_items.coinstats_item.sync_status.all_synced", count: linked_count)
else
I18n.t("coinstats_items.coinstats_item.sync_status.partial_sync", linked_count: linked_count, unlinked_count: unlinked_count)
end
end
# @return [Integer] Number of accounts with provider links
def linked_accounts_count
coinstats_accounts.joins(:account_provider).count
end
# @return [Integer] Number of accounts without provider links
def unlinked_accounts_count
coinstats_accounts.left_joins(:account_provider).where(account_providers: { id: nil }).count
end
# @return [Integer] Total number of coinstats accounts
def total_accounts_count
coinstats_accounts.count
end
# @return [String] Display name for the CoinStats connection
def institution_display_name
name.presence || "CoinStats"
end
# @return [Boolean] true if API key is set
def credentials_configured?
api_key.present?
end
end