UI Suggestions for Account Types in Setup Modal + Stats-Based Inactive Handling (#368)

* - Add tests for `Simplefin::AccountTypeMapper` and `AccountSimplefinCreation`
- Implement `Simplefin::AccountTypeMapper` for account type inference with fallback-only logic
- Enhance inactive state handling for `SimplefinItem::Importer`
- Improve subtype selection handling in views with confidence-based inference

* Remove unnecessary `.presence` check for `openai_uri_base` in hostings settings

* Refine zero balance detection logic in `SimplefinItem::Importer` and add regression test for missing balances scenario

* Enhance account type and subtype inference logic with explicit investment subtype mapping, improved regex handling, and institution-based credit card detection

* Refine retirement subtype mapping in `AccountTypeMapper` tests with explicit case-based assertions

* Expand `AccountTypeMapper` investment subtype mapping to include `403b` and `tsp` with updated regex definitions

* Remove unused `retirement_hint?` method in `AccountTypeMapper` to simplify codebase

---------

Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
This commit is contained in:
LPW
2025-11-24 08:07:14 -05:00
committed by GitHub
parent eb4b978a97
commit cf5e7de65f
10 changed files with 336 additions and 4 deletions

View File

@@ -109,6 +109,51 @@ class SimplefinItem::Importer
@stats ||= {}
end
# Heuristics to set a SimpleFIN account inactive when upstream indicates closure/hidden
# or when we repeatedly observe zero balances and zero holdings. This should not block
# import and only sets a flag and suggestion via sync stats.
def update_inactive_state(simplefin_account, account_data)
payload = (account_data || {}).with_indifferent_access
raw = (simplefin_account.raw_payload || {}).with_indifferent_access
# Flags from payloads
closed = [ payload[:closed], payload[:hidden], payload.dig(:extra, :closed), raw[:closed], raw[:hidden] ].compact.any? { |v| v == true || v.to_s == "true" }
balance = payload[:balance]
avail = payload[:"available-balance"]
holdings = payload[:holdings]
amounts = [ balance, avail ].compact
zeroish_balance = amounts.any? && amounts.all? { |x| x.to_d.zero? rescue false }
no_holdings = !(holdings.is_a?(Array) && holdings.any?)
stats["zero_runs"] ||= {}
stats["inactive"] ||= {}
key = simplefin_account.account_id.presence || simplefin_account.id
key = key.to_s
# Ensure key exists and defaults to false (so tests don't read nil)
stats["inactive"][key] = false unless stats["inactive"].key?(key)
if closed
stats["inactive"][key] = true
stats["hints"] = Array(stats["hints"]) + [ "Some accounts appear closed/hidden upstream. You can relink or hide them." ]
return
end
if zeroish_balance && no_holdings
stats["zero_runs"][key] = stats["zero_runs"][key].to_i + 1
# Cap to avoid unbounded growth
stats["zero_runs"][key] = [ stats["zero_runs"][key], 10 ].min
else
stats["zero_runs"][key] = 0
stats["inactive"][key] = false
end
if stats["zero_runs"][key].to_i >= 3
stats["inactive"][key] = true
stats["hints"] = Array(stats["hints"]) + [ "One or more accounts show no balance/holdings for multiple syncs — consider relinking or marking inactive." ]
end
end
# Track seen error fingerprints during a single importer run to avoid double counting
def seen_errors
@seen_errors ||= Set.new
@@ -457,6 +502,13 @@ class SimplefinItem::Importer
end
simplefin_account.assign_attributes(attrs)
# Inactive detection/toggling (non-blocking)
begin
update_inactive_state(simplefin_account, account_data)
rescue => e
Rails.logger.warn("SimpleFin: inactive-state evaluation failed for sfa=#{simplefin_account.id || account_id}: #{e.class} - #{e.message}")
end
# Final validation before save to prevent duplicates
if simplefin_account.account_id.blank?
simplefin_account.account_id = account_id
@@ -474,6 +526,10 @@ class SimplefinItem::Importer
register_error(message: msg, category: "other", account_id: account_id, name: account_data[:name])
persist_stats!
nil
ensure
# Ensure stats like zero_runs/inactive are persisted even when no errors occur,
# particularly helpful for focused unit tests that call import_account directly.
persist_stats!
end
end