The personal finance app for everyone (by everyone)
Ruby
[codex] Complete Sophtron account mapping (#1698)
Juan José Mata81cdccb768db
diff --git a/app/controllers/sophtron_items_controller.rb b/app/controllers/sophtron_items_controller.rbindex 73fc9c7308..81a86cd60d 100644--- a/app/controllers/sophtron_items_controller.rb+++ b/app/controllers/sophtron_items_controller.rb@@ -1,5 +1,22 @@class SophtronItemsController < ApplicationController- before_action :set_sophtron_item, only: [ :show, :edit, :update, :destroy, :sync, :setup_accounts, :complete_account_setup ]+ CONNECTION_STATUS_MAX_POLLS = 6+ LOGIN_PROGRESS_CONNECTION_STATUS_MAX_POLLS = 15+ POST_MFA_CONNECTION_STATUS_MAX_POLLS = 15+ CONNECTION_STATUS_POLL_INTERVAL_MS = 4_000+ MAX_SECURITY_ANSWERS = 10+ MAX_SECURITY_ANSWER_LENGTH = 256++ before_action :set_sophtron_item, only: [+ :show, :edit, :update, :destroy, :connect_institution, :sync,+ :connection_status, :submit_mfa,+ :setup_accounts, :complete_account_setup+ ]+ before_action :require_admin!, only: [+ :new, :create, :preload_accounts, :select_accounts, :link_accounts,+ :select_existing_account, :link_existing_account, :connect_institution,+ :edit, :update, :destroy, :sync, :connection_status, :submit_mfa,+ :setup_accounts, :complete_account_setup+ ]def index@sophtron_items = Current.family.sophtron_items.active.ordered@@ -9,122 +26,190 @@ class SophtronItemsController < ApplicationControllerdef showend- # Preload Sophtron accounts in background (async, non-blocking)def preload_accounts- begin- # Check if family has credentials- unless Current.family.has_sophtron_credentials?- render json: { success: false, error: "no_credentials_configured", has_accounts: false }- return- end+ item = configured_sophtron_item+ unless item+ render json: { success: false, error: "no_credentials_configured", has_accounts: false }+ return+ end- cache_key = "sophtron_accounts_#{Current.family.id}"+ item.ensure_customer!- # Check if already cached- cached_accounts = Rails.cache.read(cache_key)+ unless item.connected_to_institution?+ render json: { success: false, error: "no_institution_connected", has_accounts: nil }+ return+ end- if cached_accounts.present?- render json: { success: true, has_accounts: cached_accounts.any?, cached: true }- return- end+ accounts = item.fetch_remote_accounts+ render json: { success: true, has_accounts: accounts.any?, cached: true }+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron preload error: #{e.message}")+ render json: { success: false, error: "api_error", error_message: t(".api_error"), has_accounts: nil }+ rescue StandardError => e+ Rails.logger.error("Unexpected error preloading Sophtron accounts: #{e.class}: #{e.message}")+ render json: { success: false, error: "unexpected_error", error_message: t(".unexpected_error"), has_accounts: nil }+ end- # Fetch from API- sophtron_provider = Provider::SophtronAdapter.build_provider(family: Current.family)+ def select_accounts+ item = configured_sophtron_item+ unless item+ render_or_redirect_setup_required+ return+ end- unless sophtron_provider.present?- render json: { success: false, error: "no_access_key", has_accounts: false }- return- end+ item.ensure_customer!- response = sophtron_provider.get_accounts- available_accounts = response.data[:accounts] || []+ if connect_new_institution_flow? || !item.connected_to_institution?+ prepare_connection_form(item)+ render :connect, layout: false+ return+ end- # Cache the accounts for 5 minutes- Rails.cache.write(cache_key, available_accounts, expires_in: 5.minutes)+ @available_accounts = item.reject_already_linked(item.fetch_remote_accounts)+ @accountable_type = params[:accountable_type] || "Depository"+ @return_to = safe_return_to_path- render json: { success: true, has_accounts: available_accounts.any?, cached: false }- rescue Provider::Error => e- Rails.logger.error("Sophtron preload error: #{e.message}")- # API error (bad key, network issue, etc) - keep button visible, show error when clicked- render json: { success: false, error: "api_error", error_message: t(".api_error"), has_accounts: nil }- rescue StandardError => e- Rails.logger.error("Unexpected error preloading Sophtron accounts: #{e.class}: #{e.message}")- # Unexpected error - keep button visible, show error when clicked- render json: { success: false, error: "unexpected_error", error_message: t(".unexpected_error"), has_accounts: nil }+ if @available_accounts.empty?+ redirect_to new_account_path, alert: t(".no_accounts_found")+ returnend++ render layout: false+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron API error in select_accounts: #{e.message}")+ render_api_error(t(".api_error"), safe_return_to_path)+ rescue StandardError => e+ Rails.logger.error("Unexpected error in select_accounts: #{e.class}: #{e.message}")+ render_api_error(t(".unexpected_error"), safe_return_to_path)end- # Fetch available accounts from Sophtron API and show selection UI- def select_accounts- begin- # Check if family has Sophtron credentials configured- unless Current.family.has_sophtron_credentials?- if turbo_frame_request?- # Render setup modal for turbo frame requests- render partial: "sophtron_items/setup_required", layout: false- else- # Redirect for regular requests- redirect_to settings_providers_path,- alert: t(".no_credentials_configured")- end- return- end+ def connect_institution+ if params[:institution_id].blank? || params[:bank_username].blank? || params[:bank_password].blank?+ redirect_to select_accounts_sophtron_items_path(connection_context_params), alert: t(".missing_parameters")+ return+ end- cache_key = "sophtron_accounts_#{Current.family.id}"+ item = item_for_institution_connection(@sophtron_item)+ item.ensure_customer!+ response = sophtron_response_data!(+ item.sophtron_provider.create_user_institution(+ institution_id: params[:institution_id],+ username: params[:bank_username],+ password: params[:bank_password],+ pin: ""+ )+ ).with_indifferent_access- # Try to get cached accounts first- @available_accounts = Rails.cache.read(cache_key)+ job_id = response[:JobID] || response[:job_id]+ user_institution_id = response[:UserInstitutionID] || response[:user_institution_id]- # If not cached, fetch from API- if @available_accounts.nil?- sophtron_provider = Provider::SophtronAdapter.build_provider(family: Current.family)+ if job_id.blank? || user_institution_id.blank?+ raise Provider::Sophtron::Error.new("Sophtron did not return JobID and UserInstitutionID", :invalid_response)+ end- unless sophtron_provider.present?- redirect_to settings_providers_path, alert: t(".no_access_key")- return- end+ item.update!(+ name: item.name.presence || t("sophtron_items.defaults.name"),+ institution_id: params[:institution_id],+ institution_name: params[:institution_name],+ user_institution_id: user_institution_id,+ current_job_id: job_id,+ raw_job_payload: response,+ job_status: nil,+ last_connection_error: nil,+ status: :good+ )- response = sophtron_provider.get_accounts- @available_accounts = response.data[:accounts] || []+ redirect_to connection_status_sophtron_item_path(item, connection_context_params)+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron connect institution error: #{e.message}")+ redirect_to select_accounts_sophtron_items_path(connection_context_params), alert: t(".api_error", message: e.message)+ end- # Cache the accounts for 5 minutes- Rails.cache.write(cache_key, @available_accounts, expires_in: 5.minutes)- end+ def connection_status+ if prefetch_request?+ head :no_content+ return+ end- # Filter out already linked accounts- sophtron_item = Current.family.sophtron_items.first- if sophtron_item- linked_account_ids = sophtron_item.sophtron_accounts.joins(:account_provider).pluck(:account_id)- @available_accounts = @available_accounts.reject { |acc| linked_account_ids.include?(acc[:id].to_s) }- end+ if @sophtron_item.current_job_id.blank?+ redirect_to select_accounts_sophtron_items_path(connection_context_params)+ return+ end- @accountable_type = params[:accountable_type] || "Depository"- @return_to = safe_return_to_path+ @poll_attempt = requested_poll_attempt+ if @poll_attempt > connection_status_max_polls+ render_connection_timeout+ return+ end++ job = sophtron_response_data!(@sophtron_item.sophtron_provider.get_job_information(@sophtron_item.current_job_id))+ @sophtron_item.upsert_job_snapshot!(job)++ if Provider::Sophtron.job_success?(job)+ @sophtron_item.update!(+ current_job_id: nil,+ last_connection_error: nil,+ pending_account_setup: true,+ status: :good+ )+ render_account_selection(@sophtron_item, force_refresh: true)+ elsif Provider::Sophtron.job_requires_input?(job)+ @challenge = @sophtron_item.build_mfa_challenge(job)+ prepare_connection_status_context+ render :mfa, layout: false+ elsif post_mfa_polling? && Provider::Sophtron.job_completed?(job)+ return if render_account_selection_if_accounts_available(@sophtron_item)++ render_pending_connection_status+ elsif Provider::Sophtron.job_failed?(job)+ failure_message = sophtron_connection_failure_message(job)+ @sophtron_item.update!(+ current_job_id: nil,+ user_institution_id: nil,+ last_connection_error: failure_message,+ status: :requires_update+ )+ render_institution_connection_error(failure_message)+ else+ render_pending_connection_status+ end+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron job polling error: #{e.message}")+ render_api_error(t(".api_error", message: e.message), accounts_path)+ end- if @available_accounts.empty?- redirect_to new_account_path, alert: t(".no_accounts_found")+ def submit_mfa+ provider = @sophtron_item.sophtron_provider+ job_id = @sophtron_item.current_job_id++ case params[:mfa_type]+ when "security_answer"+ security_answers = normalized_security_answers+ unless security_answers+ redirect_to connection_status_sophtron_item_path(@sophtron_item, connection_context_params), alert: t(".invalid_security_answers")returnend- render layout: false- rescue Provider::Error => e- Rails.logger.error("Sophtron API error in select_accounts: #{e.message}")- @error_message = t(".api_error")- @return_path = safe_return_to_path- render partial: "sophtron_items/api_error",- locals: { error_message: @error_message, return_path: @return_path },- layout: false- rescue StandardError => e- Rails.logger.error("Unexpected error in select_accounts: #{e.class}: #{e.message}")- @error_message = t(".unexpected_error")- @return_path = safe_return_to_path- render partial: "sophtron_items/api_error",- locals: { error_message: @error_message, return_path: @return_path },- layout: false+ sophtron_response_data!(provider.update_job_security_answer(job_id, security_answers))+ when "token_choice"+ sophtron_response_data!(provider.update_job_token_input(job_id, token_choice: params[:token_choice]))+ when "token_input"+ sophtron_response_data!(provider.update_job_token_input(job_id, token_input: params[:token_input]))+ when "verify_phone"+ sophtron_response_data!(provider.update_job_token_input(job_id, verify_phone_flag: true))+ when "captcha"+ sophtron_response_data!(provider.update_job_captcha(job_id, params[:captcha_input]))+ else+ redirect_to connection_status_sophtron_item_path(@sophtron_item, connection_context_params), alert: t(".unknown_challenge")+ returnend++ redirect_to connection_status_sophtron_item_path(@sophtron_item, connection_context_params.merge(post_mfa: true))+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron MFA submission error: #{e.message}")+ redirect_to connection_status_sophtron_item_path(@sophtron_item, connection_context_params), alert: t(".api_error", message: e.message)end- # Create accounts from selected Sophtron accountsdef link_accountsselected_account_ids = params[:account_ids] || []accountable_type = params[:accountable_type] || "Depository"@@ -135,194 +220,103 @@ class SophtronItemsController < ApplicationControllerreturnend- # Create or find sophtron_item for this family- sophtron_item = Current.family.sophtron_items.first_or_create!(- name: t("sophtron_items.defaults.name")- )-- # Fetch account details from API- sophtron_provider = Provider::SophtronAdapter.build_provider(family: Current.family)- unless sophtron_provider.present?- redirect_to new_account_path, alert: t(".no_access_key")+ item = configured_sophtron_item+ unless item&.connected_to_institution?+ redirect_to select_accounts_sophtron_items_path(accountable_type: accountable_type, return_to: return_to), alert: t(".no_institution_connected")returnend- response = sophtron_provider.get_accounts+ accounts_data = item.fetch_remote_accounts(force: true)created_accounts = []already_linked_accounts = []invalid_accounts = []selected_account_ids.each do |account_id|- # Find the account data from API response- account_data = response.data[:accounts].find { |acc| acc[:id].to_s == account_id.to_s }+ account_data = accounts_data.find { |account| SophtronItem.external_account_id(account).to_s == account_id.to_s }next unless account_data- # Validate account name is not blank (required by Account model)if account_data[:account_name].blank?invalid_accounts << account_idRails.logger.warn "SophtronItemsController - Skipping account #{account_id} with blank name"nextend- # Create or find sophtron_account- sophtron_account = sophtron_item.sophtron_accounts.find_or_initialize_by(- account_id: account_id.to_s- )- sophtron_account.upsert_sophtron_snapshot!(account_data)- sophtron_account.save!- # Check if this sophtron_account is already linked+ sophtron_account = item.upsert_sophtron_account(account_data)+if sophtron_account.account_provider.present?already_linked_accounts << account_data[:account_name]nextend- # Create the internal Account with proper balance initialization- account = Account.create_and_sync(- {- family: Current.family,- name: account_data[:account_name],- balance: 0, # Initial balance will be set during sync- currency: account_data[:currency] || "USD",- accountable_type: accountable_type,- accountable_attributes: {}- },- skip_initial_sync: true- )- # Link account to sophtron_account via account_providers join table- AccountProvider.create!(- account: account,- provider: sophtron_account- )+ ActiveRecord::Base.transaction do+ account = Account.create_and_sync(+ {+ family: Current.family,+ name: account_data[:account_name],+ balance: 0,+ currency: account_data[:currency] || "USD",+ accountable_type: accountable_type,+ accountable_attributes: {}+ },+ skip_initial_sync: true+ )- created_accounts << account- end-- # Trigger sync to fetch transactions if any accounts were created- sophtron_item.sync_later if created_accounts.any?-- # Build appropriate flash message- if invalid_accounts.any? && created_accounts.empty? && already_linked_accounts.empty?- # All selected accounts were invalid (blank names)- redirect_to new_account_path, alert: t(".invalid_account_names", count: invalid_accounts.count)- elsif invalid_accounts.any? && (created_accounts.any? || already_linked_accounts.any?)- # Some accounts were created/already linked, but some had invalid names- redirect_to return_to || accounts_path,- alert: t(".partial_invalid",- created_count: created_accounts.count,- already_linked_count: already_linked_accounts.count,- invalid_count: invalid_accounts.count)- elsif created_accounts.any? && already_linked_accounts.any?- redirect_to return_to || accounts_path,- notice: t(".partial_success",- created_count: created_accounts.count,- already_linked_count: already_linked_accounts.count,- already_linked_names: already_linked_accounts.join(", "))- elsif created_accounts.any?- redirect_to return_to || accounts_path,- notice: t(".success", count: created_accounts.count)- elsif already_linked_accounts.any?- redirect_to return_to || accounts_path,- alert: t(".all_already_linked",- count: already_linked_accounts.count,- names: already_linked_accounts.join(", "))- else- redirect_to new_account_path, alert: t(".link_failed")+ AccountProvider.create!(account: account, provider: sophtron_account)+ created_accounts << account+ endend- rescue Provider::Error => e- redirect_to new_account_path, alert: t(".api_error")- Rails.logger.error("Sophtron API error in link_accounts: #{e.message}")++ item.start_initial_load_later if created_accounts.any?+ redirect_after_account_link(return_to, created_accounts, already_linked_accounts, invalid_accounts)+ rescue Provider::Sophtron::Error => e+ redirect_to new_account_path, alert: t(".api_error", message: e.message)end- # Fetch available Sophtron accounts to link with an existing accountdef select_existing_account- account_id = params[:account_id]-- unless account_id.present?+ unless params[:account_id].present?redirect_to accounts_path, alert: t(".no_account_specified")returnend- @account = Current.family.accounts.find(account_id)+ @account = Current.family.accounts.find(params[:account_id])- # Check if account is already linkedif @account.account_providers.exists?redirect_to accounts_path, alert: t(".account_already_linked")returnend- # Check if family has Sophtron credentials configured- unless Current.family.has_sophtron_credentials?- if turbo_frame_request?- # Render setup modal for turbo frame requests- render partial: "sophtron_items/setup_required", layout: false- else- # Redirect for regular requests- redirect_to settings_providers_path,- alert: t(".no_credentials_configured",- default: "Please configure your Sophtron API key first in Provider Settings.")- end+ item = configured_sophtron_item+ unless item+ render_or_redirect_setup_requiredreturnend- begin- cache_key = "sophtron_accounts_#{Current.family.id}"-- # Try to get cached accounts first- @available_accounts = Rails.cache.read(cache_key)-- # If not cached, fetch from API- if @available_accounts.nil?- sophtron_provider = Provider::SophtronAdapter.build_provider(family: Current.family)+ item.ensure_customer!- unless sophtron_provider.present?- redirect_to settings_providers_path, alert: t(".no_access_key")- return- end-- response = sophtron_provider.get_accounts- @available_accounts = response.data[:accounts] || []-- # Cache the accounts for 5 minutes- Rails.cache.write(cache_key, @available_accounts, expires_in: 5.minutes)- end-- if @available_accounts.empty?- redirect_to accounts_path, alert: t(".no_accounts_found")- return- end-- # Filter out already linked accounts- sophtron_item = Current.family.sophtron_items.first- if sophtron_item- linked_account_ids = sophtron_item.sophtron_accounts.joins(:account_provider).pluck(:account_id)- @available_accounts = @available_accounts.reject { |acc| linked_account_ids.include?(acc[:id].to_s) }- end+ unless item.connected_to_institution?+ prepare_connection_form(item, account: @account)+ render :connect, layout: false+ return+ end- if @available_accounts.empty?- redirect_to accounts_path, alert: t(".all_accounts_already_linked")- return- end+ @available_accounts = item.reject_already_linked(item.fetch_remote_accounts)+ @return_to = safe_return_to_path- @return_to = safe_return_to_path-- render layout: false- rescue Provider::Error => e- Rails.logger.error("Sophtron API error in select_existing_account: #{e.message}")- @error_message = t(".api_error", message: e.message)- render partial: "sophtron_items/api_error",- locals: { error_message: @error_message, return_path: accounts_path },- layout: false- rescue StandardError => e- Rails.logger.error("Unexpected error in select_existing_account: #{e.class}: #{e.message}")- @error_message = t(".unexpected_error")- render partial: "sophtron_items/api_error",- locals: { error_message: @error_message, return_path: accounts_path },- layout: false+ if @available_accounts.empty?+ redirect_to accounts_path, alert: t(".all_accounts_already_linked")+ returnend++ render layout: false+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron API error in select_existing_account: #{e.message}")+ render_api_error(t(".api_error", message: e.message), accounts_path)+ rescue StandardError => e+ Rails.logger.error("Unexpected error in select_existing_account: #{e.class}: #{e.message}")+ render_api_error(t(".unexpected_error"), accounts_path)end- # Link a selected Sophtron account to an existing accountdef link_existing_accountaccount_id = params[:account_id]sophtron_account_id = params[:sophtron_account_id]@@ -333,67 +327,44 @@ class SophtronItemsController < ApplicationControllerreturnend- @account = Current.family.accounts.find(account_id)+ account = Current.family.accounts.find(account_id)- # Check if account is already linked- if @account.account_providers.exists?+ if account.account_providers.exists?redirect_to accounts_path, alert: t(".account_already_linked")returnend- # Create or find sophtron_item for this family- sophtron_item = Current.family.sophtron_items.first_or_create!(- name: "Sophtron Connection"- )-- # Fetch account details from API- sophtron_provider = Provider::SophtronAdapter.build_provider(family: Current.family)- unless sophtron_provider.present?- redirect_to accounts_path, alert: t(".no_access_key")+ item = configured_sophtron_item+ unless item&.connected_to_institution?+ redirect_to accounts_path, alert: t(".no_institution_connected")returnend- response = sophtron_provider.get_accounts-- # Find the selected Sophtron account data- account_data = response.data[:accounts].find { |acc| acc[:id].to_s == sophtron_account_id.to_s }+ account_data = item.fetch_remote_accounts(force: true).find { |remote_account| SophtronItem.external_account_id(remote_account).to_s == sophtron_account_id.to_s }unless account_dataredirect_to accounts_path, alert: t(".sophtron_account_not_found")returnend- # Validate account name is not blank (required by Account model)if account_data[:account_name].blank?redirect_to accounts_path, alert: t(".invalid_account_name")returnend- # Create or find sophtron_account- sophtron_account = sophtron_item.sophtron_accounts.find_or_initialize_by(- account_id: sophtron_account_id.to_s- )- sophtron_account.upsert_sophtron_snapshot!(account_data)- sophtron_account.save!+ sophtron_account = item.upsert_sophtron_account(account_data)- # Check if this sophtron_account is already linked to another accountif sophtron_account.account_provider.present?redirect_to accounts_path, alert: t(".sophtron_account_already_linked")returnend- # Link account to sophtron_account via account_providers join table- AccountProvider.create!(- account: @account,- provider: sophtron_account- )+ AccountProvider.create!(account: account, provider: sophtron_account)+ item.start_initial_load_later- # Trigger sync to fetch transactions- sophtron_item.sync_later- redirect_to return_to || accounts_path,- notice: t(".success", account_name: @account.name)- rescue Provider::Error => e+ redirect_to return_to || accounts_path, notice: t(".success", account_name: account.name)+ rescue Provider::Sophtron::Error => eRails.logger.error("Sophtron API error in link_existing_account: #{e.message}")- redirect_to accounts_path, alert: t(".api_error")+ redirect_to accounts_path, alert: t(".api_error", message: e.message)enddef new@@ -403,84 +374,48 @@ class SophtronItemsController < ApplicationControllerdef create@sophtron_item = Current.family.sophtron_items.build(sophtron_params)@sophtron_item.name ||= t("sophtron_items.defaults.name")+if @sophtron_item.save- # Trigger initial sync to fetch accounts- @sophtron_item.sync_later- if turbo_frame_request?- flash.now[:notice] = t(".success")- @sophtron_items = Current.family.sophtron_items.ordered- render turbo_stream: [- turbo_stream.replace(- "sophtron-providers-panel",- partial: "settings/providers/sophtron_panel",- locals: { sophtron_items: @sophtron_items }- ),- *flash_notification_stream_items- ]- else- redirect_to accounts_path, notice: t(".success"), status: :see_other+ unless verify_and_provision_customer(@sophtron_item)+ render_sophtron_panel_error(:new, @sophtron_item.last_connection_error)+ returnend++ render_sophtron_panel_success(:create)else- @error_message = @sophtron_item.errors.full_messages.join(", ")- if turbo_frame_request?- render turbo_stream: turbo_stream.replace(- "sophtron-providers-panel",- partial: "settings/providers/sophtron_panel",- locals: { error_message: @error_message }- ), status: :unprocessable_entity- else- render :new, status: :unprocessable_entity- end+ render_sophtron_panel_error(:new, @sophtron_item.errors.full_messages.join(", "))endend+def editenddef updateif @sophtron_item.update(sophtron_params)- if turbo_frame_request?- flash.now[:notice] = t(".success")- @sophtron_items = Current.family.sophtron_items.ordered- render turbo_stream: [- turbo_stream.replace(- "sophtron-providers-panel",- partial: "settings/providers/sophtron_panel",- locals: { sophtron_items: @sophtron_items }- ),- *flash_notification_stream_items- ]- else- redirect_to accounts_path, notice: t(".success"), status: :see_other+ unless verify_and_provision_customer(@sophtron_item)+ render_sophtron_panel_error(:edit, @sophtron_item.last_connection_error)+ returnend++ render_sophtron_panel_success(:update)else- @error_message = @sophtron_item.errors.full_messages.join(", ")- if turbo_frame_request?- render turbo_stream: turbo_stream.replace(- "sophtron-providers-panel",- partial: "settings/providers/sophtron_panel",- locals: { error_message: @error_message }- ), status: :unprocessable_entity- else- render :edit, status: :unprocessable_entity- end+ render_sophtron_panel_error(:edit, @sophtron_item.errors.full_messages.join(", "))endenddef destroy- # Ensure we detach provider links before scheduling deletionbegin@sophtron_item.unlink_all!(dry_run: false)rescue => eRails.logger.warn("Sophtron unlink during destroy failed: #{e.class} - #{e.message}")end+@sophtron_item.destroy_laterredirect_to accounts_path, notice: t(".success")enddef sync- unless @sophtron_item.syncing?- @sophtron_item.sync_later- end+ @sophtron_item.sync_later unless @sophtron_item.syncing?respond_to do |format|format.html { redirect_back_or_to accounts_path }@@ -488,20 +423,14 @@ class SophtronItemsController < ApplicationControllerendend- # Show unlinked Sophtron accounts for setup (similar to SimpleFIN setup_accounts)def setup_accounts- # First, ensure we have the latest accounts from the API@api_error = fetch_sophtron_accounts_from_api- # Get Sophtron accounts that are not linked (no AccountProvider)@sophtron_accounts = @sophtron_item.sophtron_accounts.left_joins(:account_provider).where(account_providers: { id: nil })- # Get supported account types from the adaptersupported_types = Provider::SophtronAdapter.supported_account_types-- # Map of account type keys to their internal valuesaccount_type_keys = {"depository" => "Depository","credit_card" => "CreditCard",@@ -510,16 +439,12 @@ class SophtronItemsController < ApplicationController"other_asset" => "OtherAsset"}- # Build account type options using i18n, filtering to supported typesall_account_type_options = account_type_keys.filter_map do |key, type|next unless supported_types.include?(type)[ t(".account_types.#{key}"), type ]end- # Add "Skip" option at the beginning@account_type_options = [ [ t(".account_types.skip"), "skip" ] ] + all_account_type_options-- # Subtype options for each account type@subtype_options = {"Depository" => {label: "Account Subtype:",@@ -554,65 +479,50 @@ class SophtronItemsController < ApplicationControllerdef complete_account_setupaccount_types = params[:account_types] || {}account_subtypes = params[:account_subtypes] || {}-- # Valid account types for this providervalid_types = Provider::SophtronAdapter.supported_account_types-created_accounts = []skipped_count = 0beginActiveRecord::Base.transaction doaccount_types.each do |sophtron_account_id, selected_type|- # Skip accounts marked as "skip"if selected_type == "skip" || selected_type.blank?skipped_count += 1nextend- # Validate account type is supportedunless valid_types.include?(selected_type)Rails.logger.warn("Invalid account type '#{selected_type}' submitted for Sophtron account #{sophtron_account_id}")nextend- # Find account - scoped to this item to prevent cross-item manipulationsophtron_account = @sophtron_item.sophtron_accounts.find_by(id: sophtron_account_id)unless sophtron_accountRails.logger.warn("Sophtron account #{sophtron_account_id} not found for item #{@sophtron_item.id}")nextend- # Skip if already linked (race condition protection)if sophtron_account.account_provider.present?Rails.logger.info("Sophtron account #{sophtron_account_id} already linked, skipping")nextendselected_subtype = account_subtypes[sophtron_account_id]-- # Default subtype for CreditCard since it only has one optionselected_subtype = "credit_card" if selected_type == "CreditCard" && selected_subtype.blank?- # Create account with user-selected type and subtype (raises on failure)account = Account.create_and_sync({- family: Current.family,- name: sophtron_account.name,- balance: sophtron_account.balance || 0,- currency: sophtron_account.currency || "USD",- accountable_type: selected_type,- accountable_attributes: selected_subtype.present? ? { subtype: selected_subtype } : {}+ family: Current.family,+ name: sophtron_account.name,+ balance: sophtron_account.balance || 0,+ currency: sophtron_account.currency || "USD",+ accountable_type: selected_type,+ accountable_attributes: selected_subtype.present? ? { subtype: selected_subtype } : {}},skip_initial_sync: true)- # Link account to sophtron_account via account_providers join table (raises on failure)- AccountProvider.create!(- account: account,- provider: sophtron_account- )-+ AccountProvider.create!(account: account, provider: sophtron_account)created_accounts << accountendend@@ -630,25 +540,19 @@ class SophtronItemsController < ApplicationControllerreturnend- # Trigger a sync to process transactions- @sophtron_item.sync_later if created_accounts.any?+ @sophtron_item.start_initial_load_later if created_accounts.any?- # Set appropriate flash message- if created_accounts.any?- flash[:notice] = t(".success", count: created_accounts.count)+ flash[:notice] = if created_accounts.any?+ t(".success", count: created_accounts.count)elsif skipped_count > 0- flash[:notice] = t(".all_skipped")+ t(".all_skipped")else- flash[:notice] = t(".no_accounts")+ t(".no_accounts")endif turbo_frame_request?- # Recompute data needed by Accounts#index partials@manual_accounts = Account.uncached {- Current.family.accounts- .visible_manual- .order(:name)- .to_a+ Current.family.accounts.visible_manual.order(:name).to_a}@sophtron_items = Current.family.sophtron_items.orderedmanual_accounts_stream = if @manual_accounts.any?@@ -676,52 +580,305 @@ class SophtronItemsController < ApplicationControllerprivate- # Fetch Sophtron accounts from the API and store them locally- # Returns nil on success, or an error message string on failure- def fetch_sophtron_accounts_from_api- # Skip if we already have accounts cached- return nil unless @sophtron_item.sophtron_accounts.empty?+ def configured_sophtron_item+ Current.family.configured_sophtron_item+ end++ def normalized_security_answers+ raw_answers = Array(params[:security_answers]).flatten+ return if raw_answers.size > MAX_SECURITY_ANSWERS+ return if raw_answers.any? { |answer| answer.to_s.length > MAX_SECURITY_ANSWER_LENGTH }++ answers = raw_answers.filter_map do |answer|+ answer.to_s.strip.presence+ end++ return if answers.empty?++ answers+ end++ def sophtron_response_data!(response)+ Provider::Sophtron.response_data!(response)+ end++ def verify_and_provision_customer(item)+ provider = item.sophtron_provider+ raise Provider::Sophtron::Error.new("Sophtron provider is not configured", :configuration_error) unless provider++ sophtron_response_data!(provider.health_check_auth)+ item.ensure_customer!(provider: provider)+ true+ rescue Provider::Sophtron::Error => e+ item.update(status: :requires_update, last_connection_error: e.message)+ Rails.logger.error("Sophtron customer provisioning failed: #{e.message}")+ false+ end++ def render_sophtron_panel_success(action_name)+ if turbo_frame_request?+ flash.now[:notice] = t("sophtron_items.#{action_name}.success")+ @sophtron_items = Current.family.sophtron_items.ordered+ render turbo_stream: [+ turbo_stream.replace(+ "sophtron-providers-panel",+ partial: "settings/providers/sophtron_panel",+ locals: { sophtron_items: @sophtron_items }+ ),+ *flash_notification_stream_items+ ]+ else+ redirect_to accounts_path, notice: t("sophtron_items.#{action_name}.success"), status: :see_other+ end+ end- # Validate Access key is configured- unless @sophtron_item.credentials_configured?- return t("sophtron_items.setup_accounts.no_access_key")+ def render_sophtron_panel_error(view_name, message)+ @error_message = message+ if turbo_frame_request?+ render turbo_stream: turbo_stream.replace(+ "sophtron-providers-panel",+ partial: "settings/providers/sophtron_panel",+ locals: { error_message: @error_message }+ ), status: :unprocessable_entity+ else+ render view_name, status: :unprocessable_entityend+ end- # Use the specific sophtron_item's provider (scoped to this family's item)- sophtron_provider = @sophtron_item.sophtron_provider- unless sophtron_provider.present?- return t("sophtron_items.setup_accounts.no_access_key")+ def render_or_redirect_setup_required+ if turbo_frame_request?+ render partial: "sophtron_items/setup_required", layout: false+ else+ redirect_to settings_providers_path, alert: t("sophtron_items.select_accounts.no_credentials_configured")end+ end- begin- response = sophtron_provider.get_accounts- available_accounts = response.data[:accounts] || []+ def item_for_institution_connection(item)+ return item unless connect_new_institution_flow? && should_create_sophtron_item_for_new_institution?(item)++ Current.family.sophtron_items.create!(+ name: item.name.presence || t("sophtron_items.defaults.name"),+ user_id: item.user_id,+ access_key: item.access_key,+ base_url: item.base_url,+ customer_id: item.customer_id,+ customer_name: item.customer_name,+ raw_customer_payload: item.raw_customer_payload,+ sync_start_date: item.sync_start_date+ )+ end- if available_accounts.empty?- return nil- end+ def should_create_sophtron_item_for_new_institution?(item)+ item.user_institution_id.present? ||+ item.current_job_id.present? ||+ item.institution_id.present? ||+ item.institution_name.present? ||+ item.sophtron_accounts.exists?+ end- available_accounts.each_with_index do |account_data, index|- next if account_data[:account_name].blank?+ def prepare_connection_form(item, account: nil)+ @sophtron_item = item+ @account = account+ @accountable_type = params[:accountable_type] || "Depository"+ @return_to = safe_return_to_path+ @connect_new_institution = connect_new_institution_flow?+ @institution_search = params[:institution_name].to_s.strip+ @institutions = []- sophtron_account = @sophtron_item.sophtron_accounts.find_or_initialize_by(- account_id: account_data[:account_id].to_s- )- sophtron_account.upsert_sophtron_snapshot!(account_data)- sophtron_account.save!- end+ if @institution_search.length >= 2+ @institutions = sophtron_response_data!(item.sophtron_provider.search_institutions(@institution_search))+ end+ end++ def render_account_selection(item, force_refresh: false)+ @available_accounts = item.reject_already_linked(item.fetch_remote_accounts(force: force_refresh))+ @accountable_type = params[:accountable_type] || "Depository"+ @return_to = safe_return_to_path++ if params[:account_id].present?+ @account = Current.family.accounts.find(params[:account_id])+ render :select_existing_account, layout: false+ else+ render :select_accounts, layout: false+ end+ end++ def render_account_selection_if_accounts_available(item)+ accounts = item.fetch_remote_accounts(force: true)+ return false if accounts.empty?++ item.update!(+ current_job_id: nil,+ last_connection_error: nil,+ pending_account_setup: true,+ status: :good+ )++ @available_accounts = item.reject_already_linked(accounts)+ @accountable_type = params[:accountable_type] || "Depository"+ @return_to = safe_return_to_path++ if params[:account_id].present?+ @account = Current.family.accounts.find(params[:account_id])+ render :select_existing_account, layout: false+ else+ render :select_accounts, layout: false+ end++ true+ rescue Provider::Sophtron::Error => e+ Rails.logger.info("Sophtron accounts are not available after completed job #{item.current_job_id}: #{e.message}")+ false+ end++ def render_pending_connection_status+ if @poll_attempt >= connection_status_max_polls+ render_connection_timeout+ return+ end++ prepare_connection_status_context+ @next_poll_attempt = @poll_attempt + 1+ render :connection_status, layout: false+ end++ def prepare_connection_status_context+ @accountable_type = params[:accountable_type] || "Depository"+ @account_id = params[:account_id]+ @return_to = safe_return_to_path+ @poll_interval_ms = CONNECTION_STATUS_POLL_INTERVAL_MS+ @post_mfa_polling = post_mfa_polling?+ @max_poll_attempts = connection_status_max_polls+ end++ def requested_poll_attempt+ poll_attempt = params[:poll_attempt].to_i+ poll_attempt.positive? ? poll_attempt : 1+ end++ def render_connection_timeout+ @poll_attempt = connection_status_max_polls if @poll_attempt.to_i > connection_status_max_polls+ @poll_attempt = 1 if @poll_attempt.to_i < 1+ @sophtron_item.update!(+ last_connection_error: t(".timeout"),+ status: :requires_update+ )+ prepare_connection_status_context+ @timed_out = true+ render :connection_status, layout: false+ end++ def connection_status_max_polls+ if post_mfa_polling?+ POST_MFA_CONNECTION_STATUS_MAX_POLLS+ elsif login_progress_polling?+ LOGIN_PROGRESS_CONNECTION_STATUS_MAX_POLLS+ else+ CONNECTION_STATUS_MAX_POLLS+ end+ end++ def post_mfa_polling?+ ActiveModel::Type::Boolean.new.cast(params[:post_mfa]) || post_mfa_job_payload?(@sophtron_item.raw_job_payload)+ end++ def post_mfa_job_payload?(job_payload)+ job = (job_payload || {}).with_indifferent_access+ job[:TokenInput].present? || %w[TokenInput TransactionTable].include?(job[:LastStep].to_s)+ end++ def login_progress_polling?+ login_progress_job_payload?(@sophtron_item.raw_job_payload)+ end++ def login_progress_job_payload?(job_payload)+ job = (job_payload || {}).with_indifferent_access+ last_status = job[:LastStatus] || job[:last_status]+ return false if Provider::Sophtron.failure_job_status?(last_status)- nil # Success- rescue Provider::Error => e- Rails.logger.error("Sophtron API error: #{e.message}")- t("sophtron_items.setup_accounts.api_error")- rescue StandardError => e- Rails.logger.error("Unexpected error fetching Sophtron accounts: #{e.class}: #{e.message}")- Rails.logger.error(e.backtrace.first(10).join("\n"))- t("sophtron_items.setup_accounts.api_error")+ job[:LastStep].present? || job[:last_step].present? || last_status.present?+ end++ def prefetch_request?+ [+ request.headers["X-Sec-Purpose"],+ request.headers["Sec-Purpose"],+ request.headers["Purpose"]+ ].any? { |value| value.to_s.include?("prefetch") }+ end++ def render_institution_connection_error(message)+ render_api_error(+ message,+ select_accounts_sophtron_items_path(connection_context_params.except(:post_mfa, "post_mfa")),+ heading: t("sophtron_items.api_error.institution_unable_to_connect"),+ issue_keys: %w[bank_credentials verification_code institution_timeout unsupported_mfa],+ action_label: t("sophtron_items.api_error.try_again")+ )+ end++ def sophtron_connection_failure_message(job)+ last_status = job.with_indifferent_access[:LastStatus].to_s+ return t("sophtron_items.connection_status.failed_timeout") if last_status.match?(/timeout/i)++ t("sophtron_items.connection_status.failed")+ end++ def render_api_error(message, return_path, heading: nil, issue_keys: nil, action_label: nil)+ render partial: "sophtron_items/api_error",+ locals: {+ error_message: message,+ return_path: return_path,+ heading: heading,+ issue_keys: issue_keys,+ action_label: action_label+ },+ layout: false+ end++ def redirect_after_account_link(return_to, created_accounts, already_linked_accounts, invalid_accounts)+ if invalid_accounts.any? && created_accounts.empty? && already_linked_accounts.empty?+ redirect_to new_account_path, alert: t(".invalid_account_names", count: invalid_accounts.count)+ elsif invalid_accounts.any? && (created_accounts.any? || already_linked_accounts.any?)+ redirect_to return_to || accounts_path,+ alert: t(".partial_invalid",+ created_count: created_accounts.count,+ already_linked_count: already_linked_accounts.count,+ invalid_count: invalid_accounts.count)+ elsif created_accounts.any? && already_linked_accounts.any?+ redirect_to return_to || accounts_path,+ notice: t(".partial_success",+ created_count: created_accounts.count,+ already_linked_count: already_linked_accounts.count,+ already_linked_names: already_linked_accounts.join(", "))+ elsif created_accounts.any?+ redirect_to return_to || accounts_path, notice: t(".success", count: created_accounts.count)+ elsif already_linked_accounts.any?+ redirect_to return_to || accounts_path,+ alert: t(".all_already_linked",+ count: already_linked_accounts.count,+ names: already_linked_accounts.join(", "))+ else+ redirect_to new_account_path, alert: t(".link_failed")endend+ def fetch_sophtron_accounts_from_api+ return nil unless @sophtron_item.sophtron_accounts.empty?+ return t("sophtron_items.setup_accounts.no_access_key") unless @sophtron_item.credentials_configured?+ return t("sophtron_items.setup_accounts.no_institution_connected") unless @sophtron_item.connected_to_institution?++ @sophtron_item.fetch_remote_accounts(force: true)+ nil+ rescue Provider::Sophtron::Error => e+ Rails.logger.error("Sophtron API error: #{e.message}")+ t("sophtron_items.setup_accounts.api_error")+ rescue StandardError => e+ Rails.logger.error("Unexpected error fetching Sophtron accounts: #{e.class}: #{e.message}")+ Rails.logger.error(e.backtrace.first(10).join("\n"))+ t("sophtron_items.setup_accounts.api_error")+ end+def set_sophtron_item@sophtron_item = Current.family.sophtron_items.find(params[:id])end@@ -730,27 +887,27 @@ class SophtronItemsController < ApplicationControllerparams.require(:sophtron_item).permit(:name, :user_id, :access_key, :base_url, :sync_start_date)end- # Sanitize return_to parameter to prevent XSS attacks- # Only allow internal paths, reject external URLs and javascript: URIs+ def connection_context_params+ params.permit(:accountable_type, :account_id, :return_to, :post_mfa, :connect_new_institution).to_h.compact+ end++ def connect_new_institution_flow?+ ActiveModel::Type::Boolean.new.cast(params[:connect_new_institution])+ end+def safe_return_to_pathreturn nil if params[:return_to].blank?return_to = params[:return_to].to_s- # Parse the URL to check if it's externalbeginuri = URI.parse(return_to)-- # Reject absolute URLs with schemes (http:, https:, javascript:, etc.)- # Only allow relative pathsreturn nil if uri.scheme.present? || uri.host.present?return nil if return_to.start_with?("//")- # Ensure the path starts with / (is a relative path)return nil unless return_to.start_with?("/")return_torescue URI::InvalidURIError- # If the URI is invalid, reject itnilendenddiff --git a/app/javascript/controllers/polling_controller.js b/app/javascript/controllers/polling_controller.jsindex 0e9e743f8a..8478b2f695 100644--- a/app/javascript/controllers/polling_controller.js+++ b/app/javascript/controllers/polling_controller.js@@ -6,6 +6,7 @@ export default class extends Controller {static values = {url: String,interval: { type: Number, default: 3000 },+ frameId: String,};connect() {@@ -33,10 +34,16 @@ export default class extends Controller {async refresh() {try {+ const frame = this.frameElement();+ if (!frame) {+ this.stopPolling();+ return;+ }+const response = await fetch(this.urlValue, {headers: {Accept: "text/html",- "Turbo-Frame": this.element.id,+ "Turbo-Frame": frame.id,},});@@ -46,13 +53,19 @@ export default class extends Controller {template.innerHTML = html;const newFrame = template.content.querySelector(- `turbo-frame#${this.element.id}`,+ `turbo-frame#${this.cssEscape(frame.id)}`,);if (newFrame) {- this.element.innerHTML = newFrame.innerHTML;+ if (frame === this.element) {+ this.syncPollingAttributes(newFrame);+ }+ frame.innerHTML = newFrame.innerHTML;// Check if we should stop polling (no more pending/processing exports)- if (!newFrame.hasAttribute("data-polling-url-value")) {+ if (+ frame === this.element &&+ !newFrame.hasAttribute("data-polling-url-value")+ ) {this.stopPolling();}}@@ -61,4 +74,41 @@ export default class extends Controller {console.error("Polling error:", error);}}++ frameElement() {+ if (this.hasFrameIdValue) {+ return document.getElementById(this.frameIdValue);+ }++ if (this.element.tagName.toLowerCase() === "turbo-frame") {+ return this.element;+ }++ return this.element.closest("turbo-frame");+ }++ cssEscape(value) {+ if (window.CSS?.escape) return CSS.escape(value);++ return value.replaceAll('"', '\\"');+ }++ syncPollingAttributes(newFrame) {+ const pollingUrl = newFrame.getAttribute("data-polling-url-value");+ const pollingInterval = newFrame.getAttribute(+ "data-polling-interval-value",+ );++ if (pollingUrl) {+ this.element.setAttribute("data-polling-url-value", pollingUrl);+ } else {+ this.element.removeAttribute("data-polling-url-value");+ }++ if (pollingInterval) {+ this.element.setAttribute("data-polling-interval-value", pollingInterval);+ } else {+ this.element.removeAttribute("data-polling-interval-value");+ }+ }}diff --git a/app/jobs/sophtron_initial_load_job.rb b/app/jobs/sophtron_initial_load_job.rbnew file mode 100644index 0000000000..ca91a9c644--- /dev/null+++ b/app/jobs/sophtron_initial_load_job.rb@@ -0,0 +1,20 @@+class SophtronInitialLoadJob < ApplicationJob+ queue_as :high_priority++ RETRY_DELAY = 10.seconds+ MAX_ATTEMPTS = 30++ def perform(sophtron_item, attempts_remaining: MAX_ATTEMPTS)+ if sophtron_item.syncing?+ if attempts_remaining.positive?+ self.class.set(wait: RETRY_DELAY).perform_later(sophtron_item, attempts_remaining: attempts_remaining - 1)+ else+ Rails.logger.warn("SophtronInitialLoadJob - gave up waiting for SophtronItem #{sophtron_item.id} to finish syncing")+ end++ return+ end++ sophtron_item.sync_later+ end+enddiff --git a/app/jobs/sophtron_refresh_poll_job.rb b/app/jobs/sophtron_refresh_poll_job.rbnew file mode 100644index 0000000000..1f5afb08aa--- /dev/null+++ b/app/jobs/sophtron_refresh_poll_job.rb@@ -0,0 +1,76 @@+class SophtronRefreshPollJob < ApplicationJob+ queue_as :high_priority++ POLL_INTERVAL = 4.seconds+ MAX_ATTEMPTS = 60++ def perform(sophtron_account, job_id:, attempts_remaining: MAX_ATTEMPTS, sync: nil)+ sophtron_item = sophtron_account.sophtron_item+ provider = sophtron_item.sophtron_provider+ raise Provider::Sophtron::Error.new("Sophtron provider is not configured", :configuration_error) unless provider++ job = Provider::Sophtron.response_data!(provider.get_job_information(job_id))+ sophtron_item.upsert_job_snapshot!(job)++ if Provider::Sophtron.job_requires_input?(job)+ mark_requires_update!(sophtron_item, job_id)+ elsif Provider::Sophtron.job_failed?(job)+ sophtron_item.update!(last_connection_error: "Sophtron refresh failed")+ elsif Provider::Sophtron.job_success?(job) || Provider::Sophtron.job_completed?(job)+ import_transactions!(sophtron_account, provider, sync)+ elsif attempts_remaining.to_i > 1+ self.class.set(wait: POLL_INTERVAL).perform_later(+ sophtron_account,+ job_id: job_id,+ attempts_remaining: attempts_remaining.to_i - 1,+ sync: sync+ )+ else+ sophtron_item.update!(last_connection_error: "Sophtron refresh did not finish before the polling timeout")+ end+ rescue Provider::Sophtron::Error => e+ handle_provider_error!(sophtron_account.sophtron_item, e)+ end++ private++ def import_transactions!(sophtron_account, provider, sync)+ sophtron_item = sophtron_account.sophtron_item+ result = SophtronItem::Importer.new(sophtron_item, sophtron_provider: provider, sync: sync)+ .import_transactions_after_refresh(sophtron_account)++ unless result[:success]+ attributes = { last_connection_error: result[:error] }+ attributes[:status] = :requires_update if result[:requires_update]+ sophtron_item.update!(attributes)+ return+ end++ SophtronAccount::Processor.new(sophtron_account.reload).process++ account = sophtron_account.current_account+ return unless account++ account.sync_later(+ parent_sync: sync,+ window_start_date: sync&.window_start_date,+ window_end_date: sync&.window_end_date+ )+ end++ def mark_requires_update!(sophtron_item, job_id)+ sophtron_item.update!(+ status: :requires_update,+ current_job_id: job_id,+ last_connection_error: "Sophtron refresh requires MFA"+ )+ end++ def handle_provider_error!(sophtron_item, error)+ requires_update = error.error_type.in?([ :unauthorized, :access_forbidden ])+ attributes = { last_connection_error: error.message }+ attributes[:status] = :requires_update if requires_update+ sophtron_item.update!(attributes)+ Rails.logger.error "SophtronRefreshPollJob - Sophtron API error for item #{sophtron_item.id}: #{error.message}"+ end+enddiff --git a/app/models/family/sophtron_connectable.rb b/app/models/family/sophtron_connectable.rbindex f2f241dad1..cc1fb3b0c2 100644--- a/app/models/family/sophtron_connectable.rb+++ b/app/models/family/sophtron_connectable.rb@@ -18,12 +18,14 @@ module Family::SophtronConnectablebase_url: base_url)- sophtron_item.sync_later-sophtron_itemenddef has_sophtron_credentials?sophtron_items.where.not(user_id: [ nil, "" ], access_key: [ nil, "" ]).exists?end++ def configured_sophtron_item+ sophtron_items.where.not(user_id: [ nil, "" ], access_key: [ nil, "" ]).ordered.first+ endenddiff --git a/app/models/provider/sophtron.rb b/app/models/provider/sophtron.rbindex 69526524c9..69261133f6 100644--- a/app/models/provider/sophtron.rb+++ b/app/models/provider/sophtron.rb@@ -1,373 +1,473 @@# Sophtron API client for account aggregation.#-# This provider implements the Sophtron API v2 for fetching bank account data,-# transactions, and balances. It uses HMAC-SHA256 authentication for secure-# API requests.-#-# The Sophtron API organizes data hierarchically:-# - Customers (identified by customer_id)-# - Accounts (identified by account_id within a customer)-# - Transactions (identified by transaction_id within an account)-#-# @example Initialize a Sophtron provider-# provider = Provider::Sophtron.new(-# "user123",-# "base64_encoded_access_key",-# base_url: "https://api.sophtron.com/api/v2"-# )-#-# @see https://www.sophtron.com Documentation for Sophtron API+# Sophtron uses two API shapes:+# - V2 REST endpoints for customer provisioning.+# - V1 RPC-style endpoints for institution connection, jobs, MFA, accounts, and transactions.class Provider::Sophtron < Providerinclude HTTParty- headers "User-Agent" => "Sure Finance So Client"+ DEFAULT_BASE_URL = "https://api.sophtron.com/api"+ USER_AGENT = "Sure Finance Sophtron Client"+ FAILURE_JOB_STATUSES = %w[Completed Timeout Failed Failure Error].freeze++ headers "User-Agent" => USER_AGENTdefault_options.merge!(verify: true, ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER, timeout: 120)+ Error = Class.new(Provider::Error) do+ attr_reader :error_type++ def initialize(message, error_type = :unknown, details: nil)+ @error_type = error_type+ super(message, details: details)+ end+ end+attr_reader :user_id, :access_key, :base_url- # Initializes a new Sophtron API client.- #- # @param user_id [String] Sophtron User ID for authentication- # @param access_key [String] Base64-encoded Sophtron Access Key- # @param base_url [String] Base URL for the Sophtron API (defaults to production)- def initialize(user_id, access_key, base_url: "https://api.sophtron.com/api/v2")+ def initialize(user_id, access_key, base_url: DEFAULT_BASE_URL)@user_id = user_id@access_key = access_key- @base_url = base_url+ @base_url = normalize_base_url(base_url)super()end- # Fetches all accounts across all customers for this Sophtron user.- #- # This method:- # 1. Fetches the list of customer IDs- # 2. For each customer, fetches their accounts- # 3. Normalizes and deduplicates the account data- # 4. Returns a combined list of all accounts- #- # @return [Hash] Account data with keys:- # - :accounts [Array<Hash>] Array of account objects- # - :total [Integer] Total number of accounts- # @raise [Provider::Error] if the API request fails- # @example- # result = provider.get_accounts- # # => { accounts: [{id: "123", account_name: "Checking", ...}], total: 1 }- def get_accounts- with_provider_response do- # fetching accounts for sophtron- # Obtain customer IDs using a dedicated helper- customer_ids = get_customer_ids-- all_accounts = []- customer_ids.each do |cust_id|- begin- accounts_resp = get_customer_accounts(cust_id)-- # `handle_response` returns parsed JSON (hash/array) so normalize- raw_accounts = if accounts_resp.is_a?(Hash) && accounts_resp[:accounts].is_a?(Array)- accounts_resp[:accounts]- elsif accounts_resp.is_a?(Array)- accounts_resp- else- []- end-- normalized = raw_accounts.map { |a| a.transform_keys { |k| k.to_s.underscore }.with_indifferent_access }-- # Ensure each account has a customer_id set- normalized.each do |acc|- # check common variants that may already exist- existing = acc[:customer_id]- acc[:customer_id] = cust_id.to_s if existing.blank?- end-- all_accounts.concat(normalized)- rescue Provider::Error => e- Rails.logger.warn("Failed to fetch accounts for customer #{cust_id}: #{e.message}")- rescue => e- Rails.logger.warn("Unexpected error fetching accounts for customer #{cust_id}: #{e.class} #{e.message}")- end- end+ def auth_header_for(method, api_path)+ auth_path = self.class.auth_path(api_path)+ plain_key = "#{method.to_s.upcase}\n#{auth_path}"+ key_bytes = Base64.decode64(access_key.to_s)+ raise ArgumentError, "decoded key is empty" if key_bytes.blank?+ signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), key_bytes, plain_key)+ "FIApiAUTH:#{user_id}:#{Base64.strict_encode64(signature)}:#{auth_path}"+ rescue ArgumentError => e+ raise Error.new("Invalid Sophtron Access Key: #{e.message}", :invalid_access_key)+ end- # Deduplicate by id where present- unique_accounts = all_accounts.uniq { |a| a[:id].to_s }+ def self.auth_path(api_path)+ path = URI.parse(api_path.to_s).path+ last_segment = path.to_s.split("/").last.to_s+ "/#{last_segment}".downcase+ rescue URI::InvalidURIError+ last_segment = api_path.to_s.split("?").first.to_s.split("/").last.to_s+ "/#{last_segment}".downcase+ end++ def self.job_success?(job)+ job = job.with_indifferent_access+ job[:SuccessFlag] == true || job[:success_flag] == true || job[:LastStatus].to_s == "AccountsReady" || job[:last_status].to_s == "AccountsReady"+ end++ def self.job_failed?(job)+ job = job.with_indifferent_access+ success_flag = job.key?(:SuccessFlag) ? job[:SuccessFlag] : job[:success_flag]+ last_status = job[:LastStatus] || job[:last_status]+ success_flag == false && failure_job_status?(last_status)+ end++ def self.job_completed?(job)+ job = job.with_indifferent_access+ (job[:LastStatus] || job[:last_status]).to_s == "Completed" && !job_failed?(job)+ end- { accounts: unique_accounts, total: unique_accounts.length }+ def self.failure_job_status?(last_status)+ status = last_status.to_s+ FAILURE_JOB_STATUSES.include?(status) || status.match?(/timeout|fail|error/i)+ end++ def self.job_requires_input?(job)+ job = job.with_indifferent_access+ job[:SecurityQuestion].present? ||+ job[:security_question].present? ||+ job[:TokenMethod].present? ||+ job[:token_method].present? ||+ job_token_input_required?(job) ||+ job[:TokenRead].present? ||+ job[:token_read].present? ||+ job[:CaptchaImage].present? ||+ job[:captcha_image].present?+ end++ def self.job_token_input_required?(job)+ job = job.with_indifferent_access+ token_input = job[:TokenInput] || job[:token_input]+ token_input.blank? && (+ job[:TokenSentFlag] == true ||+ job[:token_sent_flag] == true ||+ job[:TokenInputName].present? ||+ job[:token_input_name].present? ||+ job[:LastStep].to_s == "TokenInput" ||+ job[:last_step].to_s == "TokenInput"+ )+ end++ def self.parse_json_array(value)+ return [] if value.blank?+ return value if value.is_a?(Array)++ parsed = JSON.parse(value.to_s)+ parsed.is_a?(Array) ? parsed : Array(parsed)+ rescue JSON::ParserError+ Array(value)+ end++ def self.response_data!(response)+ return response unless response.respond_to?(:success?) && response.respond_to?(:data)+ return response.data if response.success?++ raise response.error || Error.new("Sophtron provider response did not include data", :invalid_response)+ end++ # GET /api/Institution/HealthCheckAuth+ def health_check_auth+ with_provider_response do+ request(:get, "/Institution/HealthCheckAuth", parse_json: false)endend- # Fetches transactions for a specific account.- #- # Retrieves transaction history for a given account within a date range.- # If no end date is provided, defaults to tomorrow to include today's transactions.- #- # @param customer_id [String] Sophtron customer ID- # @param account_id [String] Sophtron account ID- # @param start_date [Date, nil] Start date for transaction history (optional)- # @param end_date [Date, nil] End date for transaction history (defaults to tomorrow)- # @return [Hash] Transaction data with keys:- # - :transactions [Array<Hash>] Array of transaction objects- # - :total [Integer] Total number of transactions- # @raise [Provider::Error] if the API request fails- # @example- # result = provider.get_account_transactions("cust123", "acct456", start_date: 30.days.ago)- # # => { transactions: [{id: "tx1", amount: -50.00, ...}], total: 25 }- def get_account_transactions(customer_id, account_id, start_date: nil, end_date: nil)+ # GET /api/v2/customers+ def list_customerswith_provider_response do- query_params = {}+ parsed = request(:get, "/v2/customers")+ extract_array_response(parsed, :customers, :Customers)+ end+ end- if start_date- query_params[:startDate] = start_date.to_date- end- if end_date- query_params[:endDate] = end_date.to_date- else- query_params[:endDate] = Date.tomorrow- end+ # POST /api/v2/customers+ def create_customer(unique_id:, name:, source: "Sure")+ with_provider_response do+ request(+ :post,+ "/v2/customers",+ body: {+ UniqueID: unique_id,+ Name: name,+ Source: source+ }+ )+ end+ end- path = "/customers/#{ERB::Util.url_encode(customer_id.to_s)}/accounts/#{ERB::Util.url_encode(account_id.to_s)}/transactions"- path += "?#{URI.encode_www_form(query_params)}" unless query_params.empty?- url = "#{@base_url}#{path}"+ # POST /api/Institution/GetInstitutionByName+ def search_institutions(institution_name)+ with_provider_response do+ parsed = request(+ :post,+ "/Institution/GetInstitutionByName",+ body: { InstitutionName: institution_name.to_s }+ )+ extract_array_response(parsed, :institutions, :Institutions)+ end+ end- response = self.class.get(- url,- headers: auth_headers(url: url, http_method: "GET")+ # POST /api/UserInstitution/GetUserInstitutionsByUser+ def list_user_institutions+ with_provider_response do+ parsed = request(+ :post,+ "/UserInstitution/GetUserInstitutionsByUser",+ body: { UserID: user_id })+ extract_array_response(parsed, :user_institutions, :UserInstitutions)+ end+ end- parsed = handle_response(response)- # Normalize transactions response into { transactions: [...], total: N }- if parsed.is_a?(Array)- txs = parsed.map { |tx| tx.transform_keys { |k| k.to_s.underscore }.with_indifferent_access }- mapped = txs.map { |tx| map_transaction(tx, account_id) }- { transactions: mapped, total: mapped.length }- elsif parsed.is_a?(Hash)- if parsed[:transactions].is_a?(Array)- txs = parsed[:transactions].map { |tx| tx.transform_keys { |k| k.to_s.underscore }.with_indifferent_access }- mapped = txs.map { |tx| map_transaction(tx, account_id) }- parsed[:transactions] = mapped- parsed[:total] = parsed[:total] || mapped.length- parsed- else- # Single transaction object -> wrap and map- single = parsed.transform_keys { |k| k.to_s.underscore }.with_indifferent_access- mapped = map_transaction(single, account_id)- { transactions: [ mapped ], total: 1 }- end- else- { transactions: [], total: 0 }- end+ # POST /api/UserInstitution/CreateUserInstitution+ def create_user_institution(institution_id:, username:, password:, pin: "")+ with_provider_response do+ request(+ :post,+ "/UserInstitution/CreateUserInstitution",+ body: {+ UserID: user_id,+ InstitutionID: institution_id,+ UserName: username,+ Password: password,+ PIN: pin.to_s+ }+ )endend- # Fetches the current balance for a specific account.- #- # @param customer_id [String] Sophtron customer ID- # @param account_id [String] Sophtron account ID- # @return [Hash] Balance data with keys:- # - :balance [Hash] Balance information- # - :amount [Numeric] Current balance amount- # - :currency [String] Currency code (defaults to "USD")- # @raise [Provider::Error] if the API request fails- # @example- # result = provider.get_account_balance("cust123", "acct456")- # # => { balance: { amount: 1000.00, currency: "USD" } }- def get_account_balance(customer_id, account_id)+ # POST /api/Job/GetJobInformationByID+ def get_job_information(job_id)with_provider_response do- path = "/customers/#{ERB::Util.url_encode(customer_id.to_s)}/accounts/#{ERB::Util.url_encode(account_id.to_s)}"- url = "#{@base_url}#{path}"+ fetch_job_information(job_id)+ end+ end++ # POST /api/Job/UpdateJobSecurityAnswer+ def update_job_security_answer(job_id, answers)+ security_answer = answers.is_a?(String) ? answers : Array(answers).to_json++ with_provider_response do+ request(+ :post,+ "/Job/UpdateJobSecurityAnswer",+ body: { JobID: job_id, SecurityAnswer: security_answer }+ )+ end+ end++ # POST /api/Job/UpdateJobTokenInput+ def update_job_token_input(job_id, token_choice: nil, token_input: nil, verify_phone_flag: nil)+ with_provider_response do+ request(+ :post,+ "/Job/UpdateJobTokenInput",+ body: {+ JobID: job_id,+ TokenChoice: token_choice,+ TokenInput: token_input,+ VerifyPhoneFlag: verify_phone_flag+ }+ )+ end+ end- response = self.class.get(- url,- headers: auth_headers(url: url, http_method: "GET")+ # POST /api/Job/UpdateJobCaptcha+ def update_job_captcha(job_id, captcha_input)+ with_provider_response do+ request(+ :post,+ "/Job/UpdateJobCaptcha",+ body: { JobID: job_id, CaptchaInput: captcha_input })+ end+ end++ # POST /api/UserInstitution/GetUserInstitutionAccounts+ def get_user_institution_accounts(user_institution_id)+ with_provider_response do+ fetch_user_institution_accounts(user_institution_id)+ end+ end- parsed = handle_response(response)+ def get_accounts(user_institution_id)+ with_provider_response do+ accounts = fetch_user_institution_accounts(user_institution_id)+ normalized = accounts.map { |account| normalize_account(account, user_institution_id: user_institution_id) }+ { accounts: normalized, total: normalized.size }+ end+ end- # Normalize balance information into { balance: { amount: N, currency: "XXX" } }- # Sophtron returns balance as flat fields: Balance and BalanceCurrency (capitalized)- # After JSON symbolization these become: :Balance and :BalanceCurrency- balance_amount = parsed[:Balance] || parsed[:balance]- balance_currency = parsed[:BalanceCurrency] || parsed[:balance_currency]+ # POST /api/UserInstitutionAccount/RefreshUserInstitutionAccount+ def refresh_account(account_id)+ with_provider_response do+ request(+ :post,+ "/UserInstitutionAccount/RefreshUserInstitutionAccount",+ body: { AccountID: account_id }+ )+ end+ end- if parsed.is_a?(Hash) && balance_amount.present?- result = {- balance: {- amount: balance_amount,- currency: balance_currency.presence || "USD"- }+ # POST /api/Transaction/GetTransactionsByTransactionDate+ def get_account_transactions(account_id, start_date: nil, end_date: nil)+ with_provider_response do+ parsed = request(+ :post,+ "/Transaction/GetTransactionsByTransactionDate",+ body: {+ AccountID: account_id,+ StartDate: (start_date || 120.days.ago).to_date.to_s,+ EndDate: (end_date || Date.tomorrow).to_date.to_s}- else- result = { balance: { amount: 0, currency: "USD" } }- end- result+ )++ raw_transactions = extract_array_response(parsed, :transactions, :Transactions)+ transactions = raw_transactions.map { |transaction| normalize_transaction(transaction, account_id) }++ { transactions: transactions, total: transactions.size }endend+ def poll_job(job_id, **)+ get_job_information(job_id)+ end+private- def sophtron_auth_code(url:, http_method:)- require "base64"- require "openssl"- # sophtron auth code generation- # Parse path portion of the URL and use the last "/..." segment (matching upstream examples)- uri = URI.parse(url)- # Sign the last path segment (lowercased) and include the query string if present- path = (uri.path || "").downcase- idx = path.rindex("/")- last_seg = idx ? path[idx..-1] : path- query_str = uri.query ? "?#{uri.query.to_s.downcase}" : ""- auth_path = "#{last_seg}#{query_str}"- # Build the plain text to sign: "METHOD\n/auth_path"- plain_key = "#{http_method.to_s.upcase}\n#{auth_path}"- # Decode the base64 access key and compute HMAC-SHA256- begin- key_bytes = Base64.decode64(@access_key.to_s)- rescue => decode_err- Rails.logger.error("[sophtron_auth_code] Failed to decode access_key: #{decode_err.class}: #{decode_err.message}")- raise+ def default_error_transformer(error)+ return error if error.is_a?(Error)++ super+ end++ def fetch_job_information(job_id)+ request(+ :post,+ "/Job/GetJobInformationByID",+ body: { JobID: job_id }+ )+ end++ def fetch_user_institution_accounts(user_institution_id)+ parsed = request(+ :post,+ "/UserInstitution/GetUserInstitutionAccounts",+ body: { UserInstitutionID: user_institution_id }+ )+ extract_array_response(parsed, :accounts, :Accounts)+ end++ def extract_array_response(parsed, *keys)+ return parsed if parsed.is_a?(Array)+ return [] if parsed.respond_to?(:empty?) && parsed.empty?++ if parsed.respond_to?(:with_indifferent_access)+ parsed = parsed.with_indifferent_access+ keys.each do |key|+ return Array(parsed[key]) if parsed.key?(key)+ endend- signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), key_bytes, plain_key)- sig_b64_str = Base64.strict_encode64(signature)- auth_code = "FIApiAUTH:#{@user_id}:#{sig_b64_str}:#{auth_path}"- auth_code++ raise Error.new("Invalid Sophtron response format", :invalid_response, details: parsed)end- def auth_headers(url:, http_method:)+ def request(method, api_path, body: nil, parse_json: true)+ options = { headers: auth_headers(method: method, api_path: api_path) }+ options[:body] = body.to_json if body++ response = self.class.public_send(method, "#{base_url}#{api_path}", options)+ handle_response(response, parse_json: parse_json)+ rescue Error+ raise+ rescue SocketError, Net::OpenTimeout, Net::ReadTimeout => e+ raise Error.new("Sophtron request failed: #{e.message}", :request_failed)+ rescue StandardError => e+ raise Error.new("Sophtron request failed: #{e.message}", :request_failed)+ end++ def auth_headers(method:, api_path:){- "Authorization" => sophtron_auth_code(url: url, http_method: http_method),+ "Authorization" => auth_header_for(method, api_path),"Content-Type" => "application/json","Accept" => "application/json"}end- # Fetch list of customer IDs by calling GET /customers and extracting identifier fields- def get_customer_ids- url = "#{@base_url}/customers"- response = self.class.get(- url,- headers: auth_headers(url: url, http_method: "GET")- )- parsed = handle_response(response)- ids = []- if parsed.is_a?(Array)- ids = parsed.map do |r|- next unless r.is_a?(Hash)- # Find a key that likely contains the customer id (handles :CustomerID, :customerID, :customer_id, :ID, :id)- key = r.keys.find { |k| k.to_s.downcase.include?("customer") && k.to_s.downcase.include?("id") } ||- r.keys.find { |k| k.to_s.downcase == "id" }- r[key]- end.compact- elsif parsed.is_a?(Hash)- if parsed[:customers].is_a?(Array)- ids = parsed[:customers].map do |r|- next unless r.is_a?(Hash)- key = r.keys.find { |k| k.to_s.downcase.include?("customer") && k.to_s.downcase.include?("id") } ||- r.keys.find { |k| k.to_s.downcase == "id" }- r[key]- end.compact- else- key = parsed.keys.find { |k| k.to_s.downcase.include?("customer") && k.to_s.downcase.include?("id") } ||- parsed.keys.find { |k| k.to_s.downcase == "id" }- ids = [ parsed[key] ].compact- end+ def handle_response(response, parse_json: true)+ body = response.body.to_s++ case response.code.to_i+ when 200, 201, 204+ return {} if body.strip.blank?++ parse_json ? JSON.parse(body, symbolize_names: true) : parse_optional_json(body)+ when 400+ raise Error.new("Bad request to Sophtron API: #{body}", :bad_request, details: body)+ when 401+ raise Error.new("Invalid Sophtron User ID or Access Key", :unauthorized, details: body)+ when 403+ raise Error.new("Access forbidden by Sophtron", :access_forbidden, details: body)+ when 404+ raise Error.new("Sophtron resource not found", :not_found, details: body)+ when 429+ raise Error.new("Sophtron rate limit exceeded. Please try again later.", :rate_limited, details: body)+ else+ raise Error.new(+ "Sophtron API request failed: #{response.code} #{response.message} - #{body}",+ :fetch_failed,+ details: body+ )end+ rescue JSON::ParserError => e+ raise Error.new("Invalid JSON response from Sophtron API: #{e.message}", :invalid_response, details: body)+ end- # Normalize to strings and unique (avoid destructive methods that may return nil)- ids = ids.map(&:to_s).compact.uniq- ids+ def parse_optional_json(body)+ JSON.parse(body, symbolize_names: true)+ rescue JSON::ParserError+ bodyend- # Fetch accounts for a specific customer via GET /customers/:customer_id/accounts- def get_customer_accounts(customer_id)- path = "/customers/#{ERB::Util.url_encode(customer_id.to_s)}/accounts"- url = "#{@base_url}#{path}"- response = self.class.get(- url,- headers: auth_headers(url: url, http_method: "GET")- )- handle_response(response)+ def normalize_base_url(value)+ url = value.presence || DEFAULT_BASE_URL+ url = url.to_s.chomp("/")+ url = url.delete_suffix("/v2") if url.end_with?("/v2")++ parsed = URI.parse(url)+ parsed.path.to_s.end_with?("/api") ? url : "#{url}/api"+ rescue URI::InvalidURIError+ DEFAULT_BASE_URL+ end++ def normalize_account(account, user_institution_id:)+ account = account.with_indifferent_access+ account_id = first_present(account, :AccountID, :account_id, :id)+ account_name = first_present(account, :AccountName, :account_name, :name)+ account_number = first_present(account, :AccountNumber, :account_number)+ currency = first_present(account, :BalanceCurrency, :balance_currency, :Currency, :currency).presence || "USD"++ {+ id: account_id,+ account_id: account_id,+ account_name: account_name,+ name: account_name,+ account_type: first_present(account, :AccountType, :account_type, :type).presence || "unknown",+ sub_type: first_present(account, :AccountSubType, :account_sub_type, :SubType, :sub_type).presence || "unknown",+ balance: first_present(account, :AccountBalance, :account_balance, :Balance, :balance),+ balance_currency: currency,+ currency: currency,+ account_number_mask: mask_account_number(account_number),+ status: first_present(account, :AccountStatus, :account_status, :Status, :status).presence || "active",+ user_institution_id: user_institution_id,+ institution_name: first_present(account, :InstitutionName, :institution_name),+ raw_payload: account.to_h+ }.with_indifferent_accessend- # Map a normalized Sophtron transaction hash into our standard transaction shape- # Returns: { id, accountId, type, status, amount, currency, date, merchant, description }- def map_transaction(tx, account_id)- tx = tx.with_indifferent_access+ def normalize_transaction(transaction, account_id)+ transaction = transaction.with_indifferent_access+{- id: tx[:transaction_id],+ id: first_present(transaction, :TransactionID, :TransactionId, :transaction_id, :transactionId, :ID, :id),accountId: account_id,- type: tx[:type] || "unknown",- status: tx[:status] || "completed",- amount: tx[:amount] || 0.0,- currency: tx[:currency] || "USD",- date: tx[:transaction_date] || nil,- merchant: tx[:merchant] || extract_merchant(tx[:description]) ||"",- description: tx[:description] || ""+ type: first_present(transaction, :Type, :type).presence || "unknown",+ status: first_present(transaction, :Status, :status).presence || "completed",+ amount: first_present(transaction, :Amount, :amount).presence || 0,+ currency: first_present(transaction, :Currency, :currency).presence || "USD",+ date: first_present(transaction, :TransactionDate, :transaction_date, :Date, :date),+ merchant: first_present(transaction, :Merchant, :merchant).presence || extract_merchant(first_present(transaction, :Description, :description)).presence || "",+ description: first_present(transaction, :Description, :description).presence || ""}.with_indifferent_accessend+ def first_present(hash, *keys)+ keys.each do |key|+ value = hash[key]+ return value if value.present?+ end++ nil+ end++ def mask_account_number(account_number)+ return nil if account_number.blank?++ last_four = account_number.to_s.gsub(/\s+/, "").last(4)+ last_four.present? ? "****#{last_four}" : nil+ end+def extract_merchant(line)return nil if line.nil?- line = line.strip++ line = line.to_s.stripreturn nil if line.empty?- # 1. Handle special bank fees and automated transactionsif line =~ /INSUFFICIENT FUNDS FEE/i- return "Bank Fee: Insufficient Funds"+ "Bank Fee: Insufficient Funds"elsif line =~ /OVERDRAFT PROTECTION/i- return "Bank Transfer: Overdraft Protection"+ "Bank Transfer: Overdraft Protection"elsif line =~ /AUTO PAY WF HOME MTG/i- return "Wells Fargo Home Mortgage"+ "Wells Fargo Home Mortgage"elsif line =~ /PAYDAY LOAN/i- return "Payday Loan"- end-- # 2. Refined CHECKCARD Pattern- # Logic:- # - Start after 'CHECKCARD XXXX '- # - Capture everything (.+?)- # - STOP when we see:- # a) Two or more spaces (\s{2,})- # b) A masked number (x{3,})- # c) A pattern of [One Word] + [Space] + [State Code] (\s+\S+\s+[A-Z]{2}\b)- # The (\s+\S+) part matches the city, so we stop BEFORE it.- if line =~ /CHECKCARD \d{4}\s+(.+?)(?=\s{2,}|x{3,}|\s+\S+\s+[A-Z]{2}\b)/i- return $1.strip- end-- # 3. Handle standard purchase rows (e.g., EXXONMOBIL POS 12/08)- # Stops before date (MM/DD) or hash (#)- if line =~ /^(.+?)(?=\s+\d{2}\/\d{2}|\s+#)/- name = $1.strip- return name.gsub(/\s+POS$/i, "").strip- end-- # 4. Fallback for other formats- line[0..25].strip- end-- def handle_response(response)- case response.code- when 200- begin- JSON.parse(response.body, symbolize_names: true)- rescue JSON::ParserError => e- Rails.logger.error "Sophtron API: Invalid JSON response - #{e.message}"- raise Provider::Error.new("Invalid JSON response from Sophtron API", :invalid_response)- end- when 400- Rails.logger.error "Sophtron API: Bad request - #{response.body}"- raise Provider::Error.new("Bad request to Sophtron API: #{response.body}", :bad_request)- when 401- raise Provider::Error.new("Invalid User ID or Access key", :unauthorized)- when 403- raise Provider::Error.new("Access forbidden - check your User ID and Access key permissions", :access_forbidden)- when 404- raise Provider::Error.new("Resource not found", :not_found)- when 429- raise Provider::Error.new("Rate limit exceeded. Please try again later.", :rate_limited)+ "Payday Loan"+ elsif line =~ /CHECKCARD \d{4}\s+(.+?)(?=\s{2,}|x{3,}|\s+\S+\s+[A-Z]{2}\b)/i+ Regexp.last_match(1).strip+ elsif line =~ /^(.+?)(?=\s+\d{2}\/\d{2}|\s+#)/+ Regexp.last_match(1).strip.gsub(/\s+POS$/i, "").stripelse- Rails.logger.error "Sophtron API: Unexpected response - Code: #{response.code}, Body: #{response.body}"- raise Provider::Error.new("Failed to fetch data: #{response.code} #{response.message} - #{response.body}", :fetch_failed)+ line[0..25].stripendendenddiff --git a/app/models/provider/sophtron_adapter.rb b/app/models/provider/sophtron_adapter.rbindex 155c6bb2cd..144bdd4826 100644--- a/app/models/provider/sophtron_adapter.rb+++ b/app/models/provider/sophtron_adapter.rb@@ -22,7 +22,8 @@ class Provider::SophtronAdapter < Provider::Basenew_account_path: ->(accountable_type, return_to) {Rails.application.routes.url_helpers.select_accounts_sophtron_items_path(accountable_type: accountable_type,- return_to: return_to+ return_to: return_to,+ connect_new_institution: true)},existing_account_path: ->(account_id) {@@ -45,7 +46,7 @@ class Provider::SophtronAdapter < Provider::Basereturn nil unless family.present?# Get family-specific credentials- sophtron_item = family.sophtron_items.where.not(user_id: nil, access_key: nil).first+ sophtron_item = family.configured_sophtron_itemreturn nil unless sophtron_item&.credentials_configured?Provider::Sophtron.new(@@ -88,10 +89,16 @@ class Provider::SophtronAdapter < Provider::Baseenddef institution_name- metadata = provider_account.institution_metadata+ metadata = provider_account.institution_metadata || {}return nil unless metadata.present?- metadata["name"] || item&.institution_name+ metadata_name = metadata["name"].presence || metadata["institution_name"].presence+ return metadata_name if metadata_name.present?++ metadata_user_institution_id = metadata["user_institution_id"].presence || metadata["UserInstitutionID"].presence+ return item&.institution_name if metadata_user_institution_id.present? && metadata_user_institution_id == item&.user_institution_id++ nilenddef institution_urldiff --git a/app/models/sophtron_account.rb b/app/models/sophtron_account.rbindex f61af0eab4..d6f111b3a6 100644--- a/app/models/sophtron_account.rb+++ b/app/models/sophtron_account.rb@@ -46,20 +46,37 @@ class SophtronAccount < ApplicationRecorddef upsert_sophtron_snapshot!(account_snapshot)# Convert to symbol keys or handle both string and symbol keyssnapshot = account_snapshot.with_indifferent_access+ account_id = first_present(snapshot, :account_id, :id, :AccountID)+ account_name = first_present(snapshot, :account_name, :name, :AccountName)+ account_number = first_present(snapshot, :account_number, :AccountNumber)+ currency = first_present(snapshot, :balance_currency, :currency, :BalanceCurrency, :Currency)+ balance = first_present(snapshot, :balance, :account_balance, :AccountBalance, :Balance)+ available_balance = first_present(snapshot, :"available-balance", :available_balance, :AvailableBalance)+ account_type = first_present(snapshot, :account_type, :type, :AccountType)+ account_sub_type = first_present(snapshot, :sub_type, :account_sub_type, :AccountSubType, :SubType)+ last_updated = first_present(snapshot, :last_updated, :LastUpdated)+ institution_name = first_present(snapshot, :institution_name, :InstitutionName).presence || sophtron_item&.institution_name+ user_institution_id = first_present(snapshot, :user_institution_id, :UserInstitutionID).presence || sophtron_item&.user_institution_id# Map Sophtron field names to our field namesassign_attributes(- name: snapshot[:account_name],- account_id: snapshot[:account_id],- currency: parse_currency(snapshot[:balance_currency]) || "USD",- balance: parse_balance(snapshot[:balance]),- available_balance: parse_balance(snapshot[:"available-balance"]),- account_type: snapshot["account_type"] || "unknown",- account_sub_type: snapshot["sub_type"] || "unknown",- last_updated: parse_balance_date(snapshot[:"last_updated"]),+ name: account_name,+ account_id: account_id,+ currency: parse_currency(currency) || "USD",+ balance: parse_balance(balance),+ available_balance: parse_balance(available_balance),+ account_type: account_type.presence || "unknown",+ account_sub_type: account_sub_type.presence || "unknown",+ last_updated: parse_balance_date(last_updated),+ account_status: first_present(snapshot, :account_status, :status, :AccountStatus, :Status),+ account_number_mask: snapshot[:account_number_mask].presence || mask_account_number(account_number),+ institution_metadata: {+ name: institution_name,+ user_institution_id: user_institution_id+ }.compact,raw_payload: account_snapshot,- customer_id: snapshot["customer_id"],- member_id: snapshot["member_id"]+ customer_id: first_present(snapshot, :customer_id, :CustomerID) || customer_id,+ member_id: first_present(snapshot, :member_id, :MemberID) || member_id)save!@@ -127,4 +144,20 @@ class SophtronAccount < ApplicationRecordreturn if balance.present? || available_balance.present?errors.add(:base, "Sophtron account must have either current or available balance")end++ def first_present(hash, *keys)+ keys.each do |key|+ value = hash[key]+ return value if value.present?+ end++ nil+ end++ def mask_account_number(account_number)+ return nil if account_number.blank?++ last_four = account_number.to_s.gsub(/\s+/, "").last(4)+ last_four.present? ? "****#{last_four}" : nil+ endenddiff --git a/app/models/sophtron_item.rb b/app/models/sophtron_item.rbindex 52a5f3cb75..dc278fe65e 100644--- a/app/models/sophtron_item.rb+++ b/app/models/sophtron_item.rb@@ -14,6 +14,9 @@class SophtronItem < ApplicationRecordinclude Syncable, Provided, Unlinking+ INITIAL_LOAD_LOOKBACK_DAYS = 120+ MAX_TRANSACTION_HISTORY_YEARS = 3+enum :status, { good: "good", requires_update: "requires_update" }, default: :good# Helper to detect if ActiveRecord Encryption is configured for this app.@@ -66,15 +69,15 @@ class SophtronItem < ApplicationRecord## @return [Hash] Import results with counts of accounts and transactions imported# @raise [StandardError] if the Sophtron provider is not configured- # @raise [Provider::Error] if the Sophtron API returns an error- def import_latest_sophtron_data+ # @raise [Provider::Sophtron::Error] if the Sophtron API returns an error+ def import_latest_sophtron_data(sync: nil)provider = sophtron_providerunless providerRails.logger.error "SophtronItem #{id} - Cannot import: Sophtron provider is not configured (missing API key)"raise StandardError.new("Sophtron provider is not configured")end- SophtronItem::Importer.new(self, sophtron_provider: provider).import+ SophtronItem::Importer.new(self, sophtron_provider: provider, sync: sync).importrescue => eRails.logger.error "SophtronItem #{id} - Failed to import data: #{e.message}"raise@@ -122,6 +125,24 @@ class SophtronItem < ApplicationRecordresultsend+ def start_initial_load_later+ active_sync = syncs.visible.ordered.first++ sync_later(window_start_date: initial_load_window_start_date)++ return unless active_sync&.reload&.syncing?++ SophtronInitialLoadJob.set(wait: SophtronInitialLoadJob::RETRY_DELAY).perform_later(self)+ end++ def initial_load_window_start_date+ configured_start = sync_start_date&.to_date+ default_start = INITIAL_LOAD_LOOKBACK_DAYS.days.ago.to_date+ max_history_start = MAX_TRANSACTION_HISTORY_YEARS.years.ago.to_date++ [ configured_start || default_start, max_history_start ].max+ end+def upsert_sophtron_snapshot!(accounts_snapshot)assign_attributes(raw_payload: accounts_snapshot@@ -130,6 +151,118 @@ class SophtronItem < ApplicationRecordsave!end+ def ensure_customer!(provider: sophtron_provider)+ return customer_id if customer_id.present?+ raise Provider::Sophtron::Error.new("Sophtron provider is not configured", :configuration_error) unless provider++ matching_customer = find_matching_customer(Provider::Sophtron.response_data!(provider.list_customers))+ customer_payload = matching_customer || Provider::Sophtron.response_data!(+ provider.create_customer(+ unique_id: generated_customer_unique_id,+ name: generated_customer_name,+ source: "Sure"+ )+ )++ # Some Sophtron endpoints may return an empty body on success; re-list to find+ # the customer we just created if the create response does not include an id.+ if extract_customer_id(customer_payload).blank?+ customer_payload = find_matching_customer(Provider::Sophtron.response_data!(provider.list_customers))+ end++ extracted_customer_id = extract_customer_id(customer_payload)+ raise Provider::Sophtron::Error.new("Sophtron customer response did not include CustomerID", :invalid_response) if extracted_customer_id.blank?++ update!(+ customer_id: extracted_customer_id,+ customer_name: extract_customer_name(customer_payload).presence || generated_customer_name,+ raw_customer_payload: customer_payload+ )++ customer_id+ end++ def connected_to_institution?+ user_institution_id.present? && current_job_id.blank? && good? && !failed_connection_job?+ end++ def failed_connection_job?+ payload = raw_job_payload || {}+ payload = payload.with_indifferent_access if payload.respond_to?(:with_indifferent_access)++ success_flag = if payload.respond_to?(:key?) && payload.key?(:SuccessFlag)+ payload[:SuccessFlag]+ elsif payload.respond_to?(:key?)+ payload[:success_flag]+ end++ last_status = job_status.presence ||+ (payload[:LastStatus] if payload.respond_to?(:[])) ||+ (payload[:last_status] if payload.respond_to?(:[]))++ success_flag == false && Provider::Sophtron.failure_job_status?(last_status)+ end++ def upsert_job_snapshot!(job_payload)+ job_payload = job_payload.with_indifferent_access++ update!(+ job_status: job_payload[:LastStatus] || job_payload[:last_status],+ raw_job_payload: job_payload+ )+ end++ def fetch_remote_accounts(force: false)+ cache_key = "sophtron_accounts_#{family.id}_#{id}_#{user_institution_id}"+ cached = Rails.cache.read(cache_key)+ return cached if cached.present? && !force++ accounts_data = Provider::Sophtron.response_data!(sophtron_provider.get_accounts(user_institution_id))+ accounts = accounts_data[:accounts] || []+ Rails.cache.write(cache_key, accounts, expires_in: 5.minutes)+ persist_remote_sophtron_accounts(accounts)+ accounts+ end++ def persist_remote_sophtron_accounts(accounts)+ Array(accounts).each do |account_data|+ account_data = account_data.with_indifferent_access+ next if account_data[:account_name].blank?++ upsert_sophtron_account(account_data)+ rescue ActiveRecord::RecordInvalid => e+ Rails.logger.warn("Skipping Sophtron account #{self.class.external_account_id(account_data)}: #{e.message}")+ end+ end++ def reject_already_linked(accounts)+ linked_account_ids = sophtron_accounts.joins(:account_provider).pluck(:account_id).map(&:to_s)+ Array(accounts).reject { |account| linked_account_ids.include?(self.class.external_account_id(account).to_s) }+ end++ def upsert_sophtron_account(account_data)+ sophtron_accounts.find_or_initialize_by(+ account_id: self.class.external_account_id(account_data).to_s+ ).tap do |sophtron_account|+ sophtron_account.upsert_sophtron_snapshot!(account_data)+ end+ end++ def build_mfa_challenge(job)+ job = job.with_indifferent_access+ {+ security_questions: Provider::Sophtron.parse_json_array(job[:SecurityQuestion] || job[:security_question]),+ token_methods: Provider::Sophtron.parse_json_array(job[:TokenMethod] || job[:token_method]),+ token_sent: Provider::Sophtron.job_token_input_required?(job),+ token_read: job[:TokenRead] || job[:token_read],+ captcha_image: job[:CaptchaImage] || job[:captcha_image]+ }+ end++ def self.external_account_id(account_data)+ account_data.with_indifferent_access[:account_id] || account_data.with_indifferent_access[:id]+ end+def has_completed_initial_setup?# Setup is complete if we have any linked accountsaccounts.any?@@ -167,6 +300,10 @@ class SophtronItem < ApplicationRecordinstitution_name.presence || institution_domain.presence || nameend+ def provider_display_name+ I18n.t("sophtron_items.defaults.name", default: "Sophtron Connection")+ end+def connected_institutions# Get unique institutions from all accountssophtron_accounts.includes(:account)@@ -193,6 +330,40 @@ class SophtronItem < ApplicationRecordenddef effective_base_url- base_url.presence || "https://api.sophtron.com/api/v2"+ base_url.presence || Provider::Sophtron::DEFAULT_BASE_URL+ end++ def generated_customer_unique_id+ "sure-family-#{family.id}"+ end++ def generated_customer_name+ "Sure family #{family.id}"end++ private++ def find_matching_customer(customers)+ customers = Array(customers)++ customers.find do |customer|+ extract_customer_id(customer).to_s == generated_customer_unique_id+ end || customers.find do |customer|+ extract_customer_name(customer).to_s == generated_customer_name+ end+ end++ def extract_customer_id(customer_payload)+ return nil unless customer_payload.respond_to?(:with_indifferent_access)++ customer_payload = customer_payload.with_indifferent_access+ customer_payload[:CustomerID] || customer_payload[:customer_id] || customer_payload[:id]+ end++ def extract_customer_name(customer_payload)+ return nil unless customer_payload.respond_to?(:with_indifferent_access)++ customer_payload = customer_payload.with_indifferent_access+ customer_payload[:CustomerName] || customer_payload[:customer_name] || customer_payload[:name]+ endenddiff --git a/app/models/sophtron_item/importer.rb b/app/models/sophtron_item/importer.rbindex dabf0f316e..7a58308e67 100644--- a/app/models/sophtron_item/importer.rb+++ b/app/models/sophtron_item/importer.rb@@ -14,15 +14,19 @@ require "set"# explicitly connected to Maybe Accounts). This allows users to selectively# import accounts of their choosing.class SophtronItem::Importer- attr_reader :sophtron_item, :sophtron_provider+ INCREMENTAL_SYNC_BUFFER_DAYS = 60++ attr_reader :sophtron_item, :sophtron_provider, :sync# Initializes a new importer.## @param sophtron_item [SophtronItem] The Sophtron item to import data for# @param sophtron_provider [Provider::Sophtron] Configured Sophtron API client- def initialize(sophtron_item, sophtron_provider:)+ # @param sync [Sync, nil] Optional sync record whose window should guide import scope+ def initialize(sophtron_item, sophtron_provider:, sync: nil)@sophtron_item = sophtron_item@sophtron_provider = sophtron_provider+ @sync = syncend# Performs the complete import process for this Sophtron item.@@ -48,6 +52,25 @@ class SophtronItem::Importer# # accounts_failed: 0, transactions_imported: 150, transactions_failed: 0 }def importRails.logger.info "SophtronItem::Importer - Starting import for item #{sophtron_item.id}"+ unless sophtron_item.user_institution_id.present?+ error_message = "Sophtron institution connection is incomplete"+ Rails.logger.warn "SophtronItem::Importer - Item #{sophtron_item.id} has no Sophtron UserInstitutionID"+ sophtron_item.update!(+ status: :requires_update,+ last_connection_error: error_message+ )++ return {+ success: false,+ error: error_message,+ accounts_updated: 0,+ accounts_created: 0,+ accounts_failed: 0,+ transactions_imported: 0,+ transactions_failed: 0+ }+ end+# Step 1: Fetch all accounts from Sophtronaccounts_data = fetch_accounts_dataunless accounts_data@@ -124,6 +147,7 @@ class SophtronItem::Importertransactions_imported += result[:transactions_count]elsetransactions_failed += 1+ break if result[:requires_update]endrescue => etransactions_failed += 1@@ -144,16 +168,16 @@ class SophtronItem::Importer}end+ def import_transactions_after_refresh(sophtron_account)+ fetch_and_store_transactions(sophtron_account, refresh: false)+ end+privatedef fetch_accounts_databegin- accounts_data = sophtron_provider.get_accounts- # Extract data from Provider::Response object if needed- if accounts_data.respond_to?(:data)- accounts_data = accounts_data.data- end- rescue Provider::Error => e+ accounts_data = Provider::Sophtron.response_data!(sophtron_provider.get_accounts(sophtron_item.user_institution_id))+ rescue Provider::Sophtron::Error => e# Handle authentication errors by marking item as requiring updateif e.error_type == :unauthorized || e.error_type == :access_forbiddenbegin@@ -245,34 +269,41 @@ class SophtronItem::Importer# - :success [Boolean] Whether the fetch was successful# - :transactions_count [Integer] Number of transactions fetched# - :error [String, nil] Error message if failed- def fetch_and_store_transactions(sophtron_account)+ def fetch_and_store_transactions(sophtron_account, refresh: true)start_date = determine_sync_start_date(sophtron_account)Rails.logger.info "SophtronItem::Importer - Fetching transactions for account #{sophtron_account.account_id} from #{start_date}"begin+ if refresh && !initial_transaction_fetch?(sophtron_account)+ refresh_result = refresh_account_before_transaction_fetch(sophtron_account)+ return refresh_result if refresh_result.present?+ end+# Fetch transactions- transactions_data = sophtron_provider.get_account_transactions(- sophtron_account.customer_id,- sophtron_account.account_id,- start_date: start_date+ transactions_data = Provider::Sophtron.response_data!(+ sophtron_provider.get_account_transactions(+ sophtron_account.account_id,+ start_date: start_date+ ))- # Extract data from Provider::Response object if needed- if transactions_data.respond_to?(:data)- transactions_data = transactions_data.data- end-# Validate response structureunless transactions_data.is_a?(Hash)Rails.logger.error "SophtronItem::Importer - Invalid transactions_data format for account #{sophtron_account.account_id}"return { success: false, transactions_count: 0, error: "Invalid response format" }end- transactions_count = transactions_data[:transactions]&.count || 0+ transactions = transactions_data[:transactions]+ unless transactions.is_a?(Array)+ Rails.logger.error "SophtronItem::Importer - Missing transactions array for account #{sophtron_account.account_id}"+ return { success: false, transactions_count: 0, error: "Missing transactions array" }+ end++ transactions_count = transactions.countRails.logger.info "SophtronItem::Importer - Fetched #{transactions_count} transactions for account #{sophtron_account.account_id}"# Store transactions in the account- if transactions_data[:transactions].present?+ if transactions.any?beginexisting_transactions = sophtron_account.raw_transactions_payload.to_a@@ -283,7 +314,7 @@ class SophtronItem::Importer# Filter to ONLY truly new transactions (skip duplicates)# Transactions are immutable on the bank side, so we don't need to update them- new_transactions = transactions_data[:transactions].select do |tx|+ new_transactions = transactions.select do |tx|next false unless tx.is_a?(Hash)tx_id = tx.with_indifferent_access[:id]@@ -291,10 +322,11 @@ class SophtronItem::Importerendif new_transactions.any?- Rails.logger.info "SophtronItem::Importer - Storing #{new_transactions.count} new transactions (#{existing_transactions.count} existing, #{transactions_data[:transactions].count - new_transactions.count} duplicates skipped) for account #{sophtron_account.account_id}"+ Rails.logger.info "SophtronItem::Importer - Storing #{new_transactions.count} new transactions (#{existing_transactions.count} existing, #{transactions.count - new_transactions.count} duplicates skipped) for account #{sophtron_account.account_id}"sophtron_account.upsert_sophtron_transactions_snapshot!(existing_transactions + new_transactions)else- Rails.logger.info "SophtronItem::Importer - No new transactions to store (all #{transactions_data[:transactions].count} were duplicates) for account #{sophtron_account.account_id}"+ Rails.logger.info "SophtronItem::Importer - No new transactions to store (all #{transactions.count} were duplicates) for account #{sophtron_account.account_id}"+ sophtron_account.upsert_sophtron_transactions_snapshot!(existing_transactions) if sophtron_account.raw_transactions_payload.nil?endrescue => eRails.logger.error "SophtronItem::Importer - Failed to store transactions for account #{sophtron_account.account_id}: #{e.message}"@@ -302,20 +334,15 @@ class SophtronItem::ImporterendelseRails.logger.info "SophtronItem::Importer - No transactions to store for account #{sophtron_account.account_id}"- end-- # Fetch and update balance- begin- fetch_and_update_balance(sophtron_account)- rescue => e- # Log but don't fail transaction import if balance fetch fails- Rails.logger.warn "SophtronItem::Importer - Failed to update balance for account #{sophtron_account.account_id}: #{e.message}"+ sophtron_account.upsert_sophtron_transactions_snapshot!([]) if sophtron_account.raw_transactions_payload.nil?end{ success: true, transactions_count: transactions_count }- rescue Provider::Error => e+ rescue Provider::Sophtron::Error => e+ requires_update = e.error_type.in?([ :unauthorized, :access_forbidden ])+ sophtron_item.update!(status: :requires_update) if requires_updateRails.logger.error "SophtronItem::Importer - Sophtron API error for account #{sophtron_account.id}: #{e.message}"- { success: false, transactions_count: 0, error: e.message }+ { success: false, transactions_count: 0, error: e.message, requires_update: requires_update }rescue JSON::ParserError => eRails.logger.error "SophtronItem::Importer - Failed to parse transaction response for account #{sophtron_account.id}: #{e.message}"{ success: false, transactions_count: 0, error: "Failed to parse response" }@@ -326,93 +353,80 @@ class SophtronItem::Importerendend- def fetch_and_update_balance(sophtron_account)- begin- balance_data = sophtron_provider.get_account_balance(sophtron_account.customer_id, sophtron_account.account_id)- # Extract data from Provider::Response object if needed- if balance_data.respond_to?(:data)- balance_data = balance_data.data- end+ def refresh_account_before_transaction_fetch(sophtron_account)+ refresh_response = Provider::Sophtron.response_data!(sophtron_provider.refresh_account(sophtron_account.account_id))+ job_id = refresh_response.with_indifferent_access[:JobID] || refresh_response.with_indifferent_access[:job_id]+ return nil if job_id.blank?- # Validate response structure- unless balance_data.is_a?(Hash)- Rails.logger.error "SophtronItem::Importer - Invalid balance_data format for account #{sophtron_account.account_id}"- return- end+ job = Provider::Sophtron.response_data!(sophtron_provider.get_job_information(job_id))+ sophtron_item.upsert_job_snapshot!(job)- if balance_data[:balance].present?- balance_info = balance_data[:balance]+ if Provider::Sophtron.job_requires_input?(job)+ sophtron_item.update!(+ status: :requires_update,+ current_job_id: job_id,+ last_connection_error: "Sophtron refresh requires MFA"+ )+ return { success: false, transactions_count: 0, error: "Sophtron refresh requires MFA", requires_update: true }+ end- # Validate balance info structure- unless balance_info.is_a?(Hash)- Rails.logger.error "SophtronItem::Importer - Invalid balance info format for account #{sophtron_account.account_id}"- return- end+ if Provider::Sophtron.job_failed?(job)+ return { success: false, transactions_count: 0, error: "Sophtron refresh failed" }+ end- # Only update if we have a valid amount- if balance_info[:amount].present?- sophtron_account.update!(- balance: balance_info[:amount],- currency: balance_info[:currency].presence || sophtron_account.currency- )- else- Rails.logger.warn "SophtronItem::Importer - No amount in balance data for account #{sophtron_account.account_id}"- end- else- Rails.logger.warn "SophtronItem::Importer - No balance data returned for account #{sophtron_account.account_id}"- end- rescue Provider::Error => e- Rails.logger.error "SophtronItem::Importer - Sophtron API error fetching balance for account #{sophtron_account.id}: #{e.message}"- # Don't fail if balance fetch fails- rescue ActiveRecord::RecordInvalid => e- Rails.logger.error "SophtronItem::Importer - Failed to save balance for account #{sophtron_account.id}: #{e.message}"- # Don't fail if balance save fails- rescue => e- Rails.logger.error "SophtronItem::Importer - Unexpected error updating balance for account #{sophtron_account.id}: #{e.class} - #{e.message}"- # Don't fail if balance update fails+ unless Provider::Sophtron.job_success?(job) || Provider::Sophtron.job_completed?(job)+ SophtronRefreshPollJob.set(wait: SophtronRefreshPollJob::POLL_INTERVAL).perform_later(+ sophtron_account,+ job_id: job_id,+ sync: sync+ )++ return { success: true, transactions_count: 0, refresh_pending: true }end++ nil+ rescue Provider::Sophtron::Error => e+ requires_update = e.error_type.in?([ :unauthorized, :access_forbidden ])+ sophtron_item.update!(status: :requires_update) if requires_update+ Rails.logger.error "SophtronItem::Importer - Sophtron API error refreshing account #{sophtron_account.id}: #{e.message}"+ { success: false, transactions_count: 0, error: e.message, requires_update: requires_update }end# Determines the appropriate start date for fetching transactions.## Logic:- # - For accounts with stored transactions: uses last sync date minus 60-day buffer- # - For new accounts: uses account creation date minus 60 days, capped at 120 days ago+ # - For accounts with stored transactions: uses last sync date minus a buffer+ # - For new accounts: uses the sync window or provider default initial lookback#- # This ensures we capture any late-arriving transactions while limiting- # the historical window for new accounts.+ # This captures late-arriving transactions while keeping history bounded.## @param sophtron_account [SophtronAccount] The account to determine start date for# @return [Date] The start date for transaction syncdef determine_sync_start_date(sophtron_account)- configured_start = sophtron_item.sync_start_date&.to_time- max_history_start = 3.years.ago- floor_start = [ configured_start, max_history_start ].compact.max- # Check if this account has any stored transactions- # If not, treat it as a first sync for this account even if the item has been synced before- has_stored_transactions = sophtron_account.raw_transactions_payload.to_a.any?-- if has_stored_transactions+ configured_start = sync&.window_start_date || sophtron_item.sync_start_date&.to_date+ max_history_start = SophtronItem::MAX_TRANSACTION_HISTORY_YEARS.years.ago.to_date+ floor_start = configured_start ? [ configured_start, max_history_start ].max : nil++ if !initial_transaction_fetch?(sophtron_account)# Account has been synced before, use item-level logic with buffer# For subsequent syncs, fetch from last sync date with a bufferif sophtron_item.last_synced_at- [ sophtron_item.last_synced_at - 60.days, floor_start ].compact.max+ [ sophtron_item.last_synced_at.to_date - INCREMENTAL_SYNC_BUFFER_DAYS, floor_start ].compact.maxelse# Fallback if item hasn't been synced but account has transactions- floor_start || 120.days.ago+ floor_start || sophtron_item.initial_load_window_start_dateendelse# Account has no stored transactions - this is a first sync for this account- # Use account creation date or a generous historical window- account_baseline = sophtron_account.created_at || Time.current- first_sync_window = [ account_baseline - 60.days, floor_start || 120.days.ago ].max-- # Use the more recent of: (account created - 60 days) or (120 days ago)- # This caps old accounts at 120 days while respecting recent account creation dates- first_sync_window+ # Use the configured sync window if present, otherwise the provider's default initial lookback.+ floor_start || sophtron_item.initial_load_window_start_dateendend+ def initial_transaction_fetch?(sophtron_account)+ sophtron_account.raw_transactions_payload.nil? && sophtron_item.last_synced_at.blank?+ end+# Handles API errors and marks the item for re-authentication if needed.## Authentication-related errors cause the item status to be set to@@ -420,7 +434,7 @@ class SophtronItem::Importer## @param error_message [String] The error message from the API# @return [void]- # @raise [Provider::Error] Always raises an error with the message+ # @raise [Provider::Sophtron::Error] Always raises an error with the messagedef handle_error(error_message)# Mark item as requiring update for authentication-related errorserror_msg_lower = error_message.to_s.downcase@@ -438,7 +452,7 @@ class SophtronItem::ImporterendRails.logger.error "SophtronItem::Importer - API error: #{error_message}"- raise Provider::Error.new(+ raise Provider::Sophtron::Error.new("Sophtron API error: #{error_message}",:api_error)diff --git a/app/models/sophtron_item/syncer.rb b/app/models/sophtron_item/syncer.rbindex 70465ac83d..3f74f5b548 100644--- a/app/models/sophtron_item/syncer.rb+++ b/app/models/sophtron_item/syncer.rb@@ -36,7 +36,8 @@ class SophtronItem::Syncerdef perform_sync(sync)# Phase 1: Import data from Sophtron APIsync.update!(status_text: t("sophtron_items.syncer.importing_accounts")) if sync.respond_to?(:status_text)- sophtron_item.import_latest_sophtron_data+ import_result = sophtron_item.import_latest_sophtron_data(sync: sync)+ import_errors = import_errors_for(import_result)# Phase 2: Check account setup status and collect sync statisticssync.update!(status_text: t("sophtron_items.syncer.checking_account_configuration")) if sync.respond_to?(:status_text)@@ -79,9 +80,14 @@ class SophtronItem::Syncerend# Mark sync health- collect_health_stats(sync, errors: nil)+ if import_errors.present?+ collect_health_stats(sync, errors: import_errors)+ raise StandardError.new(import_errors.map { |error| error[:message] }.join(", "))+ else+ collect_health_stats(sync, errors: nil)+ endrescue => e- collect_health_stats(sync, errors: [ { message: e.message, category: "sync_error" } ])+ collect_health_stats(sync, errors: [ { message: e.message, category: "sync_error" } ]) unless sync_errors_recorded?(sync)raiseend@@ -93,4 +99,37 @@ class SophtronItem::Syncerdef perform_post_sync# no-opend++ private++ def import_errors_for(import_result)+ return [] if import_result.blank? || import_result[:success]++ if import_result[:error].present?+ return [ { message: import_result[:error], category: "sync_error" } ]+ end++ errors = []+ if import_result[:accounts_failed].to_i.positive?+ errors << {+ message: "#{import_result[:accounts_failed]} #{'account'.pluralize(import_result[:accounts_failed])} failed to import",+ category: "account_import"+ }+ end++ if import_result[:transactions_failed].to_i.positive?+ errors << {+ message: "#{import_result[:transactions_failed]} #{'account'.pluralize(import_result[:transactions_failed])} failed to import transactions",+ category: "transaction_import"+ }+ end++ errors.presence || [ { message: "Sophtron import failed", category: "sync_error" } ]+ end++ def sync_errors_recorded?(sync)+ return false unless sync.respond_to?(:sync_stats)++ sync.sync_stats.to_h["total_errors"].to_i.positive?+ endenddiff --git a/app/views/sophtron_items/_api_error.html.erb b/app/views/sophtron_items/_api_error.html.erbindex 852fd09b76..e62f9628fe 100644--- a/app/views/sophtron_items/_api_error.html.erb+++ b/app/views/sophtron_items/_api_error.html.erb@@ -1,34 +1,40 @@-<%# locals: (error_message:, return_path: nil) %>+<%# locals: (error_message:, return_path: nil, heading: nil, issue_keys: nil, action_label: nil) %>+<% heading ||= t("sophtron_items.api_error.unable_to_connect") %>+<% issue_keys ||= %w[incorrect_user_id invalid_access_key expired_credentials network_issue service_down] %>+<% action_path = return_path.presence || settings_providers_path %>+<% action_label ||= t("sophtron_items.api_error.check_provider_settings") %>+<%= turbo_frame_tag "modal" do %><%= render DS::Dialog.new do |dialog| %><% dialog.with_header(title: t("sophtron_items.api_error.title")) %><% dialog.with_body do %><div class="space-y-4">- <div class="flex items-start gap-3">- <%= icon("alert-circle", class: "text-destructive w-5 h-5 shrink-0 mt-0.5") %>- <div class="text-sm">- <p class="font-medium text-primary mb-2"><%= t("sophtron_items.api_error.unable_to_connect") %></p>- <p class="text-secondary"><%= h(error_message) %></p>+ <div class="rounded-lg border border-primary bg-container-inset p-3">+ <div class="flex items-start gap-3">+ <%= icon("alert-circle", color: "destructive", size: "sm", class: "mt-0.5") %>+ <div class="space-y-1 text-sm">+ <p class="font-medium text-primary"><%= heading %></p>+ <p class="text-secondary"><%= h(error_message) %></p>+ </div></div></div>- <div class="bg-surface rounded-lg p-4 space-y-2 text-sm">+ <div class="rounded-lg border border-primary bg-container-inset p-3 text-sm"><p class="font-medium text-primary"><%= t("sophtron_items.api_error.common_issues_title") %></p>- <ul class="list-disc list-inside space-y-1 text-secondary">- <li><%= t("sophtron_items.api_error.incorrect_user_id") %></li>- <li><%= t("sophtron_items.api_error.invalid_access_key") %></li>- <li><%= t("sophtron_items.api_error.expired_credentials") %></li>- <li><%= t("sophtron_items.api_error.network_issue") %></li>- <li><%= t("sophtron_items.api_error.service_down") %></li>+ <ul class="mt-2 list-disc space-y-1 pl-5 text-secondary">+ <% issue_keys.each do |issue_key| %>+ <li><%= t("sophtron_items.api_error.#{issue_key}") %></li>+ <% end %></ul></div>- <div class="mt-4">- <%= link_to (return_path.presence || settings_providers_path),- class: "inline-flex items-center justify-center rounded-lg px-4 py-2 text-sm font-medium text-inverse bg-inverse hover:bg-inverse-hover focus:outline-none focus:ring-2 focus:ring-primary transition-colors",- data: { turbo: false } do %>- <%= t("sophtron_items.api_error.check_provider_settings") %>- <% end %>+ <div class="flex justify-end">+ <%= render DS::Link.new(+ text: action_label,+ href: action_path,+ variant: :primary,+ data: { turbo: false }+ ) %></div></div><% end %>diff --git a/app/views/sophtron_items/_setup_required.html.erb b/app/views/sophtron_items/_setup_required.html.erbindex 9c759597f9..d306640f41 100644--- a/app/views/sophtron_items/_setup_required.html.erb+++ b/app/views/sophtron_items/_setup_required.html.erb@@ -3,17 +3,19 @@<% dialog.with_header(title: t("sophtron_items.sophtron_setup_required.title")) %><% dialog.with_body do %><div class="space-y-4">- <div class="flex items-start gap-3">- <%= icon("alert-circle", class: "text-warning w-5 h-5 shrink-0 mt-0.5") %>- <div class="text-sm text-secondary">- <p class="font-medium text-primary mb-2"><%= t("sophtron_items.sophtron_setup_required.heading") %></p>- <p><%= t("sophtron_items.sophtron_setup_required.description") %></p>+ <div class="rounded-lg border border-primary bg-container-inset p-3">+ <div class="flex items-start gap-3">+ <%= icon("alert-circle", color: "warning", size: "sm", class: "mt-0.5") %>+ <div class="space-y-1 text-sm text-secondary">+ <p class="font-medium text-primary"><%= t("sophtron_items.sophtron_setup_required.heading") %></p>+ <p><%= t("sophtron_items.sophtron_setup_required.description") %></p>+ </div></div></div>- <div class="bg-surface rounded-lg p-4 space-y-2 text-sm">+ <div class="rounded-lg border border-primary bg-container-inset p-3 text-sm"><p class="font-medium text-primary"><%= t("sophtron_items.sophtron_setup_required.setup_steps_title") %></p>- <ol class="list-decimal list-inside space-y-1 text-secondary">+ <ol class="mt-2 list-decimal space-y-1 pl-5 text-secondary"><li><%= t("sophtron_items.sophtron_setup_required.step_1_html") %></li><li><%= t("sophtron_items.sophtron_setup_required.step_2_html") %></li><li><%= t("sophtron_items.sophtron_setup_required.step_3_html") %></li>@@ -21,14 +23,15 @@</ol></div>- <div class="mt-4">- <%= link_to settings_providers_path,- class: "inline-flex items-center justify-center rounded-lg px-4 py-2 text-sm font-medium text-inverse button-bg-primary hover:button-bg-primary-hover focus:outline-none focus:ring-2 focus:ring-gray-900 theme-dark:focus:ring-white focus:ring-offset-2 transition-colors",- data: { turbo: false } do %>- <%= t("sophtron_items.sophtron_setup_required.go_to_provider_settings") %>- <% end %>+ <div class="flex justify-end">+ <%= render DS::Link.new(+ text: t("sophtron_items.sophtron_setup_required.go_to_provider_settings"),+ href: settings_providers_path,+ variant: :primary,+ data: { turbo: false }+ ) %></div></div><% end %><% end %>-<% end %>\ No newline at end of file+<% end %>diff --git a/app/views/sophtron_items/_sophtron_item.html.erb b/app/views/sophtron_items/_sophtron_item.html.erbindex dd4431af6e..f360de15a2 100644--- a/app/views/sophtron_items/_sophtron_item.html.erb+++ b/app/views/sophtron_items/_sophtron_item.html.erb@@ -1,6 +1,7 @@<%# locals: (sophtron_item:) %><%= tag.div id: dom_id(sophtron_item) do %>+ <% provider_display_name = sophtron_item.provider_display_name %><details open class="group bg-container p-4 shadow-border-xs rounded-xl"><summary class="flex items-center justify-between gap-2"><div class="flex items-center gap-2">@@ -11,14 +12,14 @@<%= image_tag sophtron_item.logo, class: "rounded-full h-full w-full", loading: "lazy" %><% else %><div class="flex items-center justify-center">- <%= tag.p sophtron_item.name&.first&.upcase || "?", class: "text-orange-600 text-xs font-medium" %>+ <%= tag.p provider_display_name.first.upcase, class: "text-orange-600 text-xs font-medium" %></div><% end %></div><div class="pl-1 text-sm"><div class="flex items-center gap-2">- <%= tag.p sophtron_item.name, class: "font-medium text-primary" %>+ <%= tag.p provider_display_name, class: "font-medium text-primary" %><% if sophtron_item.scheduled_for_deletion? %><p class="text-destructive text-sm animate-pulse"><%= t(".deletion_in_progress") %></p><% end %>@@ -70,7 +71,7 @@icon: "trash-2",href: sophtron_item_path(sophtron_item),method: :delete,- confirm: CustomConfirm.for_resource_deletion(sophtron_item.name, high_severity: true)+ confirm: CustomConfirm.for_resource_deletion(provider_display_name, high_severity: true)) %><% end %></div>diff --git a/app/views/sophtron_items/connect.html.erb b/app/views/sophtron_items/connect.html.erbnew file mode 100644index 0000000000..210f63a5a5--- /dev/null+++ b/app/views/sophtron_items/connect.html.erb@@ -0,0 +1,82 @@+<%= turbo_frame_tag "modal" do %>+ <%= render DS::Dialog.new do |dialog| %>+ <% dialog.with_header(title: t(".title")) %>++ <% dialog.with_body do %>+ <div class="space-y-4">+ <%= form_with url: select_accounts_sophtron_items_path, method: :get, class: "space-y-3" do %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account&.id %>+ <%= hidden_field_tag :return_to, @return_to %>+ <%= hidden_field_tag :connect_new_institution, true if @connect_new_institution %>++ <div class="form-field">+ <div class="form-field__body">+ <%= label_tag :institution_name, t(".institution_search_label"), class: "form-field__label" %>+ <%= text_field_tag :institution_name, @institution_search, placeholder: t(".institution_search_placeholder"), autocomplete: "off", class: "form-field__input" %>+ </div>+ </div>++ <div class="flex justify-end">+ <%= render DS::Button.new(text: t(".search"), type: "submit") %>+ </div>+ <% end %>++ <% if @institution_search.present? && @institution_search.length < 2 %>+ <p class="rounded-lg border border-primary bg-container-inset p-3 text-sm text-secondary"><%= t(".search_too_short") %></p>+ <% elsif @institution_search.present? && @institutions.empty? %>+ <p class="rounded-lg border border-primary bg-container-inset p-3 text-sm text-secondary"><%= t(".no_institutions") %></p>+ <% end %>++ <div class="space-y-3">+ <% @institutions.each_with_index do |institution, index| %>+ <% institution = institution.with_indifferent_access %>+ <% institution_id = institution[:InstitutionID] || institution[:institution_id] %>+ <% field_suffix = (institution_id.presence || index).to_s.parameterize %>+ <% bank_username_id = "bank_username_#{field_suffix}" %>+ <% bank_password_id = "bank_password_#{field_suffix}" %>+ <%= form_with url: connect_institution_sophtron_item_path(@sophtron_item), method: :post, class: "space-y-3 rounded-lg border border-primary bg-container-inset p-3", data: { turbo_frame: "modal" } do %>+ <%= hidden_field_tag :institution_id, institution_id %>+ <%= hidden_field_tag :institution_name, institution[:InstitutionName] || institution[:institution_name] %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account&.id %>+ <%= hidden_field_tag :return_to, @return_to %>+ <%= hidden_field_tag :connect_new_institution, true if @connect_new_institution %>++ <h3 class="text-sm font-medium text-primary"><%= institution[:InstitutionName] || institution[:institution_name] %></h3>++ <div class="grid grid-cols-1 gap-3">+ <div class="form-field">+ <div class="form-field__body">+ <%= label_tag bank_username_id, t(".username"), class: "form-field__label" %>+ <%= text_field_tag :bank_username, nil, id: bank_username_id, autocomplete: "username", class: "form-field__input" %>+ </div>+ </div>++ <div class="form-field">+ <div class="form-field__body">+ <%= label_tag bank_password_id, t(".password"), class: "form-field__label" %>+ <%= password_field_tag :bank_password, nil, id: bank_password_id, autocomplete: "current-password", class: "form-field__input" %>+ </div>+ </div>+ </div>++ <div class="flex justify-end">+ <%= render DS::Button.new(text: t(".connect"), type: "submit") %>+ </div>+ <% end %>+ <% end %>+ </div>++ <div class="flex justify-end pt-2">+ <%= render DS::Link.new(+ text: t(".cancel"),+ href: @return_to || accounts_path,+ variant: :secondary,+ data: { turbo_frame: "_top", action: "click->DS--dialog#close" }+ ) %>+ </div>+ </div>+ <% end %>+ <% end %>+<% end %>diff --git a/app/views/sophtron_items/connection_status.html.erb b/app/views/sophtron_items/connection_status.html.erbnew file mode 100644index 0000000000..6c7ffa3684--- /dev/null+++ b/app/views/sophtron_items/connection_status.html.erb@@ -0,0 +1,51 @@+<% polling_data = if @timed_out+ {}+ else+ {+ controller: "polling",+ polling_frame_id_value: "modal",+ polling_url_value: connection_status_sophtron_item_path(@sophtron_item, accountable_type: @accountable_type, account_id: @account_id, return_to: @return_to, poll_attempt: @next_poll_attempt, post_mfa: @post_mfa_polling),+ polling_interval_value: @poll_interval_ms+ }+ end %>+<% check_again_attempt = @timed_out ? 1 : (@next_poll_attempt || @poll_attempt.to_i + 1) %>++<%= turbo_frame_tag "modal" do %>+ <%= tag.div(data: polling_data) do %>+ <%= render DS::Dialog.new do |dialog| %>+ <% dialog.with_header(title: t(".title")) %>++ <% dialog.with_body do %>+ <div class="space-y-4">+ <div class="rounded-lg border border-primary bg-container-inset p-3">+ <div class="flex items-start gap-3">+ <% if @timed_out %>+ <%= icon "alert-circle", size: "sm", class: "mt-0.5" %>+ <% else %>+ <%= icon "loader", size: "sm", class: "mt-0.5 animate-spin" %>+ <% end %>++ <div class="space-y-1 text-sm">+ <p class="font-medium text-primary">+ <%= @timed_out ? t(".timeout") : t(".waiting") %>+ </p>+ <p class="text-xs text-secondary">+ <%= t(".attempt", attempt: (@poll_attempt || 1), max: @max_poll_attempts) %>+ </p>+ </div>+ </div>+ </div>++ <div class="flex gap-2 justify-end">+ <%= render DS::Link.new(+ text: t(".check_again"),+ href: connection_status_sophtron_item_path(@sophtron_item, accountable_type: @accountable_type, account_id: @account_id, return_to: @return_to, poll_attempt: check_again_attempt, post_mfa: @post_mfa_polling),+ variant: :primary,+ data: { turbo_frame: "modal", turbo_prefetch: false }+ ) %>+ </div>+ </div>+ <% end %>+ <% end %>+ <% end %>+<% end %>diff --git a/app/views/sophtron_items/mfa.html.erb b/app/views/sophtron_items/mfa.html.erbnew file mode 100644index 0000000000..6100c60753--- /dev/null+++ b/app/views/sophtron_items/mfa.html.erb@@ -0,0 +1,97 @@+<%= turbo_frame_tag "modal" do %>+ <% security_questions = Array(@challenge[:security_questions]) %>+ <% token_methods = Array(@challenge[:token_methods]) %>+ <% safe_captcha_image = @challenge[:captcha_image].to_s.split(%r{[^A-Za-z0-9+/=\s]}, 2).first.to_s.gsub(/\s+/, "") %>++ <%= render DS::Dialog.new do |dialog| %>+ <% dialog.with_header(title: t(".title")) %>++ <% dialog.with_body do %>+ <div class="space-y-4">+ <% if security_questions.any? %>+ <%= form_with url: submit_mfa_sophtron_item_path(@sophtron_item), method: :post, class: "space-y-3", data: { turbo_frame: "modal" } do %>+ <%= hidden_field_tag :mfa_type, "security_answer" %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account_id %>+ <%= hidden_field_tag :return_to, @return_to %>++ <% security_questions.each_with_index do |question, index| %>+ <% answer_field_id = "security_answer_#{index}" %>+ <div class="form-field">+ <div class="form-field__body">+ <%= label_tag answer_field_id, question, class: "form-field__label" %>+ <%= text_field_tag "security_answers[]", nil, id: answer_field_id, autocomplete: "off", class: "form-field__input" %>+ </div>+ </div>+ <% end %>++ <div class="flex justify-end">+ <%= render DS::Button.new(text: t(".submit"), type: "submit") %>+ </div>+ <% end %>+ <% elsif token_methods.any? %>+ <div class="space-y-2">+ <% token_methods.each do |method| %>+ <%= form_with url: submit_mfa_sophtron_item_path(@sophtron_item), method: :post, data: { turbo_frame: "modal" } do %>+ <%= hidden_field_tag :mfa_type, "token_choice" %>+ <%= hidden_field_tag :token_choice, method %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account_id %>+ <%= hidden_field_tag :return_to, @return_to %>+ <%= button_tag type: "submit", class: "w-full rounded-lg border border-primary bg-container-inset p-3 text-left text-sm text-primary transition-colors hover:bg-container-inset-hover" do %>+ <span class="font-medium"><%= method %></span>+ <% end %>+ <% end %>+ <% end %>+ </div>+ <% elsif @challenge[:token_sent] %>+ <%= form_with url: submit_mfa_sophtron_item_path(@sophtron_item), method: :post, class: "space-y-3", data: { turbo_frame: "modal" } do %>+ <%= hidden_field_tag :mfa_type, "token_input" %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account_id %>+ <%= hidden_field_tag :return_to, @return_to %>+ <div class="form-field">+ <div class="form-field__body">+ <%= label_tag :token_input, t(".token"), class: "form-field__label" %>+ <%= text_field_tag :token_input, nil, autocomplete: "one-time-code", class: "form-field__input" %>+ </div>+ </div>+ <div class="flex justify-end">+ <%= render DS::Button.new(text: t(".submit"), type: "submit") %>+ </div>+ <% end %>+ <% elsif @challenge[:token_read].present? %>+ <div class="space-y-3">+ <p class="rounded-lg border border-primary bg-container-inset p-3 text-sm text-primary"><%= @challenge[:token_read] %></p>+ <%= form_with url: submit_mfa_sophtron_item_path(@sophtron_item), method: :post, data: { turbo_frame: "modal" } do %>+ <%= hidden_field_tag :mfa_type, "verify_phone" %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account_id %>+ <%= hidden_field_tag :return_to, @return_to %>+ <%= render DS::Button.new(text: t(".phone_confirmed"), type: "submit") %>+ <% end %>+ </div>+ <% elsif safe_captcha_image.present? %>+ <%= form_with url: submit_mfa_sophtron_item_path(@sophtron_item), method: :post, class: "space-y-3", data: { turbo_frame: "modal" } do %>+ <%= hidden_field_tag :mfa_type, "captcha" %>+ <%= hidden_field_tag :accountable_type, @accountable_type %>+ <%= hidden_field_tag :account_id, @account_id %>+ <%= hidden_field_tag :return_to, @return_to %>+ <div class="rounded-lg border border-primary bg-container-inset p-3">+ <%= image_tag "data:image/png;base64,#{safe_captcha_image}", alt: t(".captcha_alt"), class: "max-w-full rounded-md" %>+ </div>+ <div class="form-field">+ <div class="form-field__body">+ <%= label_tag :captcha_input, t(".captcha"), class: "form-field__label" %>+ <%= text_field_tag :captcha_input, nil, autocomplete: "off", class: "form-field__input" %>+ </div>+ </div>+ <div class="flex justify-end">+ <%= render DS::Button.new(text: t(".submit"), type: "submit") %>+ </div>+ <% end %>+ <% end %>+ </div>+ <% end %>+ <% end %>+<% end %>diff --git a/app/views/sophtron_items/select_accounts.html.erb b/app/views/sophtron_items/select_accounts.html.erbindex 6063ac4e6b..760847e2e8 100644--- a/app/views/sophtron_items/select_accounts.html.erb+++ b/app/views/sophtron_items/select_accounts.html.erb@@ -17,10 +17,10 @@<% @available_accounts.each do |account| %><% Rails.logger.debug "Sophtron account data: #{account.inspect}" %><% has_blank_name = account[:account_name].blank? %>- <label class="flex items-start gap-3 p-3 border <%= has_blank_name ? "border-error bg-error/5" : "border-primary" %> rounded-lg <%= has_blank_name ? "cursor-not-allowed opacity-60" : "hover:bg-subtle cursor-pointer" %> transition-colors">+ <label class="flex items-start gap-3 rounded-lg border <%= has_blank_name ? "border-destructive bg-container-inset opacity-60" : "border-primary bg-container-inset hover:bg-container-inset-hover" %> p-3 <%= has_blank_name ? "cursor-not-allowed" : "cursor-pointer" %> transition-colors"><%= check_box_tag "account_ids[]", account[:id], false, disabled: has_blank_name, class: "mt-1" %><div class="flex-1">- <div class="font-medium text-sm <%= has_blank_name ? "text-error" : "text-primary" %>">+ <div class="text-sm font-medium <%= has_blank_name ? "text-destructive" : "text-primary" %>"><% if has_blank_name %><%= t(".no_name_placeholder") %><% else %>@@ -31,7 +31,7 @@<%= account[:institution_name] %> • <%= account[:currency] %> • <%= account[:status] %></div><% if has_blank_name %>- <div class="text-xs text-error mt-1">+ <div class="mt-1 text-xs text-destructive"><%= t(".configure_name_in_sophtron") %></div><% end %>@@ -41,11 +41,13 @@</div><div class="flex gap-2 justify-end pt-4">- <%= link_to t(".cancel"), @return_to || new_account_path,- class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded-lg text-primary button-bg-secondary hover:button-bg-secondary-hover",- data: { turbo_frame: "_top", action: "DS--dialog#close" } %>- <%= submit_tag t(".link_accounts"),- class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded-lg text-inverse bg-inverse hover:bg-inverse-hover disabled:button-bg-disabled" %>+ <%= render DS::Link.new(+ text: t(".cancel"),+ href: @return_to || new_account_path,+ variant: :secondary,+ data: { turbo_frame: "_top", action: "DS--dialog#close" }+ ) %>+ <%= render DS::Button.new(text: t(".link_accounts"), type: "submit") %></div></form></div>diff --git a/app/views/sophtron_items/select_existing_account.html.erb b/app/views/sophtron_items/select_existing_account.html.erbindex 6273a7c1f3..a717a37ab1 100644--- a/app/views/sophtron_items/select_existing_account.html.erb+++ b/app/views/sophtron_items/select_existing_account.html.erb@@ -18,13 +18,13 @@<div class="space-y-2"><% @available_accounts.each do |account| %><% has_blank_name = account[:account_name].blank? %>- <label class="flex items-start gap-3 p-3 border <%= has_blank_name ? "border-error bg-error/5" : "border-primary" %> rounded-lg <%= has_blank_name ? "cursor-not-allowed opacity-60" : "hover:bg-subtle cursor-pointer" %> transition-colors">+ <label class="flex items-start gap-3 rounded-lg border <%= has_blank_name ? "border-destructive bg-container-inset opacity-60" : "border-primary bg-container-inset hover:bg-container-inset-hover" %> p-3 <%= has_blank_name ? "cursor-not-allowed" : "cursor-pointer" %> transition-colors"><%= radio_button_tag "sophtron_account_id", account[:id], false,required: true,disabled: has_blank_name,class: "mt-1" %><div class="flex-1">- <div class="font-medium text-sm <%= has_blank_name ? "text-error" : "text-primary" %>">+ <div class="text-sm font-medium <%= has_blank_name ? "text-destructive" : "text-primary" %>"><% if has_blank_name %><%= t(".no_name_placeholder") %><% else %>@@ -35,7 +35,7 @@<%= account[:institution_name] %> • <%= account[:currency] %> • <%= account[:status] %></div><% if has_blank_name %>- <div class="text-xs text-error mt-1">+ <div class="mt-1 text-xs text-destructive"><%= t(".configure_name_in_sophtron") %></div><% end %>@@ -45,11 +45,13 @@</div><div class="flex gap-2 justify-end pt-4">- <%= link_to t(".cancel"), @return_to || accounts_path,- class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded-lg text-primary button-bg-secondary hover:button-bg-secondary-hover",- data: { turbo_frame: "_top", action: "click->DS--dialog#close" } %>- <%= submit_tag t(".link_account"),- class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded-lg text-inverse bg-inverse hover:bg-inverse-hover disabled:button-bg-disabled" %>+ <%= render DS::Link.new(+ text: t(".cancel"),+ href: @return_to || accounts_path,+ variant: :secondary,+ data: { turbo_frame: "_top", action: "click->DS--dialog#close" }+ ) %>+ <%= render DS::Button.new(text: t(".link_account"), type: "submit") %></div><% end %></div>diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rbindex af9db03123..5e4690a352 100644--- a/config/initializers/filter_parameter_logging.rb+++ b/config/initializers/filter_parameter_logging.rb@@ -5,5 +5,6 @@# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.Rails.application.config.filter_parameters += [:passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :openai_access_token,- :client_id, :consumer_key, :snaptrade_user_id, :snaptrade_user_secret+ :client_id, :consumer_key, :snaptrade_user_id, :snaptrade_user_secret,+ :bank_username, :bank_password, :security_answers, :captcha_input]diff --git a/config/locales/views/sophtron_items/en.yml b/config/locales/views/sophtron_items/en.ymlindex f5a9c3353a..1a03116d6b 100644--- a/config/locales/views/sophtron_items/en.yml+++ b/config/locales/views/sophtron_items/en.yml@@ -59,6 +59,42 @@ en:no_accounts_found: No accounts found. Please check your API key configuration.no_access_key: Sophtron Access key is not configured. Please configure it in Settings.no_user_id: Sophtron User ID is not configured. Please configure it in Settings.+ no_institution_connected: Please connect a bank institution with Sophtron first.+ connect:+ cancel: Cancel+ captcha: Captcha+ connect: Connect+ institution_search_label: Institution+ institution_search_placeholder: Search by bank name+ no_institutions: No matching institutions found.+ password: Password+ search: Search+ search_too_short: Enter at least two characters to search.+ title: Connect Sophtron Institution+ username: Username+ connect_institution:+ api_error: "Sophtron connection failed: %{message}"+ missing_parameters: Select an institution and enter your bank login credentials.+ connection_status:+ api_error: "API connection error: %{message}"+ attempt: "Attempt %{attempt} of %{max}"+ check_again: Check again+ failed: Sophtron could not complete this institution connection.+ failed_timeout: Sophtron timed out while the institution was completing login.+ timeout: Sophtron did not finish connecting within the expected time. You can check again, or try reconnecting later.+ title: Connecting Sophtron+ waiting: Sophtron is still connecting to your institution.+ mfa:+ captcha: Captcha text+ captcha_alt: Sophtron captcha+ phone_confirmed: I confirmed by phone+ submit: Submit+ title: Sophtron Verification+ token: Verification code+ submit_mfa:+ api_error: "Verification failed: %{message}"+ invalid_security_answers: Security answers are missing or too long.+ unknown_challenge: Unknown Sophtron verification step.sophtron_item:accounts_need_setup: Accounts need setupdelete: Delete connection@@ -95,6 +131,7 @@ en:no_access_key: Sophtron Access key is not configured. Please configure it in Settings.no_user_id: Sophtron User ID is not configured. Please configure it in Settings.no_credentials_configured: "Please configure your Sophtron API User ID and Access Key first in Provider Settings."+ no_institution_connected: Please connect a bank institution with Sophtron first.no_name_placeholder: "(No name)"title: Select Sophtron Accountsselect_existing_account:@@ -109,6 +146,7 @@ en:no_accounts_found: No Sophtron accounts found. Please check your API key configuration.no_access_key: Sophtron Access key is not configured. Please configure it in Settings.no_user_id: Sophtron User ID is not configured. Please configure it in Settings.+ no_institution_connected: Please connect a bank institution with Sophtron first.no_name_placeholder: "(No name)"title: "Link %{account_name} with Sophtron"link_existing_account:@@ -119,6 +157,7 @@ en:sophtron_account_already_linked: This Sophtron account is already linked to another accountsophtron_account_not_found: Sophtron account not foundmissing_parameters: Missing required parameters+ no_institution_connected: Please connect a bank institution with Sophtron first.success: "Successfully linked %{account_name} with Sophtron"setup_accounts:account_type_label: "Account Type:"@@ -129,6 +168,7 @@ en:no_accounts_to_setup: "No Accounts to Set Up"no_access_key: "Sophtron Access key is not configured. Please check your connection settings."no_user_id: "Sophtron User ID is not configured. Please check your connection settings."+ no_institution_connected: "Sophtron institution is not connected yet."account_types:skip: Skip this accountdepository: Checking or Savings Account@@ -179,13 +219,19 @@ en:api_error:title: "Sophtron Connection Error"unable_to_connect: "Unable to connect to Sophtron"+ institution_unable_to_connect: "Unable to connect to the institution"common_issues_title: "Common Issues:"incorrect_user_id: "Incorrect User ID: Verify your User ID in Provider Settings"invalid_access_key: "Invalid Access Key: Check your Access Key in Provider Settings"expired_credentials: "Expired Credentials: Generate a new User ID and Access Key from Sophtron"network_issue: "Network Issue: Check your internet connection"service_down: "Service Down: Sophtron API may be temporarily unavailable"+ bank_credentials: "Bank credentials: Check the username and password for the selected institution"+ verification_code: "Verification code: Make sure the latest code was entered before it expired"+ institution_timeout: "Institution timeout: The bank login page did not finish in time"+ unsupported_mfa: "MFA support: Sophtron may not support this institution's current verification flow"check_provider_settings: "Check Provider Settings"+ try_again: "Try connecting again"select_option: "Select %{type}"subtype: "subtype"type: "type"@@ -194,12 +240,12 @@ en:setup_instructions:step_1_html: 'Visit <a href="%{url}" target="_blank" rel="noopener noreferrer" class="link">Sophtron</a> to obtain your API credentials'step_2: "Copy your User ID and Access Key from your Sophtron account settings"- step_3: "Paste the credentials below and click Save to enable Sophtron bank data sync"+ step_3: "Paste the credentials below and click Save; Sure will create or reuse your Sophtron Customer ID automatically"field_descriptions_title: "Field descriptions:"field_descriptions:user_id_html: "<strong>User ID:</strong> Your Sophtron User ID credential"access_key_html: "<strong>Access Key:</strong> Your Sophtron Access Key credential"- base_url_html: "<strong>Base URL:</strong> The Sophtron API endpoint URL (usually provided by Sophtron)"+ base_url_html: "<strong>Base URL:</strong> The Sophtron API endpoint URL, usually https://api.sophtron.com/api"fields:user_id:label: "User ID"@@ -211,7 +257,7 @@ en:placeholder_edit: "••••••••"base_url:label: "Base URL"- placeholder: "https://api.sophtron.com/v2"+ placeholder: "https://api.sophtron.com/api"save: "Save Configuration"update: "Update Configuration"status:@@ -225,4 +271,4 @@ en:calculating_balances: "Calculating balances for linked accounts..."sophtron_entry:processor:- unknown_transaction: "Unknown transaction"\ No newline at end of file+ unknown_transaction: "Unknown transaction"diff --git a/config/routes.rb b/config/routes.rbindex 6bc6e2a2a9..b9735a7579 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -544,8 +544,11 @@ Rails.application.routes.draw doendmember do+ post :connect_institutionpost :syncpost :balances+ get :connection_status+ post :submit_mfaget :setup_accountspost :complete_account_setupenddiff --git a/db/migrate/20260507120000_add_sophtron_connection_state.rb b/db/migrate/20260507120000_add_sophtron_connection_state.rbnew file mode 100644index 0000000000..8abe446214--- /dev/null+++ b/db/migrate/20260507120000_add_sophtron_connection_state.rb@@ -0,0 +1,20 @@+class AddSophtronConnectionState < ActiveRecord::Migration[7.2]+ def change+ change_column_null :sophtron_accounts, :customer_id, true+ change_column_null :sophtron_accounts, :member_id, true++ add_column :sophtron_accounts, :account_number_mask, :string++ add_column :sophtron_items, :customer_id, :string+ add_column :sophtron_items, :customer_name, :string+ add_column :sophtron_items, :raw_customer_payload, :jsonb+ add_column :sophtron_items, :user_institution_id, :string+ add_column :sophtron_items, :current_job_id, :string+ add_column :sophtron_items, :job_status, :string+ add_column :sophtron_items, :raw_job_payload, :jsonb+ add_column :sophtron_items, :last_connection_error, :text++ add_index :sophtron_items, :customer_id+ add_index :sophtron_items, :user_institution_id+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 1c11eea005..6512c65ff4 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[7.2].define(version: 2026_05_03_180000) do+ActiveRecord::Schema[7.2].define(version: 2026_05_07_120000) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -1400,8 +1400,9 @@ ActiveRecord::Schema[7.2].define(version: 2026_05_03_180000) dot.jsonb "institution_metadata"t.jsonb "raw_payload"t.jsonb "raw_transactions_payload"- t.string "customer_id", null: false- t.string "member_id", null: false+ t.string "customer_id"+ t.string "member_id"+ t.string "account_number_mask"t.datetime "created_at", null: falset.datetime "updated_at", null: falset.index ["account_id"], name: "index_sophtron_accounts_on_account_id"@@ -1426,10 +1427,20 @@ ActiveRecord::Schema[7.2].define(version: 2026_05_03_180000) dot.string "user_id", null: falset.string "access_key", null: falset.string "base_url"+ t.string "customer_id"+ t.string "customer_name"+ t.jsonb "raw_customer_payload"+ t.string "user_institution_id"+ t.string "current_job_id"+ t.string "job_status"+ t.jsonb "raw_job_payload"+ t.text "last_connection_error"t.datetime "created_at", null: falset.datetime "updated_at", null: false+ t.index ["customer_id"], name: "index_sophtron_items_on_customer_id"t.index ["family_id"], name: "index_sophtron_items_on_family_id"t.index ["status"], name: "index_sophtron_items_on_status"+ t.index ["user_institution_id"], name: "index_sophtron_items_on_user_institution_id"endcreate_table "sso_audit_logs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|diff --git a/test/controllers/sophtron_items_controller_test.rb b/test/controllers/sophtron_items_controller_test.rbnew file mode 100644index 0000000000..c69f5fa09e--- /dev/null+++ b/test/controllers/sophtron_items_controller_test.rb@@ -0,0 +1,605 @@+require "test_helper"++class SophtronItemsControllerTest < ActionDispatch::IntegrationTest+ setup do+ sign_in @user = users(:family_admin)+ @item = @user.family.sophtron_items.create!(+ name: "Sophtron",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key"),+ customer_id: "cust-1"+ )+ end++ test "select_accounts renders institution connection flow when no institution is connected" do+ SophtronItem.any_instance.stubs(:ensure_customer!).returns("cust-1")++ get select_accounts_sophtron_items_url++ assert_response :success+ assert_includes response.body, "Connect Sophtron Institution"+ end++ test "select_accounts renders institution search after failed connection attempt" do+ @item.update!(user_institution_id: "ui-1", status: :requires_update)+ SophtronItem.any_instance.stubs(:ensure_customer!).returns("cust-1")++ get select_accounts_sophtron_items_url++ assert_response :success+ assert_includes response.body, "Connect Sophtron Institution"+ end++ test "select_accounts renders institution search after stale Sophtron timeout" do+ @item.update!(+ user_institution_id: "ui-1",+ status: :good,+ job_status: "Timeout",+ raw_job_payload: {+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ SuccessFlag: false,+ LastStep: "LogInPanel",+ LastStatus: "Timeout"+ }+ )+ SophtronItem.any_instance.stubs(:ensure_customer!).returns("cust-1")++ get select_accounts_sophtron_items_url++ assert_response :success+ assert_includes response.body, "Connect Sophtron Institution"+ end++ test "select_accounts can start a new institution connection when already connected" do+ @item.update!(user_institution_id: "ui-1", status: :good)+ SophtronItem.any_instance.stubs(:ensure_customer!).returns("cust-1")++ get select_accounts_sophtron_items_url(connect_new_institution: true)++ assert_response :success+ assert_includes response.body, "Connect Sophtron Institution"+ assert_includes response.body, 'name="connect_new_institution"'+ end++ test "member cannot access Sophtron account selection" do+ sign_in users(:family_member)++ get select_accounts_sophtron_items_url++ assert_redirected_to accounts_path+ end++ test "cannot access another family's Sophtron item" do+ other_item = families(:empty).sophtron_items.create!(+ name: "Other Sophtron",+ user_id: "other-developer-user",+ access_key: Base64.strict_encode64("other-secret")+ )++ get connection_status_sophtron_item_url(other_item)++ assert_response :not_found+ end++ test "connect_institution persists job and user institution ids" do+ provider = mock+ provider.expects(:create_user_institution).with(+ institution_id: "inst-1",+ username: "bank-user",+ password: "bank-pass",+ pin: ""+ ).returns({+ JobID: "job-1",+ UserInstitutionID: "ui-1"+ })++ SophtronItem.any_instance.stubs(:ensure_customer!).returns("cust-1")+ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ post connect_institution_sophtron_item_url(@item), params: {+ institution_id: "inst-1",+ institution_name: "Example Bank",+ bank_username: "bank-user",+ bank_password: "bank-pass"+ }++ @item.reload+ assert_equal "Sophtron", @item.name+ assert_equal "Example Bank", @item.institution_name+ assert_equal "job-1", @item.current_job_id+ assert_equal "ui-1", @item.user_institution_id+ assert_redirected_to connection_status_sophtron_item_path(@item)+ end++ test "connect_institution creates separate item for additional institution" do+ @item.update!(+ institution_id: "apple-inst",+ institution_name: "Apple / Goldman Sachs",+ user_institution_id: "ui-apple",+ status: :good+ )+ @item.sophtron_accounts.create!(+ name: "Juan",+ account_id: "card-1",+ currency: "USD",+ balance: 1_947.18,+ institution_metadata: {+ name: "Apple / Goldman Sachs",+ user_institution_id: "ui-apple"+ }+ )++ provider = mock+ provider.expects(:create_user_institution).with(+ institution_id: "amazon-inst",+ username: "bank-user",+ password: "bank-pass",+ pin: ""+ ).returns({+ JobID: "job-amazon",+ UserInstitutionID: "ui-amazon"+ })++ SophtronItem.any_instance.stubs(:ensure_customer!).returns("cust-1")+ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ assert_difference -> { @user.family.sophtron_items.count }, 1 do+ post connect_institution_sophtron_item_url(@item), params: {+ institution_id: "amazon-inst",+ institution_name: "Amazon",+ bank_username: "bank-user",+ bank_password: "bank-pass",+ connect_new_institution: true+ }+ end++ @item.reload+ new_item = @user.family.sophtron_items.find_by!(user_institution_id: "ui-amazon")++ assert_equal "Apple / Goldman Sachs", @item.institution_name+ assert_equal "ui-apple", @item.user_institution_id+ assert_equal "Amazon", new_item.institution_name+ assert_equal "ui-amazon", new_item.user_institution_id+ assert_equal "job-amazon", new_item.current_job_id+ assert_redirected_to connection_status_sophtron_item_path(new_item, connect_new_institution: "true")+ end++ test "Sophtron bank credentials and mfa inputs are filtered from logs" do+ parameter_filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)+ filtered_params = parameter_filter.filter(+ bank_username: "bank-user",+ bank_password: "bank-pass",+ security_answers: [ "blue" ],+ captcha_input: "captcha"+ )++ assert_equal "[FILTERED]", filtered_params[:bank_username]+ assert_equal "[FILTERED]", filtered_params[:bank_password]+ assert_equal "[FILTERED]", filtered_params[:security_answers]+ assert_equal "[FILTERED]", filtered_params[:captcha_input]+ end++ test "create verifies credentials and persists provisioned customer id" do+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")+ .to_return(status: 200, body: "")+ stub_request(:get, "https://api.sophtron.com/api/v2/customers")+ .to_return(status: 200, body: [].to_json)+ stub_request(:post, "https://api.sophtron.com/api/v2/customers")+ .to_return(status: 200, body: {+ CustomerID: "cust-new",+ CustomerName: "Sure family #{@user.family.id}"+ }.to_json)++ assert_difference "SophtronItem.count", 1 do+ post sophtron_items_url, params: {+ sophtron_item: {+ name: "New Sophtron",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key")+ }+ }+ end++ item = @user.family.sophtron_items.find_by!(name: "New Sophtron")+ assert_equal "cust-new", item.customer_id+ assert_redirected_to accounts_path+ end++ test "connection_status renders MFA challenge when Sophtron asks for security answers" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ SecurityQuestion: [ "What is your favorite color?" ].to_json,+ SuccessFlag: nil,+ LastStatus: "Waiting"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item)++ assert_response :success+ assert_includes response.body, "What is your favorite color?"+ end++ test "connection_status sanitizes captcha image before rendering data uri" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ CaptchaImage: "YWJj+/=\"><svg onload=alert(1)>",+ LastStatus: "Waiting"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item)++ assert_response :success+ captcha_src = response.body[/src="data:image\/png;base64,([^"]+)"/, 1]+ assert_equal "YWJj+/=", captcha_src+ assert_no_match(/svg|onload|alert|[<>"\s]/i, captcha_src)+ end++ test "connection_status renders token challenge before failed timeout handling" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ SuccessFlag: false,+ TokenSentFlag: true,+ TokenInputName: "Token",+ LastStep: "TokenInput",+ LastStatus: "Timeout"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item)++ assert_response :success+ assert_includes response.body, "Verification code"+ assert_not_includes response.body, "Sophtron could not complete this connection."+ end++ test "connection_status times out after max UI polls" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: SophtronItemsController::CONNECTION_STATUS_MAX_POLLS)++ assert_response :success+ assert_includes response.body, "Sophtron did not finish connecting"+ assert_includes response.body, "Attempt #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS} of #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS}"+ assert_equal "requires_update", @item.reload.status+ assert_equal "job-1", @item.current_job_id+ end++ test "connection_status increments polling attempt while job is still running" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: 2)++ assert_response :success+ assert_includes response.body, "Attempt 2 of #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS}"+ assert_includes response.body, "poll_attempt=3"+ assert_includes response.body, 'data-controller="polling"'+ assert_includes response.body, 'data-polling-frame-id-value="modal"'+ assert_includes response.body, 'data-turbo-prefetch="false"'+ assert_select "a[href*='poll_attempt=3']"+ end++ test "connection_status keeps polling through the third initial attempt for delayed otp" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: 3)++ assert_response :success+ assert_includes response.body, "Attempt 3 of #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS}"+ assert_includes response.body, "poll_attempt=4"+ assert_not_includes response.body, "Sophtron did not finish connecting"+ assert_not_equal "requires_update", @item.reload.status+ end++ test "connection_status extends polling when Sophtron starts institution login before otp" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ LastStep: "LogInPanel",+ LastStatus: "Started"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: SophtronItemsController::CONNECTION_STATUS_MAX_POLLS)++ assert_response :success+ assert_includes response.body, "Attempt #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS} of #{SophtronItemsController::LOGIN_PROGRESS_CONNECTION_STATUS_MAX_POLLS}"+ assert_includes response.body, "poll_attempt=#{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS + 1}"+ assert_not_includes response.body, "Sophtron did not finish connecting"+ assert_not_equal "requires_update", @item.reload.status+ end++ test "connection_status keeps polling after initial max when login progress was already seen" do+ @item.update!(+ user_institution_id: "ui-1",+ current_job_id: "job-1",+ raw_job_payload: {+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ LastStep: "LogInPanel",+ LastStatus: "Started"+ }+ )+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ LastStep: "LogInPanel",+ LastStatus: "Started"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: SophtronItemsController::CONNECTION_STATUS_MAX_POLLS + 1)++ assert_response :success+ assert_includes response.body, "Attempt #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS + 1} of #{SophtronItemsController::LOGIN_PROGRESS_CONNECTION_STATUS_MAX_POLLS}"+ assert_includes response.body, "poll_attempt=#{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS + 2}"+ assert_not_includes response.body, "Sophtron did not finish connecting"+ assert_not_equal "requires_update", @item.reload.status+ end++ test "connection_status uses longer polling after mfa is submitted" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ TokenInput: "123456",+ LastStep: "TransactionTable",+ LastStatus: "Started"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: SophtronItemsController::CONNECTION_STATUS_MAX_POLLS, post_mfa: true)++ assert_response :success+ assert_includes response.body, "Attempt #{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS} of 15"+ assert_includes response.body, "poll_attempt=#{SophtronItemsController::CONNECTION_STATUS_MAX_POLLS + 1}"+ assert_not_includes response.body, "Sophtron did not finish connecting"+ end++ test "connection_status renders accounts when post mfa completed job has available accounts" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ TokenInput: "123456",+ LastStep: "TokenInput",+ LastStatus: "Completed"+ })+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ id: "acct-1",+ account_id: "acct-1",+ account_name: "Sophtron Checking",+ institution_name: "Example Bank",+ balance: "123.45",+ balance_currency: "USD",+ currency: "USD",+ status: "active"+ }.with_indifferent_access+ ],+ total: 1+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: 5, post_mfa: true)++ assert_response :success+ assert_includes response.body, "Sophtron Checking"+ assert_not_includes response.body, "poll_attempt=6"++ @item.reload+ assert_nil @item.current_job_id+ assert_equal "good", @item.status+ assert @item.pending_account_setup?+ end++ test "connection_status keeps polling when post mfa completed job has no accounts yet" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ TokenInput: "123456",+ LastStep: "Reset",+ LastStatus: "Completed"+ })+ provider.expects(:get_accounts).with("ui-1").returns({ accounts: [], total: 0 })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item, poll_attempt: 6, post_mfa: true)++ assert_response :success+ assert_includes response.body, "Attempt 6 of #{SophtronItemsController::POST_MFA_CONNECTION_STATUS_MAX_POLLS}"+ assert_includes response.body, "poll_attempt=7"+ assert_not_includes response.body, "Sophtron did not finish connecting"+ assert_equal "job-1", @item.reload.current_job_id+ end++ test "connection_status ignores browser prefetches" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ SophtronItem.any_instance.expects(:sophtron_provider).never++ get connection_status_sophtron_item_url(@item, poll_attempt: 2), headers: { "X-Sec-Purpose" => "prefetch" }++ assert_response :no_content+ assert_nil @item.reload.job_status+ end++ test "connection_status treats Sophtron timeout as failed" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:get_job_information).with("job-1").returns({+ AccountID: "00000000-0000-0000-0000-000000000000",+ JobType: "AddAccounts",+ JobID: "job-1",+ SuccessFlag: false,+ LastStep: "LogInPanel",+ LastStatus: "Timeout"+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ get connection_status_sophtron_item_url(@item)++ assert_response :success+ assert_includes response.body, "Sophtron timed out while the institution was completing login."+ assert_includes response.body, "Unable to connect to the institution"+ assert_includes response.body, "Bank credentials"+ assert_includes response.body, "Verification code"+ assert_includes response.body, "Try connecting again"+ assert_not_includes response.body, "Check Provider Settings"+ @item.reload+ assert_equal "requires_update", @item.status+ assert_nil @item.current_job_id+ assert_nil @item.user_institution_id+ end++ test "submit_mfa sends security answer as array string" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:update_job_security_answer).with("job-1", [ "blue" ]).returns({})++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ post submit_mfa_sophtron_item_url(@item), params: {+ mfa_type: "security_answer",+ security_answers: [ "blue" ]+ }++ assert_redirected_to connection_status_sophtron_item_path(@item, post_mfa: true)+ end++ test "submit_mfa rejects too many security answers" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:update_job_security_answer).never++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ post submit_mfa_sophtron_item_url(@item), params: {+ mfa_type: "security_answer",+ security_answers: Array.new(SophtronItemsController::MAX_SECURITY_ANSWERS + 1, "blue")+ }++ assert_redirected_to connection_status_sophtron_item_path(@item)+ assert_equal "Security answers are missing or too long.", flash[:alert]+ end++ test "submit_mfa rejects oversized security answers" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:update_job_security_answer).never++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ post submit_mfa_sophtron_item_url(@item), params: {+ mfa_type: "security_answer",+ security_answers: [ "a" * (SophtronItemsController::MAX_SECURITY_ANSWER_LENGTH + 1) ]+ }++ assert_redirected_to connection_status_sophtron_item_path(@item)+ assert_equal "Security answers are missing or too long.", flash[:alert]+ end++ test "submit_mfa redirects to post mfa polling window" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1")+ provider = mock+ provider.expects(:update_job_token_input).with("job-1", token_input: "123456").returns({})++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ post submit_mfa_sophtron_item_url(@item), params: {+ mfa_type: "token_input",+ token_input: "123456"+ }++ assert_redirected_to connection_status_sophtron_item_path(@item, post_mfa: true)+ end++ test "link_existing_account links manual account to sophtron account" do+ @item.update!(user_institution_id: "ui-1")+ account = accounts(:depository)+ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ id: "acct-1",+ account_id: "acct-1",+ account_name: "Sophtron Checking",+ balance: "123.45",+ balance_currency: "USD",+ currency: "USD",+ account_type: "checking"+ }.with_indifferent_access+ ],+ total: 1+ })++ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)+ SophtronItem.any_instance.stubs(:start_initial_load_later)++ assert_difference "AccountProvider.count", 1 do+ post link_existing_account_sophtron_items_url, params: {+ account_id: account.id,+ sophtron_account_id: "acct-1"+ }+ end++ assert account.reload.linked?+ assert_equal "SophtronAccount", account.account_providers.first.provider_type+ assert_redirected_to accounts_path+ end+enddiff --git a/test/jobs/sophtron_refresh_poll_job_test.rb b/test/jobs/sophtron_refresh_poll_job_test.rbnew file mode 100644index 0000000000..c29fa4d993--- /dev/null+++ b/test/jobs/sophtron_refresh_poll_job_test.rb@@ -0,0 +1,47 @@+require "test_helper"++class SophtronRefreshPollJobTest < ActiveJob::TestCase+ setup do+ @family = families(:dylan_family)+ @item = @family.sophtron_items.create!(+ name: "Sophtron",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key"),+ customer_id: "cust-1",+ user_institution_id: "ui-1"+ )+ @account = accounts(:depository)+ @sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100,+ raw_transactions_payload: [ { id: "existing-tx" } ]+ )+ AccountProvider.create!(account: @account, provider: @sophtron_account)+ end++ test "re-enqueues while Sophtron refresh job is still running" do+ provider = mock+ provider.expects(:get_job_information).with("refresh-job").returns({ LastStatus: "Started" })+ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)++ assert_enqueued_with(job: SophtronRefreshPollJob) do+ SophtronRefreshPollJob.perform_now(@sophtron_account, job_id: "refresh-job", attempts_remaining: 2)+ end+ end++ test "imports transactions and schedules account sync when refresh completes" do+ provider = mock+ provider.expects(:get_job_information).with("refresh-job").returns({ LastStatus: "Completed" })+ SophtronItem.any_instance.stubs(:sophtron_provider).returns(provider)+ SophtronItem::Importer.any_instance.expects(:import_transactions_after_refresh)+ .with(@sophtron_account)+ .returns({ success: true, transactions_count: 1 })+ SophtronAccount::Processor.any_instance.expects(:process).returns({ transactions_imported: 1 })++ assert_enqueued_with(job: SyncJob) do+ SophtronRefreshPollJob.perform_now(@sophtron_account, job_id: "refresh-job")+ end+ end+enddiff --git a/test/models/provider/sophtron_adapter_test.rb b/test/models/provider/sophtron_adapter_test.rbnew file mode 100644index 0000000000..2c041c9499--- /dev/null+++ b/test/models/provider/sophtron_adapter_test.rb@@ -0,0 +1,35 @@+require "uri"++require "test_helper"++class Provider::SophtronAdapterTest < ActiveSupport::TestCase+ test "new account connection config starts a new institution connection" do+ config = Provider::SophtronAdapter.connection_configs(family: families(:empty)).first++ new_account_uri = URI.parse(config[:new_account_path].call("Depository", "/accounts"))++ assert_equal "/sophtron_items/select_accounts", new_account_uri.path+ assert_includes new_account_uri.query, "accountable_type=Depository"+ assert_includes new_account_uri.query, "return_to=%2Faccounts"+ assert_includes new_account_uri.query, "connect_new_institution=true"+ end++ test "institution_name does not fall back to another institution item" do+ item = families(:dylan_family).sophtron_items.create!(+ name: "Sophtron Connection",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key"),+ institution_name: "Amazon",+ user_institution_id: "ui-amazon"+ )+ account = item.sophtron_accounts.create!(+ name: "Juan",+ account_id: "card-1",+ currency: "USD",+ balance: 1_947.18,+ institution_metadata: { user_institution_id: "ui-apple" }+ )++ assert_nil Provider::SophtronAdapter.new(account).institution_name+ end+enddiff --git a/test/models/provider/sophtron_test.rb b/test/models/provider/sophtron_test.rbnew file mode 100644index 0000000000..dfdeeb806c--- /dev/null+++ b/test/models/provider/sophtron_test.rb@@ -0,0 +1,225 @@+require "test_helper"++class Provider::SophtronTest < ActiveSupport::TestCase+ setup do+ @access_key = Base64.strict_encode64("secret-key")+ @provider = Provider::Sophtron.new("developer-user", @access_key)+ end++ test "builds FIApiAUTH header from last path segment only" do+ auth_header = @provider.auth_header_for("POST", "/api/UserInstitution/CreateUserInstitution")+ expected_auth_path = "/createuserinstitution"+ expected_signature = Base64.strict_encode64(+ OpenSSL::HMAC.digest(+ OpenSSL::Digest.new("sha256"),+ "secret-key",+ "POST\n#{expected_auth_path}"+ )+ )++ assert_equal "FIApiAUTH:developer-user:#{expected_signature}:#{expected_auth_path}", auth_header+ end++ test "lists v2 customers" do+ stub_request(:get, "https://api.sophtron.com/api/v2/customers")+ .to_return(status: 200, body: [ { CustomerID: "cust-1", CustomerName: "Sure family 1" } ].to_json)++ customers = provider_data(@provider.list_customers)++ assert_equal 1, customers.length+ assert_equal "cust-1", customers.first[:CustomerID]+ end++ test "creates customer with documented body" do+ stub_request(:post, "https://api.sophtron.com/api/v2/customers")+ .with(body: { UniqueID: "sure-family-1", Name: "Sure family 1", Source: "Sure" }.to_json)+ .to_return(status: 200, body: { CustomerID: "cust-1", CustomerName: "Sure family 1" }.to_json)++ customer = provider_data(@provider.create_customer(unique_id: "sure-family-1", name: "Sure family 1", source: "Sure"))++ assert_equal "cust-1", customer[:CustomerID]+ end++ test "health check auth accepts empty success body" do+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")+ .to_return(status: 200, body: "")++ assert_equal({}, provider_data(@provider.health_check_auth))+ end++ test "health check auth accepts non json success body" do+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")+ .to_return(status: 200, body: "OK")++ assert_equal "OK", provider_data(@provider.health_check_auth)+ end++ test "creates user institution with documented V1 body" do+ stub_request(:post, "https://api.sophtron.com/api/UserInstitution/CreateUserInstitution")+ .with(body: {+ UserID: "developer-user",+ InstitutionID: "inst-1",+ UserName: "bank-user",+ Password: "bank-pass",+ PIN: ""+ }.to_json)+ .to_return(status: 200, body: { JobID: "job-1", UserInstitutionID: "ui-1" }.to_json)++ response = provider_data(@provider.create_user_institution(+ institution_id: "inst-1",+ username: "bank-user",+ password: "bank-pass"+ ))++ assert_equal "job-1", response[:JobID]+ assert_equal "ui-1", response[:UserInstitutionID]+ end++ test "classifies Sophtron timeout job as failed" do+ job = {+ SuccessFlag: false,+ LastStep: "LogInPanel",+ LastStatus: "Timeout"+ }++ assert Provider::Sophtron.job_failed?(job)+ end++ test "classifies completed job without failure flag as completed" do+ job = {+ LastStep: "TokenInput",+ LastStatus: "Completed"+ }++ assert Provider::Sophtron.job_completed?(job)+ assert_not Provider::Sophtron.job_failed?(job)+ end++ test "does not classify completed job with failure flag as completed" do+ job = {+ SuccessFlag: false,+ LastStep: "LogInPanel",+ LastStatus: "Completed"+ }++ assert Provider::Sophtron.job_failed?(job)+ assert_not Provider::Sophtron.job_completed?(job)+ end++ test "classifies token input prompt as requiring input" do+ job = {+ TokenInputName: "Token",+ LastStep: "TokenInput",+ LastStatus: "Started"+ }++ assert Provider::Sophtron.job_requires_input?(job)+ end++ test "does not classify submitted token as requiring input" do+ job = {+ TokenInputName: "Token",+ TokenInput: "123456",+ LastStep: "TokenInput",+ LastStatus: "Started"+ }++ assert_not Provider::Sophtron.job_requires_input?(job)+ end++ test "submits security answers as JSON array string" do+ stub_request(:post, "https://api.sophtron.com/api/Job/UpdateJobSecurityAnswer")+ .with(body: { JobID: "job-1", SecurityAnswer: [ "blue" ].to_json }.to_json)+ .to_return(status: 200, body: "")++ assert_equal({}, provider_data(@provider.update_job_security_answer("job-1", [ "blue" ])))+ end++ test "fetches transactions by transaction date with documented body" do+ stub_request(:post, "https://api.sophtron.com/api/Transaction/GetTransactionsByTransactionDate")+ .with(body: {+ AccountID: "acct-1",+ StartDate: "2026-01-01",+ EndDate: "2026-02-01"+ }.to_json)+ .to_return(status: 200, body: [+ {+ TransactionID: "tx-1",+ Amount: "-12.34",+ TransactionDate: "2026-01-15",+ Description: "CHECKCARD 1234 Coffee Shop NY"+ }+ ].to_json)++ result = provider_data(@provider.get_account_transactions("acct-1", start_date: Date.new(2026, 1, 1), end_date: Date.new(2026, 2, 1)))+ transaction = result[:transactions].first++ assert_equal "tx-1", transaction[:id]+ assert_equal "acct-1", transaction[:accountId]+ assert_equal "-12.34", transaction[:amount]+ assert_equal "2026-01-15", transaction[:date]+ end++ test "normalizes TransactionId transaction identifiers" do+ stub_request(:post, "https://api.sophtron.com/api/Transaction/GetTransactionsByTransactionDate")+ .to_return(status: 200, body: [+ {+ TransactionId: "tx-1",+ Amount: "-12.34",+ TransactionDate: "2026-01-15",+ Description: "CHECKCARD 1234 Coffee Shop NY"+ }+ ].to_json)++ result = provider_data(@provider.get_account_transactions("acct-1", start_date: Date.new(2026, 1, 1), end_date: Date.new(2026, 2, 1)))++ assert_equal "tx-1", result[:transactions].first[:id]+ end++ test "maps user institution accounts from V1 fields" do+ stub_request(:post, "https://api.sophtron.com/api/UserInstitution/GetUserInstitutionAccounts")+ .with(body: { UserInstitutionID: "ui-1" }.to_json)+ .to_return(status: 200, body: [+ {+ AccountID: "acct-1",+ AccountName: "Checking",+ AccountType: "checking",+ AccountBalance: "123.45",+ AccountNumber: "00001234"+ }+ ].to_json)++ result = provider_data(@provider.get_accounts("ui-1"))+ account = result[:accounts].first++ assert_equal "acct-1", account[:account_id]+ assert_equal "Checking", account[:account_name]+ assert_equal "123.45", account[:balance]+ assert_equal "****1234", account[:account_number_mask]+ end++ test "empty success body parses as empty hash" do+ stub_request(:post, "https://api.sophtron.com/api/Job/UpdateJobCaptcha")+ .to_return(status: 200, body: "")++ assert_equal({}, provider_data(@provider.update_job_captcha("job-1", "abc123")))+ end++ test "raises typed error on unauthorized response" do+ stub_request(:get, "https://api.sophtron.com/api/Institution/HealthCheckAuth")+ .to_return(status: 401, body: "bad auth")++ response = @provider.health_check_auth++ assert_not response.success?+ assert_instance_of Provider::Sophtron::Error, response.error+ assert_equal :unauthorized, response.error.error_type+ end++ private++ def provider_data(response)+ assert response.success?+ response.data+ end+enddiff --git a/test/models/sophtron_account_test.rb b/test/models/sophtron_account_test.rbnew file mode 100644index 0000000000..9c5d197c62--- /dev/null+++ b/test/models/sophtron_account_test.rb@@ -0,0 +1,24 @@+require "test_helper"++class SophtronAccountTest < ActiveSupport::TestCase+ test "upsert_sophtron_snapshot stores owning item institution metadata fallback" do+ item = families(:dylan_family).sophtron_items.create!(+ name: "Sophtron Connection",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key"),+ institution_name: "Apple / Goldman Sachs",+ user_institution_id: "ui-apple"+ )++ account = item.sophtron_accounts.build+ account.upsert_sophtron_snapshot!(+ account_id: "card-1",+ account_name: "Juan",+ currency: "USD",+ balance: "1947.18"+ )++ assert_equal "Apple / Goldman Sachs", account.institution_metadata["name"]+ assert_equal "ui-apple", account.institution_metadata["user_institution_id"]+ end+enddiff --git a/test/models/sophtron_item/importer_test.rb b/test/models/sophtron_item/importer_test.rbnew file mode 100644index 0000000000..6fe1067777--- /dev/null+++ b/test/models/sophtron_item/importer_test.rb@@ -0,0 +1,298 @@+require "test_helper"++class SophtronItem::ImporterTest < ActiveSupport::TestCase+ include ActiveJob::TestHelper++ setup do+ @family = families(:dylan_family)+ @item = @family.sophtron_items.create!(+ name: "Sophtron",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key"),+ customer_id: "cust-1",+ user_institution_id: "ui-1"+ )+ end++ test "fetches accounts by stored user institution id" do+ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert result[:success]+ assert_equal 1, result[:accounts_created]+ assert_equal "acct-1", @item.sophtron_accounts.first.account_id+ end++ test "missing user institution id fails import and marks item requires update" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100+ )+ AccountProvider.create!(account: account, provider: sophtron_account)+ @item.update!(user_institution_id: nil, status: :good, last_connection_error: nil)++ provider = mock+ provider.expects(:get_accounts).never++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert_not result[:success]+ assert_equal "Sophtron institution connection is incomplete", result[:error]+ assert_equal "requires_update", @item.reload.status+ assert_equal "Sophtron institution connection is incomplete", @item.last_connection_error+ end++ test "initial linked account import fetches transactions without starting a refresh job" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100+ )+ AccountProvider.create!(account: account, provider: sophtron_account)++ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ provider.expects(:refresh_account).never+ provider.expects(:get_account_transactions).with("acct-1", start_date: anything).returns({+ transactions: [+ {+ id: "tx-1",+ accountId: "acct-1",+ amount: "-12.34",+ currency: "USD",+ date: "2026-05-01",+ merchant: "Coffee Shop",+ description: "Coffee Shop"+ }.with_indifferent_access+ ],+ total: 1+ })++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert result[:success]+ assert_equal 1, result[:transactions_imported]+ assert_equal 1, sophtron_account.reload.raw_transactions_payload.count+ end++ test "later sync refreshes account after an empty initial transaction fetch" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100+ )+ AccountProvider.create!(account: account, provider: sophtron_account)++ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ provider.expects(:refresh_account).never+ provider.expects(:get_account_transactions).with("acct-1", start_date: anything).returns({+ transactions: [],+ total: 0+ })++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert result[:success]+ assert_equal [], sophtron_account.reload.raw_transactions_payload++ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ provider.expects(:refresh_account).with("acct-1").returns({ JobID: "refresh-job" })+ provider.expects(:get_job_information).with("refresh-job").returns({ LastStatus: "Completed" })+ provider.expects(:get_account_transactions).with("acct-1", start_date: anything).returns({+ transactions: [+ {+ id: "tx-1",+ accountId: "acct-1",+ amount: "-12.34",+ currency: "USD",+ date: "2026-05-01",+ merchant: "Coffee Shop",+ description: "Coffee Shop"+ }.with_indifferent_access+ ],+ total: 1+ })++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert result[:success]+ assert_equal 1, result[:transactions_imported]+ assert_equal 1, sophtron_account.reload.raw_transactions_payload.count+ end++ test "completed item sync with no stored transaction payload refreshes before fetching" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100+ )+ AccountProvider.create!(account: account, provider: sophtron_account)+ @item.stubs(:last_synced_at).returns(Time.current)++ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ provider.expects(:refresh_account).with("acct-1").returns({ JobID: "refresh-job" })+ provider.expects(:get_job_information).with("refresh-job").returns({ LastStatus: "Completed" })+ provider.expects(:get_account_transactions).with("acct-1", start_date: anything).returns({+ transactions: [+ {+ id: "tx-1",+ accountId: "acct-1",+ amount: "-12.34",+ currency: "USD",+ date: "2026-05-01",+ merchant: "Coffee Shop",+ description: "Coffee Shop"+ }.with_indifferent_access+ ],+ total: 1+ })++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert result[:success]+ assert_equal 1, sophtron_account.reload.raw_transactions_payload.count+ end++ test "marks item requires update when refresh job requires mfa" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100,+ raw_transactions_payload: [ { id: "existing-tx" } ]+ )+ AccountProvider.create!(account: account, provider: sophtron_account)++ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ provider.expects(:refresh_account).with("acct-1").returns({ JobID: "refresh-job" })+ provider.expects(:get_job_information).with("refresh-job").returns({+ SecurityQuestion: [ "Question?" ].to_json,+ LastStatus: "Waiting"+ })++ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert_not result[:success]+ assert_equal "requires_update", @item.reload.status+ assert_equal "refresh-job", @item.current_job_id+ end++ test "refresh job still running enqueues poll job without fetching transactions" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Checking",+ currency: "USD",+ balance: 100,+ raw_transactions_payload: [ { id: "existing-tx" } ]+ )+ AccountProvider.create!(account: account, provider: sophtron_account)++ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ account_id: "acct-1",+ account_name: "Checking",+ balance: "100.00",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ provider.expects(:refresh_account).with("acct-1").returns({ JobID: "refresh-job" })+ provider.expects(:get_job_information).with("refresh-job").returns({ LastStatus: "Started" })+ provider.expects(:get_account_transactions).never++ assert_enqueued_with(job: SophtronRefreshPollJob) do+ result = SophtronItem::Importer.new(@item, sophtron_provider: provider).import++ assert result[:success]+ assert_equal 0, result[:transactions_imported]+ assert_equal 0, result[:transactions_failed]+ end+ end+enddiff --git a/test/models/sophtron_item_test.rb b/test/models/sophtron_item_test.rbnew file mode 100644index 0000000000..7be757e6dc--- /dev/null+++ b/test/models/sophtron_item_test.rb@@ -0,0 +1,165 @@+require "test_helper"++class SophtronItemTest < ActiveSupport::TestCase+ include ActiveJob::TestHelper++ setup do+ @family = families(:dylan_family)+ @item = @family.sophtron_items.create!(+ name: "Sophtron",+ user_id: "developer-user",+ access_key: Base64.strict_encode64("secret-key")+ )+ end++ test "ensure_customer reuses persisted customer id" do+ @item.update!(customer_id: "cust-existing")+ provider = mock+ provider.expects(:list_customers).never++ assert_equal "cust-existing", @item.ensure_customer!(provider: provider)+ end++ test "ensure_customer reuses matching listed customer" do+ provider = mock+ provider.expects(:list_customers).returns([+ { CustomerID: "cust-1", CustomerName: @item.generated_customer_name }+ ])+ provider.expects(:create_customer).never++ assert_equal "cust-1", @item.ensure_customer!(provider: provider)+ assert_equal "cust-1", @item.customer_id+ assert_equal @item.generated_customer_name, @item.customer_name+ end++ test "ensure_customer creates customer when no matching customer exists" do+ provider = mock+ provider.expects(:list_customers).returns([])+ provider.expects(:create_customer)+ .with(unique_id: @item.generated_customer_unique_id, name: @item.generated_customer_name, source: "Sure")+ .returns({ CustomerID: "cust-new", CustomerName: @item.generated_customer_name })++ assert_equal "cust-new", @item.ensure_customer!(provider: provider)+ assert_equal "cust-new", @item.customer_id+ end++ test "connected_to_institution ignores failed connection attempts" do+ @item.update!(user_institution_id: "ui-1", status: :requires_update)++ assert_not @item.connected_to_institution?+ end++ test "connected_to_institution ignores jobs that are still running" do+ @item.update!(user_institution_id: "ui-1", current_job_id: "job-1", status: :good)++ assert_not @item.connected_to_institution?+ end++ test "connected_to_institution ignores stale timeout job snapshots" do+ @item.update!(+ user_institution_id: "ui-1",+ status: :good,+ job_status: "Timeout",+ raw_job_payload: {+ SuccessFlag: false,+ LastStatus: "Timeout"+ }+ )++ assert_not @item.connected_to_institution?+ end++ test "provider_display_name keeps accounts grouping provider-level" do+ @item.update!(name: "Bank of America", institution_name: "Bank of America")++ assert_equal "Sophtron Connection", @item.provider_display_name+ end++ test "fetch_remote_accounts persists Sophtron account snapshots" do+ @item.update!(user_institution_id: "ui-1")+ provider = mock+ provider.expects(:get_accounts).with("ui-1").returns({+ accounts: [+ {+ id: "acct-1",+ account_id: "acct-1",+ account_name: "Sophtron Checking",+ balance: "123.45",+ balance_currency: "USD",+ currency: "USD"+ }.with_indifferent_access+ ],+ total: 1+ })+ @item.stubs(:sophtron_provider).returns(provider)++ accounts = @item.fetch_remote_accounts(force: true)++ assert_equal 1, accounts.count+ assert_equal "Sophtron Checking", @item.sophtron_accounts.find_by!(account_id: "acct-1").name+ end++ test "reject_already_linked removes accounts with existing account provider links" do+ account = accounts(:depository)+ sophtron_account = @item.sophtron_accounts.create!(+ account_id: "acct-1",+ name: "Sophtron Checking",+ currency: "USD",+ balance: 100+ )+ AccountProvider.create!(account: account, provider: sophtron_account)++ available = @item.reject_already_linked([+ { id: "acct-1", account_name: "Linked" },+ { id: "acct-2", account_name: "Available" }+ ])++ assert_equal [ "acct-2" ], available.map { |account_data| SophtronItem.external_account_id(account_data) }+ end++ test "build_mfa_challenge normalizes Sophtron job challenge fields" do+ challenge = @item.build_mfa_challenge(+ SecurityQuestion: [ "Question?" ].to_json,+ TokenMethod: [ "sms" ].to_json,+ TokenSentFlag: true,+ TokenInputName: "Token",+ TokenRead: "phone",+ CaptchaImage: "YWJj"+ )++ assert_equal [ "Question?" ], challenge[:security_questions]+ assert_equal [ "sms" ], challenge[:token_methods]+ assert_equal true, challenge[:token_sent]+ assert_equal "phone", challenge[:token_read]+ assert_equal "YWJj", challenge[:captcha_image]+ end++ test "start_initial_load_later starts a sync when no active sync exists" do+ assert_no_enqueued_jobs only: SophtronInitialLoadJob do+ assert_difference "@item.syncs.count", 1 do+ assert_enqueued_with job: SyncJob do+ @item.start_initial_load_later+ end+ end+ end+ end++ test "start_initial_load_later seeds sync window for transaction import" do+ @item.update!(sync_start_date: Date.new(2026, 1, 1))++ @item.start_initial_load_later++ assert_equal Date.new(2026, 1, 1), @item.syncs.ordered.first.window_start_date+ end++ test "start_initial_load_later queues a follow-up when current sync is already running" do+ sync = @item.syncs.create!+ sync.start!++ assert_no_difference "@item.syncs.count" do+ assert_enqueued_with job: SophtronInitialLoadJob do+ @item.start_initial_load_later+ end+ end+ end+end