Files
sure/app/models/simplefin_item.rb
luckyPipewrench fea228d03e Simplefin sync improvements (#240)
* Fix syncing issues with new connections and accounts..

- Keep SimpleFin institution metadata strictly per account (`simplefin_accounts.org_data`).
- Relax `simplefin_items` institution constraints to allow creating items before org data exists.
- Remove code that copied the first account’s `org` onto `simplefin_items`.

* Improve Simplefin Sync
•
SimpleFin: family “Sync” includes SimpleFin items; importer does unbounded discovery (with pending=1 fallback) before windowed fetch, for both regular and first syncs.
•
Stop populating item‑level institution fields; keep institution metadata per account.
•
Relax NOT NULL on item institution fields.
•
Post‑sync dashboard broadcasts are now guarded (UI cannot fail the job).
•
Show a friendly “daily refresh limit” banner on the SimpleFin card when the latest sync is rate‑limited.
•
Add bin/rails sure:simplefin:debug[ITEM_ID] to print latest sync, snapshot account count, simplefin_accounts count, and unlinked list.

* Fixed double‑quoted strings, spacing around array brackets and commas

* chore: ignore local .junie files

* - Broadcast error logs now include full backtraces
- SimpleFin discovery logic deduplicated fixed
- app/models/simplefin_item/importer.rb
--Added a concise docstring for perform_account_discovery describing purpose, steps, and side‑effects.
--Added a docstring for fetch_accounts_data describing params and return value.
2025-10-26 15:50:45 +01:00

177 lines
5.0 KiB
Ruby

class SimplefinItem < ApplicationRecord
include Syncable, Provided
enum :status, { good: "good", requires_update: "requires_update" }, default: :good
# Virtual attribute for the setup token form field
attr_accessor :setup_token
if Rails.application.credentials.active_record_encryption.present?
encrypts :access_url, deterministic: true
end
validates :name, :access_url, presence: true
before_destroy :remove_simplefin_item
belongs_to :family
has_one_attached :logo
has_many :simplefin_accounts, dependent: :destroy
has_many :accounts, through: :simplefin_accounts
scope :active, -> { where(scheduled_for_deletion: false) }
scope :ordered, -> { order(created_at: :desc) }
scope :needs_update, -> { where(status: :requires_update) }
def destroy_later
update!(scheduled_for_deletion: true)
DestroyJob.perform_later(self)
end
def import_latest_simplefin_data
SimplefinItem::Importer.new(self, simplefin_provider: simplefin_provider).import
end
def process_accounts
simplefin_accounts.joins(:account).each do |simplefin_account|
SimplefinAccount::Processor.new(simplefin_account).process
end
end
def schedule_account_syncs(parent_sync: nil, window_start_date: nil, window_end_date: nil)
accounts.each do |account|
account.sync_later(
parent_sync: parent_sync,
window_start_date: window_start_date,
window_end_date: window_end_date
)
end
end
def upsert_simplefin_snapshot!(accounts_snapshot)
assign_attributes(
raw_payload: accounts_snapshot,
)
# Do not populate item-level institution fields from account data.
# Institution metadata belongs to each simplefin_account (in org_data).
save!
end
def upsert_institution_data!(org_data)
org = org_data.to_h.with_indifferent_access
url = org[:url] || org[:"sfin-url"]
domain = org[:domain]
# Derive domain from URL if missing
if domain.blank? && url.present?
begin
domain = URI.parse(url).host&.gsub(/^www\./, "")
rescue URI::InvalidURIError
Rails.logger.warn("Invalid SimpleFin institution URL: #{url.inspect}")
end
end
assign_attributes(
institution_id: org[:id],
institution_name: org[:name],
institution_domain: domain,
institution_url: url,
raw_institution_payload: org_data
)
end
def has_completed_initial_setup?
# Setup is complete if we have any linked accounts
accounts.any?
end
def sync_status_summary
latest = latest_sync
return nil unless latest
# If sync has statistics, use them
if latest.sync_stats.present?
stats = latest.sync_stats
total = stats["total_accounts"] || 0
linked = stats["linked_accounts"] || 0
unlinked = stats["unlinked_accounts"] || 0
if total == 0
"No accounts found"
elsif unlinked == 0
"#{linked} #{'account'.pluralize(linked)} synced"
else
"#{linked} synced, #{unlinked} need setup"
end
else
# Fallback to current account counts
total_accounts = simplefin_accounts.count
linked_count = accounts.count
unlinked_count = total_accounts - linked_count
if total_accounts == 0
"No accounts found"
elsif unlinked_count == 0
"#{linked_count} #{'account'.pluralize(linked_count)} synced"
else
"#{linked_count} synced, #{unlinked_count} need setup"
end
end
end
def institution_display_name
# Try to get institution name from stored metadata
institution_name.presence || institution_domain.presence || name
end
def connected_institutions
# Get unique institutions from all accounts
simplefin_accounts.includes(:account)
.where.not(org_data: nil)
.map { |acc| acc.org_data }
.uniq { |org| org["domain"] || org["name"] }
end
def institution_summary
institutions = connected_institutions
case institutions.count
when 0
"No institutions connected"
when 1
institutions.first["name"] || institutions.first["domain"] || "1 institution"
else
"#{institutions.count} institutions"
end
end
# Detect a recent rate-limited sync and return a friendly message, else nil
def rate_limited_message
latest = latest_sync
return nil unless latest
# Some Sync records may not have a status_text column; guard with respond_to?
parts = []
parts << latest.error if latest.respond_to?(:error)
parts << latest.status_text if latest.respond_to?(:status_text)
msg = parts.compact.join("")
return nil if msg.blank?
down = msg.downcase
if down.include?("make fewer requests") || down.include?("only refreshed once every 24 hours") || down.include?("rate limit")
"You've hit SimpleFin's daily refresh limit. Please try again after the bridge refreshes (up to 24 hours)."
else
nil
end
end
private
def remove_simplefin_item
# SimpleFin doesn't require server-side cleanup like Plaid
# The access URL just becomes inactive
end
end