Replace stored Enable Banking rows the bank redelivers with corrected data (#5358)

## What this PR does

When Enable Banking delivers a transaction again with the same id but
corrected data (amount, remittance text, counterparty), the corrected
version now replaces the stored row in the raw transactions snapshot.

Before, the importer paired stored and fetched rows only by content. A
row that kept its id but changed its content was not paired, so the
stored row was retained and the fetched correction was dropped as
already stored. The processor kept reading the old data forever.

Now a fetched row whose id matches a stored row also supersedes it.
Stored rows keep their position, new rows are appended, and the snapshot
is only rewritten when it actually changed. Rows that share a
`transaction_id` are still told apart by `entry_reference`, as before.

This came out of the review of the IBAN series (#3511), where it was
split into its own PR.

## Validation

- New tests in `test/models/enable_banking_item/importer_pdng_test.rb`.
Two of them fail without this change: a corrected row replaces the
stored one in place, and a reference-less row redelivered on the same
date replaces the stored one. The other three are guards that also pass
on main: an unchanged refetch skips the snapshot write, a pending row
does not replace a booked one, and a reference-less row on another date
that reuses a transaction_id does not replace the stored row.
- Full Enable Banking model tests green, CI green.

## Known limitation

Rows without an entry_reference are matched by transaction_id and
booking date only. If a bank reuses a transaction_id for a different
payment on the same day, the stored entry is now replaced by the new
one. Before this change, the new row was dropped instead. In both cases
one movement is lost, because a correction and a reuse look the same
without a reference. A separate identity for reference-less rows is
planned as a follow-up.

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Fixes the Enable Banking importer so a transaction the bank redelivers
with corrected data replaces the stored row.

Before, rows were paired only by content, so a refetch of the same id
with a changed amount, remittance text, or counterparty was dropped as
already stored and the processor kept reading the old data. Now a
fetched row supersedes a stored row with the same id only when the id is
unique on both sides, the `entry_reference` matches (or, without one,
the date matches), and a pending row is not replacing a booked one.
Stored rows keep their order, new rows are appended, and the snapshot is
only rewritten when it actually changed.

**Validation**
- New model tests cover corrected replacements, unchanged refetches
skipping the snapshot write, and the pending and reused-id cases that
must not replace a stored row.

<sup>Written for commit 28d7c0b7eb.
Summary will update on new commits.</sup>

<a
href="https://cubic.dev/pr/we-promise/sure/pull/5358?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

<!-- End of auto-generated description by cubic. -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Refetched transactions now update stored entries when their matching
external ID and other identifying details confirm they are corrections,
while preserving existing entry order. Pending transactions no longer
overwrite booked entries, and reused IDs are distinguished by entry
reference or booking date.
  * Unchanged transaction refetches no longer create a new snapshot.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
GFR
2026-11-21 00:03:05 -08:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 92abc3ac6f
commit 6aa8cf2186
2 changed files with 157 additions and 3 deletions
+30 -3
View File
@@ -1035,12 +1035,24 @@ class EnableBankingItem::Importer
end
end
# 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
end
retained_transactions = existing_transactions.reject { |tx| superseded_set.include?(tx) }
next_snapshot, incoming_transactions = merge_transaction_rows(
@@ -1254,6 +1266,21 @@ class EnableBankingItem::Importer
rows.zip(EnableBankingEntry::Processor.external_ids_for(rows)).to_h
end
# 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_set
incoming_transactions = fetched_transactions.select do |row|
@@ -269,4 +269,131 @@ class EnableBankingItem::ImporterPdngTest < ActiveSupport::TestCase
assert_equal 1, stored.size
assert_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] }
end
end