Skip to content

Enable Banking: keep repeated transaction references from failing a link - #5

Open
canyild wants to merge 2 commits into
nextwork-projects:mainfrom
canyild:fix/enablebanking-duplicate-ids
Open

canyild wants to merge 2 commits into
nextwork-projects:mainfrom
canyild:fix/enablebanking-duplicate-ids

Conversation

@canyild

@canyild canyild commented Nov 12, 2026

Copy link
Copy Markdown

What broke

Linking a real bank through Enable Banking failed with "That bank didn't connect." The server log showed:

duplicate key value violates unique constraint "transactions_plaid_transaction_id_key"
Key (plaid_transaction_id)=(enablebanking:<account uid>:2025-01-03.0) already exists.

The bank sent no transaction_id, so ebTxn used entry_reference. That bank reuses one reference for different rows on the same day. Both rows got the same id, the insert failed, and the whole link failed with it. It happened again on every retry.

The fix

  • feeds.ts: new uniqueIds(), run on Enable Banking rows. An exact repeat (same id, day, amount and text) is dropped. A different row with a taken id gets |amount|text added. That id comes out the same on every sync, so rows don't double up later. The first row keeps the bank's id, so rows already saved stay matched.
  • sync.ts: saveFeed keeps one row per id before inserting, so one bad row can't fail a link for any provider.
  • feeds.test.ts: covers dropped repeats, stable new ids, and same amount and text on another day.

Checked

  • node src/lib/providers/feeds.test.ts, npx tsc --noEmit and npm run lint on the changed files pass.
  • Linked a real bank account through Enable Banking with this change. Accounts and transactions came in.

🤖 Generated with Claude Code

Some banks send two different rows with the same reference on one day, or
the same row twice across pages. Both used the same id, so the insert hit
the unique key and the whole link failed.

Exact repeats are now dropped. A different row with a taken id gets its
amount and text added, which gives the same id on every sync. The shared
save step also drops repeated ids, so one bad row can't fail a link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@minimus7
minimus7 self-requested a review November 16, 2026 17:15

@minimus7 minimus7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix, the link works again. Two things in uniqueIds before merging.

Comment thread src/lib/providers/feeds.ts Outdated
const key = JSON.stringify([r.plaid_transaction_id, r.date, r.amount, r.name]);
if (same.has(key)) continue;
same.add(key);
if (!seen.has(r.plaid_transaction_id)) { seen.set(r.plaid_transaction_id, r); continue; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoever comes first keeps the bank id, so if the bank sends the two rows in the other order next sync, the ids swap. Tried it: [Coffee, Lunch] gives x = Coffee, but [Lunch, Coffee] gives x = Lunch. Coffee's saved row then gets Lunch's amount (keeping Coffee's category), and both show up again as new rows. Sorting rows that share an id by amount and name first would keep it stable.

Comment thread src/lib/providers/feeds.ts Outdated
* same id on every sync. The first row keeps the bank's id, so rows saved before this stay matched. */
export function uniqueIds(rows: FeedTxn[]): FeedTxn[] {
const seen = new Map<string, FeedTxn>();
const same = new Set<string>(); // bank id, day, amount and text: the same row sent again

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also drops two real identical purchases (two $5 coffees on the same day with the reused ref). Maybe only treat it as a repeat when it comes from a different page?

Comment thread src/lib/sync.ts Outdated
const accountId = new Map((saved ?? []).map((a) => [a.plaid_account_id, a.id]));
const txns = feed.filter((t) => accountId.has(t.account_ref));
// One row per id: a repeated id in one batch would fail the whole insert. Providers make their ids unique first.
const txns = [...new Map(feed.filter((t) => accountId.has(t.account_ref)).map((t) => [t.plaid_transaction_id, t])).values()];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine as a backstop. Could log when it drops something so we know which provider sent bad ids.

… page

Review follow-up on uniqueIds.

- Rows that share an id are ordered by day, amount and text before ids
  are given out, so each row keeps its id when the bank sends them in
  another order.
- A row is dropped as a repeat only when an earlier page already held
  it. The same row twice on one page is two real payments, so both stay.
- saveFeed logs how many rows it dropped for repeated ids. Count and
  provider only, no ids, amounts or text.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@canyild

canyild commented Nov 23, 2026

Copy link
Copy Markdown
Author

This reply and commit 6b869a4 were written by Claude (Claude Code) on canyild's behalf, not by canyild personally.

Thanks for the review. All three are in 6b869a4.

  • Ids swapping with order: rows that share an id are now ordered by day, amount and text before ids are given out. [Coffee, Lunch] and [Lunch, Coffee] give the same ids. There is a test for both orders.
  • Identical purchases: uniqueIds now takes the rows page by page. A row is dropped only when an earlier page already held it. Two $5 coffees on one page both stay.
  • Logging: saveFeed logs the provider and how many rows it dropped. No ids, amounts or text, since the ids can hold account numbers and payment text.

Two limits are left that I want to be open about:

  1. A row that joins later can still take the id. If Lunch books at noon it gets x. If Coffee books later with the same reference, Coffee sorts first and takes x, and Lunch moves to x|12|Lunch. The saved Lunch row is then overwritten and Lunch comes in again as new. Sorting fixes order, not a growing group.
  2. Twins split over a page break. Two identical rows on two different pages still look like a re-send, so one is dropped. Only when the break lands exactly between them.

A fix for 1 would be to never use the bare entry_reference as the id, and always build it from reference, amount and text. That changes the ids of rows already saved for banks without a transaction_id, so it needs a migration to rename them first. I left that out because it touches everyone's saved data. Happy to add it here or in a follow-up if you want it.

Checked with node src/lib/providers/feeds.test.ts, npx tsc --noEmit and lint on the changed files. Not run against a real bank again after this commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants