Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Show institution name and Apple logo for FinanceKit accounts (#5897)

Jason Cronjé06d8cb47a0d2
diff --git a/app/models/provider/financekit_adapter.rb b/app/models/provider/financekit_adapter.rb
index 331d4233de..ae6557d3e4 100644
--- a/app/models/provider/financekit_adapter.rb
+++ b/app/models/provider/financekit_adapter.rb
@@ -6,10 +6,27 @@ class Provider::FinancekitAdapter < Provider::Base
%w[Depository CreditCard]
end
+ # Wallet accounts report "Apple" (or an Apple product name) as the institution.
+ # Matched exactly, not by prefix: "Apple Bank" is a different institution.
+ # Anything not listed gets no domain rather than a guess.
+ APPLE_INSTITUTION_NAMES = [ "apple", "apple inc", "apple card", "apple cash", "apple wallet" ].freeze
+ APPLE_DOMAIN = "apple.com".freeze
+
def provider_name
"financekit"
end
+ # The mapping row keeps the name the device sent; the Account never receives it.
+ # Read at display time, so accounts created before this existed pick it up too.
+ def institution_name
+ latest_mapping&.institution_name.to_s.strip.presence
+ end
+
+ def institution_domain
+ normalized = institution_name.to_s.downcase.delete(".").squish
+ APPLE_DOMAIN if APPLE_INSTITUTION_NAMES.include?(normalized)
+ end
+
def item
provider_account.financekit_accounts.joins(:financekit_item)
.where(financekit_items: { status: %w[active repair_required] })
@@ -23,4 +40,10 @@ class Provider::FinancekitAdapter < Provider::Base
last_accepted_at: item.last_accepted_at, last_imported_at: item.last_imported_at,
last_downstream_at: item.last_downstream_at, repair_required: item.status == "repair_required")
end
+
+ private
+
+ def latest_mapping
+ provider_account.financekit_accounts.order(created_at: :desc, mapping_version: :desc).first
+ end
end
diff --git a/test/models/provider/financekit_adapter_test.rb b/test/models/provider/financekit_adapter_test.rb
new file mode 100644
index 0000000000..b9f0bf1491
--- /dev/null
+++ b/test/models/provider/financekit_adapter_test.rb
@@ -0,0 +1,75 @@
+require "test_helper"
+require_relative "../../support/financekit_test_helper"
+
+class Provider::FinancekitAdapterTest < ActiveSupport::TestCase
+ include FinancekitTestHelper
+
+ setup do
+ financekit_setup
+ @lineage = @source.financekit_account_lineage
+ @account = @lineage.account
+ end
+
+ teardown { travel_back }
+
+ def adapter
+ Provider::FinancekitAdapter.new(@lineage, account: @account)
+ end
+
+ def rename_institution(name)
+ @source.update_columns(institution_name: name)
+ end
+
+ test "institution_name comes from the stored mapping" do
+ assert_equal "Apple Wallet", adapter.institution_name
+ end
+
+ test "institution_name uses the latest mapping for the lineage" do
+ @source.update_columns(created_at: 2.days.ago)
+ second_item = Financekit::Enrollment.create!(@user, @enrollment.merge("enrollment_id" => SecureRandom.uuid)).item
+ second_item.financekit_accounts.create!(financekit_account_lineage: @lineage, source_id: SecureRandom.uuid,
+ mapping_version: 2, mapping_digest: "digest", name: "Test Wallet", institution_name: "Apple",
+ currency: "USD", accountable_type: "Depository", subtype: "checking", ledger_timezone: "America/Los_Angeles")
+
+ assert_equal "Apple", adapter.institution_name
+ end
+
+ test "institution_name is nil when blank" do
+ rename_institution(" ")
+
+ assert_nil adapter.institution_name
+ assert_nil adapter.institution_domain
+ end
+
+ test "institution_domain is apple.com for Apple and its Wallet products" do
+ [ "Apple", "apple", " Apple Inc. ", "Apple Card", "Apple Cash", "Apple Wallet" ].each do |name|
+ rename_institution(name)
+ assert_equal "apple.com", adapter.institution_domain, "expected apple.com for #{name.inspect}"
+ end
+ end
+
+ test "institution_domain is not guessed for other institutions" do
+ [ "Apple Bank", "Pineapple Credit Union", "Chase", "Goldman Sachs" ].each do |name|
+ rename_institution(name)
+ assert_nil adapter.institution_domain, "expected no domain for #{name.inspect}"
+ end
+ end
+
+ test "linked account shows the institution without any column on the account" do
+ @account.reload
+ assert @account.linked?
+ assert_nil @account.read_attribute(:institution_name)
+
+ assert_equal "Apple Wallet", @account.institution_name
+ assert_equal "apple.com", @account.institution_domain
+ end
+
+ test "a value stored on the account still wins over the provider" do
+ @account.update!(institution_name: "My Bank", institution_domain: "https://www.mybank.example/")
+ @account.reload
+ assert @account.linked?
+
+ assert_equal "My Bank", @account.institution_name
+ assert_equal "mybank.example", @account.institution_domain
+ end
+end