The personal finance app for everyone (by everyone)
Ruby
fix(providers): honor the category matcher toggle for Up and monobank (#3490)
Yaroslav Shparukbf8c72548104
diff --git a/app/models/account/linkable.rb b/app/models/account/linkable.rbindex a3d3b5c988..9920fb5086 100644--- a/app/models/account/linkable.rb+++ b/app/models/account/linkable.rb@@ -75,10 +75,15 @@ module Account::Linkableend# Whether this account's provider applies the category matcher to imported- # transactions. Only Plaid honors `enable_category_matcher` today; extend this- # when other providers (e.g. SimpleFIN) wire up category matching.+ # transactions, and therefore honors `enable_category_matcher`. Add a provider+ # here only once its entry processor checks the toggle; listing one that+ # ignores it shows the user a switch that does nothing.+ CATEGORY_MATCHER_PROVIDER_TYPES = %w[PlaidAccount UpAccount MonobankAccount].freeze+def supports_category_matcher?- plaid_account.present? || linked_to?("PlaidAccount")+ return true if plaid_account.present?++ account_providers.exists?(provider_type: CATEGORY_MATCHER_PROVIDER_TYPES)end# Check if holdings can be deleteddiff --git a/app/models/monobank_entry/processor.rb b/app/models/monobank_entry/processor.rbindex 9fce7916aa..ee37f649be 100644--- a/app/models/monobank_entry/processor.rb+++ b/app/models/monobank_entry/processor.rb@@ -110,11 +110,13 @@ class MonobankEntry::Processorend# The id of the Sure category the transaction's MCC maps to, or nil when no matcher- # was injected, the MCC is missing, or it has no confident equivalent among the- # family's categories. The import adapter applies this via enrich_attribute, so it- # never overwrites a category the user has set or locked.+ # was injected, the account has category matching switched off, the MCC is missing,+ # or it has no confident equivalent among the family's categories. The import+ # adapter applies this via enrich_attribute, so it never overwrites a category the+ # user has set or locked.def matched_category_idreturn nil unless @category_matcher+ return nil unless account&.enable_category_matcher?@category_matcher.match(data[:mcc])&.idenddiff --git a/app/models/up_entry/processor.rb b/app/models/up_entry/processor.rbindex b08909b700..db6bcf5d85 100644--- a/app/models/up_entry/processor.rb+++ b/app/models/up_entry/processor.rb@@ -106,12 +106,13 @@ class UpEntry::Processorend# The id of the Sure category that Up's category slug maps to, or nil when no- # matcher was injected, the transaction has no Up category (transfers/income), or- # the slug has no confident equivalent among the family's categories. The import- # adapter applies this via enrich_attribute, so it never overwrites a category the- # user has set or locked.+ # matcher was injected, the account has category matching switched off, the+ # transaction has no Up category (transfers/income), or the slug has no confident+ # equivalent among the family's categories. The import adapter applies this via+ # enrich_attribute, so it never overwrites a category the user has set or locked.def matched_category_idreturn nil unless @category_matcher+ return nil unless account&.enable_category_matcher?@category_matcher.match(data[:category_id])&.idenddiff --git a/test/models/account/linkable_test.rb b/test/models/account/linkable_test.rbindex ec6ecfb828..7900bdf7b6 100644--- a/test/models/account/linkable_test.rb+++ b/test/models/account/linkable_test.rb@@ -56,7 +56,21 @@ class Account::LinkableTest < ActiveSupport::TestCaseassert @account.supports_category_matcher?end- test "supports_category_matcher? returns false for unlinked and non-Plaid accounts" do+ test "supports_category_matcher? returns true for Up-linked accounts" do+ up_item = UpItem.create!(family: @family, name: "Test Up", access_token: "up-access-token")+ up_account = UpAccount.create!(up_item: up_item, name: "Up Spending", account_id: "up_acc_1", currency: "AUD")+ AccountProvider.create!(account: @account, provider: up_account)++ assert @account.supports_category_matcher?+ end++ test "supports_category_matcher? returns true for Monobank-linked accounts" do+ AccountProvider.create!(account: @account, provider: monobank_accounts(:black_card))++ assert @account.supports_category_matcher?+ end++ test "supports_category_matcher? returns false for unlinked accounts and providers without a matcher" dorefute @account.supports_category_matcher?simplefin_item = SimplefinItem.create!(diff --git a/test/models/monobank_entry/processor_test.rb b/test/models/monobank_entry/processor_test.rbindex 8c5ca0438c..0b1d246bc0 100644--- a/test/models/monobank_entry/processor_test.rb+++ b/test/models/monobank_entry/processor_test.rb@@ -183,12 +183,58 @@ class MonobankEntry::ProcessorTest < ActiveSupport::TestCaseassert first.start_with?("monobank_pending_")end+ test "applies the matched Sure category when a category_matcher is provided" do+ @family.categories.bootstrap!++ entry = process(+ **grocery_transaction,+ category_matcher: category_matcher+ )++ assert_equal "Groceries", entry.transaction.category&.name+ # The raw MCC is still preserved in extra for reference.+ assert_equal 5411, entry.transaction.extra.dig("monobank", "mcc")+ end++ test "skips category matching when the account has the matcher switched off" do+ @family.categories.bootstrap!+ @account.update!(enable_category_matcher: false)++ entry = process(+ **grocery_transaction,+ category_matcher: category_matcher+ )++ assert_nil entry.transaction.category_id+ # Still imported, still carries the MCC — only the auto-category is withheld.+ assert_equal 5411, entry.transaction.extra.dig("monobank", "mcc")+ end+private- def process(**transaction_data)+ def process(category_matcher: nil, **transaction_data)MonobankEntry::Processor.new(transaction_data.deep_stringify_keys,- monobank_account: @monobank_account+ monobank_account: @monobank_account,+ category_matcher: category_matcher).processend++ def grocery_transaction+ {+ id: "tx_cat_1",+ time: MIDDAY_UNIX,+ description: "Silpo",+ mcc: 5411,+ originalMcc: 5411,+ hold: false,+ amount: -50_000,+ operationAmount: -50_000,+ currencyCode: 980+ }+ end++ def category_matcher+ MonobankAccount::Transactions::CategoryMatcher.new(@family.categories.to_a, locale: @family.locale)+ endenddiff --git a/test/models/up_entry/processor_test.rb b/test/models/up_entry/processor_test.rbindex c58c0ac75f..f9eeb1b769 100644--- a/test/models/up_entry/processor_test.rb+++ b/test/models/up_entry/processor_test.rb@@ -197,4 +197,29 @@ class UpEntry::ProcessorTest < ActiveSupport::TestCaseassert_nil entry.transaction.category_idend++ test "skips category matching when the account has the matcher switched off" do+ @family.categories.bootstrap!+ @account.update!(enable_category_matcher: false)+ matcher = UpAccount::Transactions::CategoryMatcher.new(@family.categories.to_a)++ entry = UpEntry::Processor.new(+ {+ id: "tx_cat_3",+ account_id: "acc_123",+ status: "SETTLED",+ description: "Woolworths",+ amount: { currencyCode: "AUD", value: "-40.00", valueInBaseUnits: -4000 },+ settledAt: "2026-01-15T00:00:00+11:00",+ createdAt: "2026-01-15T00:00:00+11:00",+ category_id: "groceries"+ },+ up_account: @up_account,+ category_matcher: matcher+ ).process++ assert_nil entry.transaction.category_id+ # Still imported, still carries the Up slug — only the auto-category is withheld.+ assert_equal "groceries", entry.transaction.extra.dig("up", "category_id")+ endend