Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Read SimpleFIN v2 institution details from connections (#4838)

Krish Wanchoo1636931c8571
diff --git a/app/models/simplefin_item/importer.rb b/app/models/simplefin_item/importer.rb
index 710e1b8d57..2869c5fefc 100644
--- a/app/models/simplefin_item/importer.rb
+++ b/app/models/simplefin_item/importer.rb
@@ -811,9 +811,32 @@ class SimplefinItem::Importer
end
end
+ attach_connection_org_data!(accounts_data)
accounts_data
end
+ # Protocol v2 accounts carry a conn_id instead of an org object, and the
+ # institution details sit in the top-level connections list. Give each
+ # such account the org hash that the rest of the import reads.
+ def attach_connection_org_data!(accounts_data)
+ connections = Array(accounts_data[:connections]).index_by { |connection| connection[:conn_id].to_s }
+ return if connections.empty?
+
+ Array(accounts_data[:accounts]).each do |account_data|
+ next if account_data[:org].present?
+
+ connection = connections[account_data[:conn_id].to_s]
+ next if connection.nil?
+
+ account_data[:org] = {
+ id: connection[:org_id],
+ name: connection[:org_name].presence || connection[:name],
+ url: connection[:org_url],
+ "sfin-url": connection[:sfin_url]
+ }.compact
+ end
+ end
+
def determine_sync_start_date
# For the first sync, get only a limited amount of data to avoid SimpleFin API limits
# SimpleFin requires a start_date parameter - without it, only returns recent transactions
diff --git a/test/models/simplefin_item/importer_test.rb b/test/models/simplefin_item/importer_test.rb
index 2bc3110e3e..de6b2d3a15 100644
--- a/test/models/simplefin_item/importer_test.rb
+++ b/test/models/simplefin_item/importer_test.rb
@@ -155,6 +155,42 @@ class SimplefinItem::ImporterTest < ActiveSupport::TestCase
assert @item.reload.requires_update?
end
+ test "a protocol v2 account takes its institution from the matching connection" do
+ provider = mock
+ provider.expects(:get_accounts).returns(
+ connections: [
+ { conn_id: "CON-other", name: "Other Bank", org_name: "Other Bank" },
+ { conn_id: "CON-schwab", name: "Charles Schwab US", org_id: "mx.69378", org_name: "Charles Schwab US",
+ org_url: "https://client.schwab.com/", sfin_url: "https://beta-bridge.simplefin.org/simplefin" }
+ ],
+ accounts: [ { id: "sf_brokerage_v2", name: "Brokerage ...123", conn_id: "CON-schwab", currency: "USD", balance: "1000" } ]
+ )
+
+ SimplefinItem::Importer.new(@item, simplefin_provider: provider).import_balances_only
+
+ simplefin_account = @item.simplefin_accounts.find_by!(account_id: "sf_brokerage_v2")
+ assert_equal(
+ { "id" => "mx.69378", "name" => "Charles Schwab US", "url" => "https://client.schwab.com/",
+ "sfin-url" => "https://beta-bridge.simplefin.org/simplefin" },
+ simplefin_account.org_data
+ )
+ assert SimplefinAccount::Investments::HoldingsProcessor.new(simplefin_account).send(:institution_reports_total_basis?)
+ end
+
+ test "an account that already has an org keeps it" do
+ provider = mock
+ provider.expects(:get_accounts).returns(
+ connections: [ { conn_id: "CON-1", name: "Connection Name", org_name: "Connection Org" } ],
+ accounts: [ { id: "sf_v1_org", name: "Checking", conn_id: "CON-1", org: { name: "Account Org", domain: "bank.example" },
+ currency: "USD", balance: "10" } ]
+ )
+
+ SimplefinItem::Importer.new(@item, simplefin_provider: provider).import_balances_only
+
+ assert_equal({ "name" => "Account Org", "domain" => "bank.example" },
+ @item.simplefin_accounts.find_by!(account_id: "sf_v1_org").org_data)
+ end
+
test "a regular sync after a long gap splits the range into requests the Bridge accepts" do
travel_to Time.zone.parse("2026-11-07 12:00") do
@item.stubs(:last_synced_at).returns(100.days.ago)