forked from maybe-finance/maybe
-
Notifications
You must be signed in to change notification settings - Fork 539
UI Suggestions for Account Types in Setup Modal + Stats-Based Inactive Handling #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
df9589c
- Add tests for `Simplefin::AccountTypeMapper` and `AccountSimplefinC…
d66e636
Remove unnecessary `.presence` check for `openai_uri_base` in hosting…
4ce3331
Refine zero balance detection logic in `SimplefinItem::Importer` and …
f18cb4c
Enhance account type and subtype inference logic with explicit invest…
597827c
Refine retirement subtype mapping in `AccountTypeMapper` tests with e…
dd48b97
Expand `AccountTypeMapper` investment subtype mapping to include `403…
f872503
Remove unused `retirement_hint?` method in `AccountTypeMapper` to sim…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Fallback-only inference for SimpleFIN-provided accounts. | ||
| # Conservative, used only to suggest a default type during setup/creation. | ||
| # Never overrides a user-selected type. | ||
| module Simplefin | ||
| class AccountTypeMapper | ||
| Inference = Struct.new(:accountable_type, :subtype, :confidence, keyword_init: true) | ||
|
|
||
| RETIREMENT_KEYWORDS = /\b(401k|401\(k\)|403b|403\(b\)|tsp|ira|roth|retirement)\b/i.freeze | ||
| BROKERAGE_KEYWORD = /\bbrokerage\b/i.freeze | ||
| CREDIT_NAME_KEYWORDS = /\b(credit|card)\b/i.freeze | ||
| CREDIT_BRAND_KEYWORDS = /\b(visa|mastercard|amex|american express|discover|apple card|freedom unlimited|quicksilver)\b/i.freeze | ||
| LOAN_KEYWORDS = /\b(loan|mortgage|heloc|line of credit|loc)\b/i.freeze | ||
|
|
||
luckyPipewrench marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Explicit investment subtype tokens mapped to known SUBTYPES keys | ||
| EXPLICIT_INVESTMENT_TOKENS = { | ||
| /\btraditional\s+ira\b/i => "ira", | ||
| /\broth\s+ira\b/i => "roth_ira", | ||
| /\broth\s+401\(k\)\b|\broth\s*401k\b/i => "roth_401k", | ||
| /\b401\(k\)\b|\b401k\b/i => "401k", | ||
| /\b529\s*plan\b|\b529\b/i => "529_plan", | ||
| /\bhsa\b|\bhealth\s+savings\s+account\b/i => "hsa", | ||
| /\bpension\b/i => "pension", | ||
| /\bmutual\s+fund\b/i => "mutual_fund", | ||
| /\b403b\b|\b403\(b\)\b/i => "403b", | ||
| /\btsp\b/i => "tsp" | ||
| }.freeze | ||
|
|
||
| # Public API | ||
| # @param name [String, nil] | ||
| # @param holdings [Array<Hash>, nil] | ||
| # @param extra [Hash, nil] - provider extras when present | ||
| # @param balance [Numeric, String, nil] | ||
| # @param available_balance [Numeric, String, nil] | ||
| # @return [Inference] e.g. Inference.new(accountable_type: "Investment", subtype: "retirement", confidence: :high) | ||
| def self.infer(name:, holdings: nil, extra: nil, balance: nil, available_balance: nil, institution: nil) | ||
| nm_raw = name.to_s | ||
| nm = nm_raw | ||
| # Normalized form to catch variants like RothIRA, Traditional-IRA, 401(k) | ||
| nm_norm = nm_raw.downcase.gsub(/[^a-z0-9]+/, " ").squeeze(" ").strip | ||
| inst = institution.to_s | ||
| holdings_present = holdings.is_a?(Array) && holdings.any? | ||
| bal = (balance.to_d rescue nil) | ||
| avail = (available_balance.to_d rescue nil) | ||
|
|
||
| # 0) Explicit retirement/plan tokens → Investment with explicit subtype (match against normalized name) | ||
| if (explicit_sub = EXPLICIT_INVESTMENT_TOKENS.find { |rx, _| nm_norm.match?(rx) }&.last) | ||
| if defined?(Investment::SUBTYPES) && Investment::SUBTYPES.key?(explicit_sub) | ||
| return Inference.new(accountable_type: "Investment", subtype: explicit_sub, confidence: :high) | ||
| else | ||
| return Inference.new(accountable_type: "Investment", subtype: nil, confidence: :high) | ||
| end | ||
| end | ||
|
|
||
| # 1) Holdings present => Investment (high confidence) | ||
| if holdings_present | ||
| # Do not guess generic retirement; explicit tokens handled above | ||
| return Inference.new(accountable_type: "Investment", subtype: nil, confidence: :high) | ||
| end | ||
|
|
||
| # 2) Name suggests LOAN (high confidence) | ||
| if LOAN_KEYWORDS.match?(nm) | ||
| return Inference.new(accountable_type: "Loan", confidence: :high) | ||
| end | ||
|
|
||
| # 3) Credit card signals | ||
| # - Name contains credit/card (medium to high) | ||
| # - Card brands (Visa/Mastercard/Amex/Discover/Apple Card) → high | ||
| # - Or negative balance with available-balance present (medium) | ||
| if CREDIT_NAME_KEYWORDS.match?(nm) || CREDIT_BRAND_KEYWORDS.match?(nm) || CREDIT_BRAND_KEYWORDS.match?(inst) | ||
| return Inference.new(accountable_type: "CreditCard", confidence: :high) | ||
| end | ||
| # Strong combined signal for credit card: negative balance and positive available-balance | ||
| if bal && bal < 0 && avail && avail > 0 | ||
| return Inference.new(accountable_type: "CreditCard", confidence: :high) | ||
| end | ||
|
|
||
| # 4) Retirement keywords without holdings still point to Investment (retirement) | ||
| if RETIREMENT_KEYWORDS.match?(nm) | ||
luckyPipewrench marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # If the name contains 'brokerage', avoid forcing retirement subtype | ||
| subtype = BROKERAGE_KEYWORD.match?(nm) ? nil : "retirement" | ||
| return Inference.new(accountable_type: "Investment", subtype: subtype, confidence: :high) | ||
| end | ||
|
|
||
| # 5) Default | ||
| Inference.new(accountable_type: "Depository", confidence: :low) | ||
| end | ||
|
|
||
| def self.retirement_hint?(name, extra) | ||
| return true if RETIREMENT_KEYWORDS.match?(name.to_s) | ||
|
|
||
| # sometimes providers include hints in extra payload | ||
| x = (extra || {}).with_indifferent_access | ||
| candidate = [ x[:account_subtype], x[:type], x[:subtype], x[:category] ].compact.join(" ") | ||
| RETIREMENT_KEYWORDS.match?(candidate) | ||
| end | ||
| private_class_method :retirement_hint? | ||
| end | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require "test_helper" | ||
|
|
||
| class AccountSimplefinCreationTest < ActiveSupport::TestCase | ||
| setup do | ||
| @family = families(:dylan_family) | ||
| @item = SimplefinItem.create!(family: @family, name: "SF Conn", access_url: "https://example.com/access") | ||
| end | ||
|
|
||
| test "requires explicit account_type at creation" do | ||
| sfa = SimplefinAccount.create!( | ||
| simplefin_item: @item, | ||
| name: "Brokerage", | ||
| account_id: "acct_1", | ||
| currency: "USD", | ||
| account_type: "investment", | ||
| current_balance: 1000 | ||
| ) | ||
|
|
||
| assert_raises(ArgumentError) do | ||
| Account.create_from_simplefin_account(sfa, nil) | ||
| end | ||
| end | ||
|
|
||
| test "uses provided account_type without inference" do | ||
| sfa = SimplefinAccount.create!( | ||
| simplefin_item: @item, | ||
| name: "My Loan", | ||
| account_id: "acct_2", | ||
| currency: "USD", | ||
| account_type: "loan", | ||
| current_balance: -5000 | ||
| ) | ||
|
|
||
| account = Account.create_from_simplefin_account(sfa, "Loan") | ||
|
|
||
| assert_equal "Loan", account.accountable_type | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require "test_helper" | ||
|
|
||
| class Simplefin::AccountTypeMapperTest < ActiveSupport::TestCase | ||
| test "holdings present implies Investment" do | ||
| inf = Simplefin::AccountTypeMapper.infer(name: "Vanguard Brokerage", holdings: [ { symbol: "VTI" } ]) | ||
| assert_equal "Investment", inf.accountable_type | ||
| assert_nil inf.subtype | ||
| end | ||
|
|
||
| test "explicit retirement tokens map to exact subtypes" do | ||
| cases = { | ||
| "My Roth IRA" => "roth_ira", | ||
| "401k Fidelity" => "401k" | ||
| } | ||
| cases.each do |name, expected_subtype| | ||
| inf = Simplefin::AccountTypeMapper.infer(name: name, holdings: [ { symbol: "VTI" } ]) | ||
| assert_equal "Investment", inf.accountable_type | ||
| assert_equal expected_subtype, inf.subtype | ||
| end | ||
| end | ||
|
|
||
| test "credit card names map to CreditCard" do | ||
| [ "Chase Credit Card", "VISA Card", "CREDIT" ] .each do |name| | ||
| inf = Simplefin::AccountTypeMapper.infer(name: name) | ||
| assert_equal "CreditCard", inf.accountable_type | ||
| end | ||
| end | ||
|
|
||
| test "loan-like names map to Loan" do | ||
| [ "Mortgage", "Student Loan", "HELOC", "Line of Credit" ].each do |name| | ||
| inf = Simplefin::AccountTypeMapper.infer(name: name) | ||
| assert_equal "Loan", inf.accountable_type | ||
| end | ||
| end | ||
|
|
||
| test "default is Depository" do | ||
| inf = Simplefin::AccountTypeMapper.infer(name: "Everyday Checking") | ||
| assert_equal "Depository", inf.accountable_type | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.