mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* fix: deduplicate Enable Banking API transactions with different entry_reference IDs (#954) Enable Banking API sometimes returns the same logical transaction multiple times with different entry_reference values in a single response. This causes duplicate entries because the existing ID-based deduplication treats them as distinct transactions. Add content-based deduplication that compares (date, amount, currency, creditor, debtor, remittance_information, status) to detect and remove these API-level duplicates before storing them. The first occurrence is kept. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add Enable Banking processor and importer deduplication tests (#954) Add tests for: - EnableBankingEntry::Processor: verifies entry_reference fallback for external_id, idempotent re-processing, string key handling - EnableBankingItem::Importer: verifies content-based deduplication removes API-level duplicates while preserving legitimate distinct transactions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: handle nil values in remittance_information array for dedup key (#954) Call compact and map(&:to_s) before sort.join when remittance_information is an array, preventing ArgumentError when it contains nil elements. Also document the known limitation of content-based deduplication collapsing genuinely distinct identical transactions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add coverage for nil values in remittance_information array (#954) Verify that deduplication handles remittance_information arrays containing nil elements without raising ArgumentError, and correctly treats arrays with different nil positions but same non-nil content as duplicates. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: prefer transaction_id over content-based dedup to preserve legit duplicates (#954) When transaction_id is present, use it as the dedup key instead of falling back to content-based deduplication. This preserves legitimately distinct transactions with identical content (e.g. two laundromat payments of the same amount on the same day) while still deduplicating the Enable Banking duplicate entry_reference issue when transaction_id is nil. Addresses review feedback from @jjmata about legitimate duplicate transactions being incorrectly collapsed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: use composite key for dedup instead of transaction_id alone (#954) Per the Enable Banking API docs, transaction_id is not guaranteed to be unique. Include it as one component of the composite content key rather than using it as the sole dedup criterion. This preserves transactions with non-unique transaction_ids but different content, while still deduplicating true API-level duplicates. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add value_date fallback coverage for dedup key (#954) build_transaction_content_key falls back to value_date when booking_date is absent. This test exercises that path. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: document known limitation of content-based dedup (#954) When transaction_id is nil for both transactions, pure content comparison applies, which could theoretically collapse two genuinely distinct transactions with identical fields. Document this trade-off inline for future maintainers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: add credit_debit_indicator to dedup composite key (#954) transaction_amount.amount is always positive in the Enable Banking API, with direction encoded separately in credit_debit_indicator (CRDT/DBIT). Without it in the composite key, a payment and a same-day refund of the same amount to the same merchant would produce identical keys, silently dropping one transaction. - Add credit_debit_indicator to build_transaction_content_key - Add test for payment + same-day refund scenario - Update docstring to document the rationale Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
121 lines
3.9 KiB
Ruby
121 lines
3.9 KiB
Ruby
require "test_helper"
|
|
|
|
class EnableBankingEntry::ProcessorTest < ActiveSupport::TestCase
|
|
setup do
|
|
@family = families(:dylan_family)
|
|
@account = accounts(:depository)
|
|
@enable_banking_item = EnableBankingItem.create!(
|
|
family: @family,
|
|
name: "Test Enable Banking",
|
|
country_code: "DE",
|
|
application_id: "test_app_id",
|
|
client_certificate: "test_cert"
|
|
)
|
|
@enable_banking_account = EnableBankingAccount.create!(
|
|
enable_banking_item: @enable_banking_item,
|
|
name: "N26 Hauptkonto",
|
|
uid: "eb_uid_1",
|
|
currency: "EUR"
|
|
)
|
|
AccountProvider.create!(
|
|
account: @account,
|
|
provider: @enable_banking_account
|
|
)
|
|
end
|
|
|
|
test "uses entry_reference as external_id when transaction_id is nil" do
|
|
tx = {
|
|
entry_reference: "31e13269-03fc-11f1-89d2-cd465703551c",
|
|
transaction_id: nil,
|
|
booking_date: Date.current.to_s,
|
|
transaction_amount: { amount: "11.65", currency: "EUR" },
|
|
creditor: { name: "Spar Dankt 3418" },
|
|
credit_debit_indicator: "DBIT",
|
|
status: "BOOK"
|
|
}
|
|
|
|
assert_difference "@account.entries.count", 1 do
|
|
EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process
|
|
end
|
|
|
|
entry = @account.entries.find_by!(
|
|
external_id: "enable_banking_31e13269-03fc-11f1-89d2-cd465703551c",
|
|
source: "enable_banking"
|
|
)
|
|
assert_equal 11.65, entry.amount.to_f
|
|
assert_equal "EUR", entry.currency
|
|
end
|
|
|
|
test "uses transaction_id as external_id when present" do
|
|
tx = {
|
|
entry_reference: "ref_123",
|
|
transaction_id: "txn_456",
|
|
booking_date: Date.current.to_s,
|
|
transaction_amount: { amount: "25.00", currency: "EUR" },
|
|
creditor: { name: "Amazon" },
|
|
credit_debit_indicator: "DBIT",
|
|
status: "BOOK"
|
|
}
|
|
|
|
EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process
|
|
|
|
entry = @account.entries.find_by!(external_id: "enable_banking_txn_456", source: "enable_banking")
|
|
assert_equal 25.0, entry.amount.to_f
|
|
end
|
|
|
|
test "does not create duplicate when same entry_reference is processed twice" do
|
|
tx = {
|
|
entry_reference: "unique_ref_abc",
|
|
transaction_id: nil,
|
|
booking_date: Date.current.to_s,
|
|
transaction_amount: { amount: "50.00", currency: "EUR" },
|
|
creditor: { name: "Rewe" },
|
|
credit_debit_indicator: "DBIT",
|
|
status: "BOOK"
|
|
}
|
|
|
|
assert_difference "@account.entries.count", 1 do
|
|
EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process
|
|
end
|
|
|
|
assert_no_difference "@account.entries.count" do
|
|
EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process
|
|
end
|
|
end
|
|
|
|
test "raises ArgumentError when both transaction_id and entry_reference are nil" do
|
|
tx = {
|
|
transaction_id: nil,
|
|
entry_reference: nil,
|
|
booking_date: Date.current.to_s,
|
|
transaction_amount: { amount: "10.00", currency: "EUR" },
|
|
creditor: { name: "Test" },
|
|
credit_debit_indicator: "DBIT",
|
|
status: "BOOK"
|
|
}
|
|
|
|
assert_raises(ArgumentError) do
|
|
EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process
|
|
end
|
|
end
|
|
|
|
test "handles string keys in transaction data" do
|
|
tx = {
|
|
"entry_reference" => "string_key_ref",
|
|
"transaction_id" => nil,
|
|
"booking_date" => Date.current.to_s,
|
|
"transaction_amount" => { "amount" => "15.00", "currency" => "EUR" },
|
|
"creditor" => { "name" => "Lidl" },
|
|
"credit_debit_indicator" => "DBIT",
|
|
"status" => "BOOK"
|
|
}
|
|
|
|
assert_difference "@account.entries.count", 1 do
|
|
EnableBankingEntry::Processor.new(tx, enable_banking_account: @enable_banking_account).process
|
|
end
|
|
|
|
entry = @account.entries.find_by!(external_id: "enable_banking_string_key_ref", source: "enable_banking")
|
|
assert_equal 15.0, entry.amount.to_f
|
|
end
|
|
end
|