Fix "invisible" merchants (#262)

- Fix display issues on the settings -> merchants page.
Currently it was showing only user created merchants, not provider created ones.
  - SimpleFin had its own MerchantDetector class that directly called ProviderMerchant.find_or_create_by!
  - This was inconsistent with Plaid and Lunchflow which use the centralized import_adapter.find_or_create_merchant method
  - Different unique keys: SimpleFin used source + name, others used source + provider_merchant_id
This commit is contained in:
soky srm
2025-10-30 21:30:20 +01:00
committed by GitHub
parent 5eadfaad98
commit 369ae8a6da
2 changed files with 21 additions and 2 deletions

View File

@@ -4,7 +4,8 @@ class FamilyMerchantsController < ApplicationController
def index
@breadcrumbs = [ [ "Home", root_path ], [ "Merchants", nil ] ]
@family_merchants = Current.family.merchants.alphabetically
# Show all merchants assigned to transactions (both FamilyMerchant and ProviderMerchant)
@family_merchants = Current.family.assigned_merchants.alphabetically
render layout: "settings"
end

View File

@@ -1,3 +1,5 @@
require "digest/md5"
class SimplefinEntry::Processor
# simplefin_transaction is the raw hash fetched from SimpleFin API and converted to JSONB
def initialize(simplefin_transaction, simplefin_account:)
@@ -100,6 +102,22 @@ class SimplefinEntry::Processor
def merchant
@merchant ||= SimplefinAccount::Transactions::MerchantDetector.new(data).detect_merchant
# Use SimpleFin's clean payee data for merchant detection
payee = data[:payee]&.strip
return nil unless payee.present?
@merchant ||= import_adapter.find_or_create_merchant(
provider_merchant_id: generate_merchant_id(payee),
name: payee,
source: "simplefin"
)
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error "SimplefinEntry::Processor - Failed to create merchant '#{payee}': #{e.message}"
nil
end
def generate_merchant_id(merchant_name)
# Generate a consistent ID for merchants without explicit IDs
"simplefin_#{Digest::MD5.hexdigest(merchant_name.downcase)}"
end
end