The personal finance app for everyone (by everyone)
Ruby
Replace stored Enable Banking rows the bank redelivers with corrected data (#5358)
GFR6aa8cf2186ad
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rbindex 178079f2b4..d7ecfa7086 100644--- a/app/models/enable_banking_item/importer.rb+++ b/app/models/enable_banking_item/importer.rb@@ -1035,12 +1035,24 @@ class EnableBankingItem::Importerendend- # Only a fetched row describing the same movement supersedes a stored one.- # A row the response left out may lie outside the window the ASPSP served,- # or on a page that was never fetched.+ # Only a fetched row describing the same movement, or carrying the same id+ # (the ASPSP corrected its amount, remittance or counterparty), supersedes+ # a stored one. A row the response left out may lie outside the window the+ # ASPSP served, or on a page that was never fetched.ids_by_row = external_ids_by_row(existing_transactions + all_transactions)superseded_rows = content_pairs(existing_transactions, all_transactions, ids_by_row, ids_by_row).map(&:first)superseded_set = Set.new.compare_by_identity.merge(superseded_rows)+ stored_by_id = existing_transactions.group_by { |row| ids_by_row[row] }+ fetched_by_id = all_transactions.group_by { |row| ids_by_row[row] }+ existing_transactions.each do |row|+ id = ids_by_row[row]+ next if id.blank? || superseded_set.include?(row)+ next unless stored_by_id[id].one? && fetched_by_id[id]&.one?+ next unless redelivered_correction?(row, fetched_by_id[id].first)++ superseded_rows << row+ superseded_set << row+ endretained_transactions = existing_transactions.reject { |tx| superseded_set.include?(tx) }next_snapshot, incoming_transactions = merge_transaction_rows(@@ -1254,6 +1266,21 @@ class EnableBankingItem::Importerrows.zip(EnableBankingEntry::Processor.external_ids_for(rows)).to_hend+ # A fetched row sharing a stored row's id is a corrected redelivery only when+ # nothing marks it as another movement. A pending row never replaces a booked+ # one. Rows without an entry_reference can share an id through a reused+ # transaction_id alone, so they must also share a date.+ def redelivered_correction?(stored_row, fetched_row)+ return false if pending_row?(fetched_row) && !pending_row?(stored_row)++ stored_reference = stored_row.with_indifferent_access[:entry_reference].to_s+ fetched_reference = fetched_row.with_indifferent_access[:entry_reference].to_s+ return stored_reference == fetched_reference if stored_reference.present? || fetched_reference.present?++ stored_date = row_date(stored_row)+ stored_date.present? && stored_date == row_date(fetched_row)+ end+def merge_transaction_rows(existing_transactions, retained_transactions, fetched_transactions, ids_by_row)retained_ids = retained_transactions.map { |row| ids_by_row[row] }.compact.to_setincoming_transactions = fetched_transactions.select do |row|diff --git a/test/models/enable_banking_item/importer_pdng_test.rb b/test/models/enable_banking_item/importer_pdng_test.rbindex aa54e557d4..4800b83ebd 100644--- a/test/models/enable_banking_item/importer_pdng_test.rb+++ b/test/models/enable_banking_item/importer_pdng_test.rb@@ -269,4 +269,131 @@ class EnableBankingItem::ImporterPdngTest < ActiveSupport::TestCaseassert_equal 1, stored.sizeassert_equal "settled_ref", stored.first.with_indifferent_access[:entry_reference]end++ test "fetch_and_store_transactions replaces a stored row the ASPSP redelivers with corrected data" do+ @importer.stubs(:include_pending?).returns(false)+ # Inside the 30-day window determine_sync_start_date uses once rows are stored.+ recent_booking_date = 5.days.ago.to_date.iso8601+ row = ->(reference, remittance) {+ {+ entry_reference: reference,+ transaction_id: "id_#{reference}",+ booking_date: recent_booking_date,+ transaction_amount: { amount: "10.00", currency: "EUR" },+ credit_debit_indicator: "DBIT",+ remittance_information: [ remittance ],+ status: "BOOK"+ }+ }++ @enable_banking_account.update!(raw_transactions_payload: [ row.call("first", "Rent"), row.call("second", "Old text") ])++ @importer.stubs(:fetch_paginated_transactions).with(@enable_banking_account, has_entries(transaction_status: "BOOK"))+ .returns([ [ row.call("third", "New"), row.call("second", "Corrected text") ], false ])++ result = @importer.send(:fetch_and_store_transactions, @enable_banking_account)++ assert result[:success]+ stored = @enable_banking_account.reload.raw_transactions_payload.map(&:with_indifferent_access)+ assert_equal %w[first second third], stored.map { |tx| tx[:entry_reference] },+ "stored rows keep their order and new rows are appended"+ assert_equal [ "Corrected text" ], stored.second[:remittance_information]+ end++ test "fetch_and_store_transactions skips the snapshot write when a refetch changes nothing" do+ @importer.stubs(:include_pending?).returns(false)+ recent_booking_date = 5.days.ago.to_date.iso8601+ stored_tx = {+ entry_reference: "tx_ref",+ transaction_id: "tx_id",+ booking_date: recent_booking_date,+ transaction_amount: { amount: "10.00", currency: "EUR" },+ credit_debit_indicator: "DBIT",+ creditor_account: { iban: "DE89370400440532013000" }, # pipelock:ignore IBAN+ status: "BOOK"+ }+ @enable_banking_account.update!(raw_transactions_payload: [ stored_tx ])+ @enable_banking_account.reload++ @importer.stubs(:fetch_paginated_transactions).with(@enable_banking_account, has_entries(transaction_status: "BOOK")).returns([ [ stored_tx.deep_dup ], false ])+ @enable_banking_account.expects(:upsert_enable_banking_transactions_snapshot!).never++ result = @importer.send(:fetch_and_store_transactions, @enable_banking_account)++ assert result[:success]+ end++ test "fetch_and_store_transactions keeps a stored booked row when a pending row redelivers its ids" do+ @importer.stubs(:include_pending?).returns(true)+ booked = {+ entry_reference: "r1",+ transaction_id: "t1",+ booking_date: 5.days.ago.to_date.iso8601,+ transaction_amount: { amount: "10.00", currency: "EUR" },+ credit_debit_indicator: "DBIT",+ remittance_information: [ "Booked" ],+ status: "BOOK"+ }+ @enable_banking_account.update!(raw_transactions_payload: [ booked ])++ pending = booked.merge(remittance_information: [ "Pending" ], status: "PDNG")+ @importer.stubs(:fetch_paginated_transactions).with(@enable_banking_account, has_entries(transaction_status: "BOOK")).returns([ [], false ])+ @importer.stubs(:fetch_paginated_transactions).with(@enable_banking_account, has_entries(transaction_status: "PDNG")).returns([ [ pending ], false ])++ result = @importer.send(:fetch_and_store_transactions, @enable_banking_account)++ assert result[:success]+ stored = @enable_banking_account.reload.raw_transactions_payload.map(&:with_indifferent_access)+ assert_equal 1, stored.size+ assert_equal [ "Booked" ], stored.first[:remittance_information]+ assert_not stored.first[:_pending]+ end++ test "fetch_and_store_transactions keeps a reference-less stored row when a row on another date reuses its transaction_id" do+ @importer.stubs(:include_pending?).returns(false)+ row = ->(days_ago, amount) {+ {+ transaction_id: "reused",+ booking_date: days_ago.days.ago.to_date.iso8601,+ transaction_amount: { amount: amount, currency: "EUR" },+ credit_debit_indicator: "DBIT",+ status: "BOOK"+ }+ }+ @enable_banking_account.update!(raw_transactions_payload: [ row.call(10, "10.00") ])++ @importer.stubs(:fetch_paginated_transactions).with(@enable_banking_account, has_entries(transaction_status: "BOOK"))+ .returns([ [ row.call(3, "25.00") ], false ])++ result = @importer.send(:fetch_and_store_transactions, @enable_banking_account)++ assert result[:success]+ stored = @enable_banking_account.reload.raw_transactions_payload.map(&:with_indifferent_access)+ assert_includes stored.map { |tx| tx.dig(:transaction_amount, :amount) }, "10.00",+ "the older movement is not overwritten by a row that only shares its transaction_id"+ end++ test "fetch_and_store_transactions replaces a reference-less stored row redelivered on the same date" do+ @importer.stubs(:include_pending?).returns(false)+ row = ->(remittance) {+ {+ transaction_id: "only_id",+ booking_date: 5.days.ago.to_date.iso8601,+ transaction_amount: { amount: "10.00", currency: "EUR" },+ credit_debit_indicator: "DBIT",+ remittance_information: [ remittance ],+ status: "BOOK"+ }+ }+ @enable_banking_account.update!(raw_transactions_payload: [ row.call("Old text") ])++ @importer.stubs(:fetch_paginated_transactions).with(@enable_banking_account, has_entries(transaction_status: "BOOK"))+ .returns([ [ row.call("Corrected text") ], false ])++ result = @importer.send(:fetch_and_store_transactions, @enable_banking_account)++ assert result[:success]+ stored = @enable_banking_account.reload.raw_transactions_payload.map(&:with_indifferent_access)+ assert_equal [ [ "Corrected text" ] ], stored.map { |tx| tx[:remittance_information] }+ endend