The personal finance app for everyone (by everyone)
Ruby
Enable Banking: progressive date_from fallback on WRONG_TRANSACTIONS_PERIOD (#2992)
Pedro Santos7c56c8e2e8c9
diff --git a/app/models/provider/enable_banking.rb b/app/models/provider/enable_banking.rbindex 540accfc47..f6d0bff3fa 100644--- a/app/models/provider/enable_banking.rb+++ b/app/models/provider/enable_banking.rb@@ -6,6 +6,11 @@ class Provider::EnableBankingBASE_URL = "https://api.enablebanking.com".freeze+ # Progressive fallback windows (days) for retrying a transactions fetch when+ # an ASPSP rejects the requested period (WRONG_TRANSACTIONS_PERIOD) without+ # suggesting a corrected date_from in the response payload.+ FALLBACK_TRANSACTIONS_DATE_FROM_DAYS = [ 89, 60, 30 ].freeze+headers "User-Agent" => "Sure Finance Enable Banking Client"default_options.merge!({ timeout: 120 }.merge(httparty_ssl_options))@@ -164,7 +169,7 @@ class Provider::EnableBanking# @param psu_headers [Hash] Optional PSU context headers required by some ASPSPs# @return [Hash] Transactions and continuation_key for paginationdef get_account_transactions(account_id:, date_from: nil, date_to: nil,- continuation_key: nil, transaction_status: nil, psu_headers: {}, retried_date_from: false)+ continuation_key: nil, transaction_status: nil, psu_headers: {}, retry_attempt: 0)encoded_id = CGI.escape(account_id.to_s)query_params = {}query_params[:transaction_status] = transaction_status if transaction_status.present?@@ -180,17 +185,17 @@ class Provider::EnableBankinghandle_response(response)rescue EnableBankingError => e- corrected_date_from = e.corrected_date_from+ next_date_from = next_transactions_date_from(e, date_from, retry_attempt)- if !retried_date_from && e.wrong_transactions_period? && corrected_date_from.present? && corrected_date_from != date_from+ if next_date_fromget_account_transactions(account_id: account_id,- date_from: corrected_date_from,+ date_from: next_date_from,date_to: date_to,continuation_key: continuation_key,transaction_status: transaction_status,psu_headers: psu_headers,- retried_date_from: true+ retry_attempt: retry_attempt + 1)elseraise@@ -201,6 +206,35 @@ class Provider::EnableBankingprivate+ # Decides the next date_from to retry a transactions fetch with after an+ # ASPSP rejects the requested window with WRONG_TRANSACTIONS_PERIOD.+ #+ # Some ASPSPs (e.g. certain PT banks) return this error WITHOUT a corrected+ # date_from in the payload, which defeated the previous single-shot retry.+ # In that case we step through progressively shorter windows so the sync can+ # still succeed instead of surfacing a generic error. Returns nil when no+ # further retry should be attempted.+ def next_transactions_date_from(error, current_date_from, retry_attempt)+ return nil unless error.wrong_transactions_period?+ return nil if retry_attempt > FALLBACK_TRANSACTIONS_DATE_FROM_DAYS.length++ current = current_date_from&.to_date++ # Prefer the ASPSP-suggested date on the first retry (original behaviour),+ # but only when it actually moves the window forward.+ if retry_attempt.zero?+ corrected = error.corrected_date_from+ return corrected if corrected.present? && (current.nil? || corrected > current)+ end++ # Otherwise pick the first progressively-shorter window that advances the+ # window forward, skipping any window that is not newer than the current+ # date_from. Moving strictly forward guarantees progress and termination.+ FALLBACK_TRANSACTIONS_DATE_FROM_DAYS+ .map { |days| days.days.ago.to_date }+ .find { |candidate| current.nil? || candidate > current }+ end+def safe_psu_headers(headers)headers.except("Authorization", :Authorization, "Accept", :Accept, "Content-Type", :"Content-Type")enddiff --git a/test/models/provider/enable_banking_test.rb b/test/models/provider/enable_banking_test.rbindex f74f91b76c..62d853ca6a 100644--- a/test/models/provider/enable_banking_test.rb+++ b/test/models/provider/enable_banking_test.rb@@ -43,6 +43,62 @@ class Provider::EnableBankingTest < ActiveSupport::TestCaseassert_equal "2026-01-17", requested_queries.second[:date_from]end+ test "get_account_transactions falls back to a shorter window when no corrected date_from is given" do+ requested_queries = []++ # Some ASPSPs reject the period without suggesting a corrected date_from.+ validation_response = OpenStruct.new(+ code: 422,+ body: {+ error: "WRONG_TRANSACTIONS_PERIOD",+ detail: { message: "Requested time period out of bound." }+ }.to_json+ )+ success_response = OpenStruct.new(code: 200, body: { transactions: [] }.to_json)++ Provider::EnableBanking.expects(:get).twice.with do |_url, options|+ requested_queries << options[:query].dup+ true+ end.returns(validation_response, success_response)++ result = @provider.get_account_transactions(+ account_id: "acct_123",+ date_from: 6.months.ago.to_date,+ transaction_status: "BOOK"+ )++ assert_equal [], result[:transactions]+ assert_equal 6.months.ago.to_date.iso8601, requested_queries.first[:date_from]+ assert_equal 89.days.ago.to_date.iso8601, requested_queries.second[:date_from]+ end++ test "get_account_transactions skips fallback windows that do not advance the search" do+ requested_queries = []++ validation_response = OpenStruct.new(+ code: 422,+ body: { error: "WRONG_TRANSACTIONS_PERIOD", detail: { message: "out of bound" } }.to_json+ )+ success_response = OpenStruct.new(code: 200, body: { transactions: [] }.to_json)++ Provider::EnableBanking.expects(:get).twice.with do |_url, options|+ requested_queries << options[:query].dup+ true+ end.returns(validation_response, success_response)++ # A 45-day lookback is newer than the 89- and 60-day windows; only the+ # 30-day window moves the search forward, so it must be the one retried.+ result = @provider.get_account_transactions(+ account_id: "acct_123",+ date_from: 45.days.ago.to_date,+ transaction_status: "BOOK"+ )++ assert_equal [], result[:transactions]+ assert_equal 45.days.ago.to_date.iso8601, requested_queries.first[:date_from]+ assert_equal 30.days.ago.to_date.iso8601, requested_queries.second[:date_from]+ end+test "validation errors expose parsed response data" doresponse = OpenStruct.new(code: 422,