The personal finance app for everyone (by everyone)
Ruby
Clean up Wise import
Juan José Mataae7a00dc9219
diff --git a/app/models/wise_item/importer.rb b/app/models/wise_item/importer.rbindex 8c5fc9e372..b8a693e59d 100644--- a/app/models/wise_item/importer.rb+++ b/app/models/wise_item/importer.rb@@ -146,6 +146,7 @@ class WiseItem::Importercutoff = transfer_cutoffactivities = []cursor = nil+ seen_cursors = Set.newloop dotrace_wise_fetch(operation: "activities", status: "started", level: "info", cursor: cursor, page_size: 100)@@ -167,8 +168,23 @@ class WiseItem::Importerbreak if old_ones.any? || batch.size < 100- cursor = result["cursor"]- break if cursor.nil?+ next_cursor = result["cursor"].presence+ break if next_cursor.nil?++ if seen_cursors.include?(next_cursor)+ trace_wise_fetch(+ operation: "activities",+ status: "failed",+ level: "warn",+ cursor: cursor,+ next_cursor: next_cursor,+ error: StandardError.new("Wise activities pagination cursor did not advance")+ )+ break+ end++ seen_cursors << next_cursor+ cursor = next_cursorendactivities.uniq { |a| a["id"] }@@ -428,6 +444,8 @@ class WiseItem::Importermessage = "#{message} for wise_account #{wise_account.id}" if wise_accountmessage = "#{message}: #{error.message}" if error+ return Rails.logger.debug(message) unless error+Rails.logger.public_send(level, message)DebugLogEntry.capture(category: error ? "provider_sync_error" : "provider_sync",diff --git a/app/views/wise_items/_wise_item.html.erb b/app/views/wise_items/_wise_item.html.erbindex 29739f8cc6..be8b4be244 100644--- a/app/views/wise_items/_wise_item.html.erb+++ b/app/views/wise_items/_wise_item.html.erb@@ -14,9 +14,6 @@<div class="pl-1 text-sm"><div class="flex items-center gap-2"><%= tag.p wise_item.name, class: "font-medium text-primary" %>- <span class="text-xs px-1.5 py-0.5 rounded bg-surface-inset text-secondary">- <%= t("wise_items.profile_types.#{wise_item.profile_type}") %>- </span><% if wise_item.scheduled_for_deletion? %><%= tag.p t(".deletion_in_progress"), class: "text-destructive text-sm animate-pulse" %><% end %>diff --git a/test/models/wise_item/importer_test.rb b/test/models/wise_item/importer_test.rbindex 50249d4648..bbbd1fb0a2 100644--- a/test/models/wise_item/importer_test.rb+++ b/test/models/wise_item/importer_test.rb@@ -98,7 +98,10 @@ class WiseItem::ImporterTest < ActiveSupport::TestCasetest "imports STANDARD balances and creates WiseAccount records" doprovider = FakeWiseProvider.new- result = WiseItem::Importer.new(@wise_item, wise_provider: provider).import+ result = nil+ assert_no_difference -> { DebugLogEntry.where(provider_key: "wise").count } do+ result = WiseItem::Importer.new(@wise_item, wise_provider: provider).import+ endassert result[:success]assert_equal 1, result[:accounts_created]@@ -507,6 +510,20 @@ class WiseItem::ImporterTest < ActiveSupport::TestCaseassert_empty standard.raw_transactions_payload.select { |a| a["type"] == "BALANCE_CASHBACK" }end+ test "stops activity pagination when Wise repeats a cursor" do+ activity = build_interbalance("To <strong>Jar</strong>", resource_id: "5001")+ repeated_cursor = "repeated-cursor"+ provider = FakeWiseProvider.new+ provider.define_singleton_method(:get_activities) do |_profile_id, cursor: nil, size: 100|+ calls << :get_activities+ { "activities" => Array.new(size, activity), "cursor" => repeated_cursor }+ end++ WiseItem::Importer.new(@wise_item, wise_provider: provider).import++ assert_equal 2, provider.calls.count(:get_activities)+ end+# Returns failed result when balances fetch failstest "returns failed result when STANDARD balances fetch fails" do