Commit Graph
6 Commits
Author SHA1 Message Date
GFRandClaude Sonnet 5 0252787dc0 fix(enable-banking): use real merchant instead of POS terminal line for name (#2968)
* fix(enable-banking): use real merchant instead of POS terminal line for name

Some ASPSPs (e.g. BankDirekt/Raiffeisen in Austria) return
remittance_information as a multi-element array where the first line is a
generic card terminal descriptor (\"POS   45,13 AT  D6   31.07. 10:27\")
and a later line holds the real merchant. EnableBankingEntry::Processor
always used the first array element, so transaction names showed the
terminal string instead of the merchant.

primary_remittance_information now skips lines that look like a technical
terminal booking (POS/ATM + amount, or a trailing date+time stamp) and
prefers the first descriptive line, falling back to the original element
when nothing better is available. It also strips known small-merchant
payment-processor prefixes (SumUp, Square, iZettle, PayPal) from the
selected line.

Fixes #2935

Disclosure: this fix was written by Claude Code, verified against the
reporter's real (decrypted) Enable Banking payload and against test-stack
Rails test / RuboCop / Brakeman runs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(enable-banking): require both technical-line signals together

Address CodeRabbit/Codex review feedback on #2968: the POS/ATM+amount
prefix and the trailing date+time suffix were OR'd, so either alone
could misclassify a legitimate line as technical. A line embedding the
merchant right after the amount (e.g. "POS 45,13 BILLA DANKT ...") or
a legitimate descriptor that happens to end in a timestamp (e.g.
"Invoice paid 31.07. 10:27") would have been wrongly skipped.

Both signals are now required together in a single anchored pattern,
matching every real technical line observed in production while no
longer misclassifying either of the scenarios above.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(enable-banking): tighten date-stamp regex and clean up the merchant line

Addresses two review threads on this PR:

1. jjmata (PR review): technical_remittance_line?'s date-stamp check required
   exactly 2-digit day/month (\d{2}[./]\d{2}), so an un-padded date ("1.07."
   instead of "01.07.") wasn't recognized as technical and the line would
   resurface as the transaction name -- reproducing the original #2935 bug for
   that date shape. Now accepts 1-2 digits for both.

2. john-frandsen (issue #2935 comment): suggested cleaning up the merchant line
   further (e.g. "BILLA DANKT 0007114 SIEGENDORF 7011" -> "Billa"). Checked
   point 1 (structured remittance fields) against Enable Banking's own API
   docs -- no such field exists there, not applicable. Points 2/3/5 already
   match current behavior. Point 4 (loyalty-marker cleanup) implemented as two
   layers:
   - Primary: match the line against merchants the family already knows
     (Family#known_merchant_names) -- self-maintaining, no pattern-guessing,
     and now also assigns the transaction's merchant when matched (previously
     out of scope for blank-counterparty EB transactions). Case-insensitive,
     regex-escaped, longest-match-wins, with a minimum length guard against
     spurious short-name matches.
   - Fallback (no known merchant yet): remove only the "DANKT"/"DANKE"
     thank-you marker word itself, not a directional truncation -- the marker
     can precede or follow the merchant name depending on phrasing ("X DANKT"
     vs. "DANKE ... bei X"), so truncating at it risked deleting the real
     merchant name in one of the two phrasings.

Verified against the full test suite, RuboCop, and Brakeman on a test-stack
Rails instance (0 RuboCop offenses, 0 Brakeman warnings; full-suite failures
present on that instance are pre-existing/environmental and unrelated to
these files).

Disclosure: this fix (investigation, implementation, and tests) was written
by Claude Code.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(enable-banking): address CodeRabbit/Codex review on merchant-line cleanup

Follow-up to 5e39ee6b, in response to automated review on that push:

- CodeRabbit (functional correctness): remittance cleanup was order-dependent
  -- strip_payment_processor_prefix ran before strip_loyalty_marker, so a
  marker preceding the (start-anchored) processor-prefix pattern would leave
  the prefix unstripped. Swapped the order (marker removal first) so the
  result no longer depends on which came first in the input. Also switched
  strip_loyalty_marker from sub to gsub so it removes every marker
  occurrence, not just the first.

- CodeRabbit + Codex (performance, independently flagged by both): Family#
  known_merchant_names was re-queried for every transaction in a sync batch,
  since EnableBankingAccount::Transactions::Processor creates a new
  EnableBankingEntry::Processor per row. Added an optional
  known_merchant_names: keyword to the processor's constructor (same pattern
  already used for the shared import_adapter) and compute it once per batch
  in the caller instead of once per row.

- CodeRabbit (test quality, nitpick): the known_merchant_names test only used
  distinct names, so `assert_equal names.uniq, names` couldn't actually catch
  a deduplication bug, and never exercised the documented
  recently-unlinked-merchants exclusion. Rewrote it to create a genuine
  duplicate name across two different merchant records and to assign-then-
  unlink a merchant, asserting it's excluded from known_merchant_names while
  still present in available_merchants (the deliberate difference between the
  two methods).

Re-verified test/models/enable_banking_entry/processor_test.rb +
test/models/family_test.rb (67 runs, 170 assertions, 0 failures) and RuboCop
on all changed files on a test-stack instance.

Disclosure: this fix was written by Claude Code.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(enable-banking): drop DANKT/DANKE loyalty-marker stripping

Country-specific fallback heuristic flagged in review (only recognized
German "thank you" markers). The core fix (skipping the technical POS/
ATM line and matching against the family's known merchant list) is
already market-independent; this text heuristic only helped cosmetically
for the first transaction from a not-yet-known German/Austrian merchant.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-22 23:52:35 +02:00
Otter 1c6d018b6a Fix transactions posting a day early when booked around midnight. (#2744)
* Fix 2668

* Fix CodeRabbit nitpick

* Fix Akahu parsing

* Anchor provider transaction date parsing to family timezone

* Coderabbit suggestion for Date/DateTime order

* Remove duplicate condition in wise

* Safe unless column_exists + pass family to date parse to family components
2026-07-30 02:29:13 +02:00
CrossDrain 33bc6b59c8 fix(enable-banking): import transactions missing transaction_id and entry_reference (#1767)
* fix(enable-banking): handle transactions missing transaction_id and entry_reference

Some ASPSPs omit both transaction_id and entry_reference from their transaction payloads, which is valid per the PSD2/Berlin Group spec. Previously, every such transaction raised an ArgumentError and was silently dropped during sync.

compute_external_id now falls back to a deterministic MD5 fingerprint (prefixed enable_banking_content_) derived from date, amount, currency, direction, counterparty, and remittance info. This fingerprint is stable across re-syncs, so duplicate imports are still correctly prevented. An ArgumentError is only raised for truly empty/unidentifiable payloads.

The importer is updated in three places to use compute_external_id
consistently: the pending pre-filter (before combining with booked),
the C4 stored-pending cleanup, and the new_transactions dedup. This means ID-less pending entries are now also removed when their settled booked counterpart arrives.

Tests cover compute_external_id directly (all 5 cases), end-to-end
fingerprint import, idempotency, and importer storage/dedup behaviour for ID-less transactions including the pending→booked settlement path.

* fix(enable-banking): implement dual-strategy matching for transaction settlement

When a stored pending row had only entry_reference (no transaction_id) and
the settled BOOK row arrived with a new transaction_id, compute_external_id
produced different fingerprints for each side (enable_banking_<ref> vs
enable_banking_<txn_id>). The fingerprint-only comparison introduced in the
previous commit never matched, leaving the stale pending entry in
raw_transactions_payload. Both rows were then imported as separate visible
transactions.

Restore a book_entry_refs set alongside book_fingerprints in both the
pending pre-filter and the C4 stored-pending cleanup. A pending entry is
now removed when either its fingerprint or its entry_reference matches a
booked counterpart — covering same-ID settlement, content-fingerprint
settlement, and the entry_reference cross-match settlement path.

Also updates the ArgumentError message in external_id to accurately
reflect that transaction_id, entry_reference, and content fingerprint
are all accepted identifiers, and aligns build_transaction_content_key
to use transaction_date as a fallback (matching compute_external_id).

Adds a regression test that stores a pending-only row and asserts it is removed when the booked counterpart arrives with a new transaction_id.
2026-05-12 00:17:49 +02:00
d415672247 EnableBanking: use remittance for CARD-* names and merchants (#1478)
* EnableBanking: skip CARD-* counterparty in name

# Conflicts:
#	test/models/enable_banking_entry/processor_test.rb

# Conflicts:
#	test/models/enable_banking_entry/processor_test.rb

* Fix whitespace in remittance_information array

Whitespace added before 'ACME SHOP' in remittance_information.

Signed-off-by: Juan José Mata <jjmata@jjmata.com>

* Fix merchant creation for Wise and prefer remittance for Entry name if counterparty is CARD-XXX

* Fix review

* Handle scalars

* Handle empty strings

* Fix review

* Make truncate not use ellipsis at the end

---------

Signed-off-by: Juan José Mata <jjmata@jjmata.com>
Co-authored-by: quentinreytinas <quentin@reytinas.fr>
Co-authored-by: Juan José Mata <jjmata@jjmata.com>
2026-04-16 17:44:42 +02:00
LouisandJuan José Mata e96fb0c23f feat(enable-banking): enhance transaction import, metadata handling, and UI (#1406)
* feat(enable-banking): enhance transaction import, metadata handling, and UI

* fix(enable-banking): address security, sync edge cases and PR feedback

* fix(enable-banking): resolve silent failures, auth overrides, and sync logic bugs

* fix(enable-banking): resolve sync logic bugs, trailing whitespaces, and apply safe_psu_headers

* test(enable-banking): mock set_current_balance to return success result

* fix(budget): properly filter pending transactions and classify synced loan payments

* style: fix trailing whitespace detected by rubocop

* refactor: address code review feedback for Enable Banking sync and reporting

---------

Signed-off-by: Louis <contact@boul2gom.com>
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-04-10 23:19:48 +02:00
0xRozierandClaude Opus 4.6 005d2fac20 Fix/issue 954 enable banking duplicate transactions (#988)
* 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>
2026-03-28 16:53:30 +01:00