A local-first personal finance app
[AI] Skip unimportable Enable Banking transactions instead of failing the whole sync (#8086)
mheiland67ea8d9ad050
diff --git a/packages/sync-server/src/app-enablebanking/app-enablebanking.ts b/packages/sync-server/src/app-enablebanking/app-enablebanking.tsindex 308d27320b..c13cfd5bb6 100644--- a/packages/sync-server/src/app-enablebanking/app-enablebanking.ts+++ b/packages/sync-server/src/app-enablebanking/app-enablebanking.ts@@ -16,6 +16,7 @@ import type {} from './services/enablebanking-service';import {enableBankingService,+ isImportableTransaction,normalizeAccount,normalizeBalance,normalizeTransaction,@@ -527,6 +528,20 @@ app.post(for (const tx of rawTransactions) {const normalized = normalizeTransaction(tx);++ // Drop records Actual's client can't insert (empty/non-ISO date or+ // non-numeric amount) — one of them would otherwise abort the entire+ // account sync. Pending transactions that carry a date are unaffected.+ if (!isImportableTransaction(normalized)) {+ debug(+ 'Skipping unimportable transaction id=%s date=%s amount=%s',+ normalized.transactionId,+ normalized.date,+ normalized.transactionAmount.amount,+ );+ continue;+ }+all.push(normalized);if (normalized.booked) {booked.push(normalized);diff --git a/packages/sync-server/src/app-enablebanking/services/enablebanking-service.ts b/packages/sync-server/src/app-enablebanking/services/enablebanking-service.tsindex 6e7a79a36f..4c8f514f95 100644--- a/packages/sync-server/src/app-enablebanking/services/enablebanking-service.ts+++ b/packages/sync-server/src/app-enablebanking/services/enablebanking-service.ts@@ -264,6 +264,26 @@ export function normalizeTransaction(};}+const IMPORTABLE_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;++// Actual's client imports a transaction by inserting it into a local SQLite+// database: the `date` column is a required integer derived from an ISO date,+// and the amount must be numeric. A record with an empty / non-ISO date or a+// non-numeric amount makes that insert throw, which aborts the whole account+// sync. Enable Banking occasionally returns such records — e.g. a pending+// transaction with no booking/value/transaction date — so callers skip them+// instead of failing the entire import.+export function isImportableTransaction(tx: BankSyncTransaction): boolean {+ // Trim and reject empty amounts explicitly: Number('') is 0 (finite), so an+ // empty/whitespace amount would otherwise slip through as a zero transaction.+ const amount = tx.transactionAmount.amount.trim();+ return (+ IMPORTABLE_DATE_REGEX.test(tx.date) &&+ amount !== '' &&+ Number.isFinite(Number(amount))+ );+}+export function normalizeBalance(bal: EnableBankingBalance): BankSyncBalance {const amount = Math.round(parseFloat(bal.balance_amount.amount) * 100);return {diff --git a/packages/sync-server/src/app-enablebanking/services/tests/fixtures.ts b/packages/sync-server/src/app-enablebanking/services/tests/fixtures.tsindex e82d470b1e..90d03f0e4f 100644--- a/packages/sync-server/src/app-enablebanking/services/tests/fixtures.ts+++ b/packages/sync-server/src/app-enablebanking/services/tests/fixtures.ts@@ -107,6 +107,16 @@ export const mockTransactionMinimal = {status: 'BOOK',} satisfies EnableBankingTransaction;+// A pending transaction with no booking/value/transaction date. Enable Banking+// returns these for some pending entries; normalizeTransaction maps it to+// date: '', which Actual's client cannot insert.+export const mockPendingTransactionNoDate = {+ transaction_id: 'tx-no-date',+ transaction_amount: { currency: 'EUR', amount: '-7.50' },+ status: 'PDNG',+ remittance_information: ['Card payment, not yet booked'],+} satisfies EnableBankingTransaction;+export const mockBalance = {balance_amount: { currency: 'EUR', amount: '1234.56' },balance_type: 'CLAV',diff --git a/packages/sync-server/src/app-enablebanking/services/tests/normalization.spec.ts b/packages/sync-server/src/app-enablebanking/services/tests/normalization.spec.tsindex 46d0836b96..7c15c64dbd 100644--- a/packages/sync-server/src/app-enablebanking/services/tests/normalization.spec.ts+++ b/packages/sync-server/src/app-enablebanking/services/tests/normalization.spec.ts@@ -1,6 +1,7 @@import { describe, expect, it } from 'vitest';import {+ isImportableTransaction,normalizeAccount,normalizeBalance,normalizeTransaction,@@ -12,6 +13,7 @@ import {mockDebitTransaction,mockNegativeBalance,mockPendingTransaction,+ mockPendingTransactionNoDate,mockSessionAccount,mockSessionAccountMinimal,mockSessionAccountNoName,@@ -96,6 +98,54 @@ describe('normalizeTransaction', () => {});});+describe('isImportableTransaction', () => {+ it('accepts a normal booked transaction (ISO date + numeric amount)', () => {+ expect(+ isImportableTransaction(normalizeTransaction(mockCreditTransaction)),+ ).toBe(true);+ });++ it('accepts a pending transaction that carries a date (no regression)', () => {+ expect(+ isImportableTransaction(normalizeTransaction(mockPendingTransaction)),+ ).toBe(true);+ });++ it('rejects a pending transaction with no date (the case that aborts the sync)', () => {+ const normalized = normalizeTransaction(mockPendingTransactionNoDate);+ expect(normalized.date).toBe('');+ expect(normalized.booked).toBe(false);+ expect(isImportableTransaction(normalized)).toBe(false);+ });++ it('rejects a non-ISO date', () => {+ const normalized = normalizeTransaction({+ ...mockCreditTransaction,+ booking_date: '03/01/2026',+ value_date: undefined,+ transaction_date: undefined,+ });+ expect(isImportableTransaction(normalized)).toBe(false);+ });++ it('rejects a non-numeric amount', () => {+ const normalized = normalizeTransaction({+ ...mockCreditTransaction,+ transaction_amount: { currency: 'EUR', amount: 'n/a' },+ });+ expect(isImportableTransaction(normalized)).toBe(false);+ });++ it('rejects an empty amount (Number("") would coerce to 0)', () => {+ const normalized = normalizeTransaction({+ ...mockCreditTransaction,+ transaction_amount: { currency: 'EUR', amount: '' },+ });+ expect(normalized.transactionAmount.amount).toBe('');+ expect(isImportableTransaction(normalized)).toBe(false);+ });+});+describe('SEPA prefix stripping', () => {it('strips EREF+ from a single line', () => {const tx = {diff --git a/packages/sync-server/src/app-enablebanking/tests/poll-auth.spec.ts b/packages/sync-server/src/app-enablebanking/tests/poll-auth.spec.tsindex bdc98947ec..1635930671 100644--- a/packages/sync-server/src/app-enablebanking/tests/poll-auth.spec.ts+++ b/packages/sync-server/src/app-enablebanking/tests/poll-auth.spec.ts@@ -450,6 +450,7 @@ describe('Enable Banking Express routes', () => {entry_reference: 'tx-1',transaction_amount: { currency: 'EUR', amount: '10.00' },status: 'BOOK',+ booking_date: '2026-01-05',},],continuation_key: 'page-2',@@ -461,6 +462,7 @@ describe('Enable Banking Express routes', () => {entry_reference: 'tx-2',transaction_amount: { currency: 'EUR', amount: '20.00' },status: 'BOOK',+ booking_date: '2026-01-06',},],});diff --git a/packages/sync-server/src/app-enablebanking/tests/transactions.spec.ts b/packages/sync-server/src/app-enablebanking/tests/transactions.spec.tsnew file mode 100644index 0000000000..96048ae01e--- /dev/null+++ b/packages/sync-server/src/app-enablebanking/tests/transactions.spec.ts@@ -0,0 +1,115 @@+import express from 'express';+import request from 'supertest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import {+ mockDebitTransaction,+ mockPendingTransaction,+ mockPendingTransactionNoDate,+} from '#app-enablebanking/services/tests/fixtures';++// Mock all external dependencies before importing the app (mirrors+// poll-auth.spec.ts).+vi.mock('../../services/secrets-service', () => ({+ SecretName: {+ enablebanking_applicationId: 'enablebanking_applicationId',+ enablebanking_secretKey: 'enablebanking_secretKey',+ },+ secretsService: {+ get: vi.fn(() => 'test-value'),+ set: vi.fn(),+ },+}));++vi.mock('../utils/jwt', () => ({+ getJWT: vi.fn(() => 'mock-jwt-token'),+}));++vi.mock('../../util/middlewares', () => ({+ requestLoggerMiddleware: (_req: unknown, _res: unknown, next: () => void) =>+ next(),+ validateSessionMiddleware: (_req: unknown, _res: unknown, next: () => void) =>+ next(),+}));++vi.mock('../../app-gocardless/util/handle-error', () => ({+ handleError:+ (fn: Function) =>+ (req: unknown, res: { send: (data: unknown) => void }) => {+ Promise.resolve(fn(req, res)).catch((err: Error) => {+ res.send({+ status: 'ok',+ data: {+ error_code: 'INTERNAL_ERROR',+ error_type: err.message || 'internal-error',+ },+ });+ });+ },+}));++// Mock fetch globally (the service is stubbed below, this just guards stray calls)+vi.stubGlobal('fetch', vi.fn());++const { handlers } = await import('../app-enablebanking');+const { enableBankingService } =+ await import('../services/enablebanking-service');++const app = express();+app.set('trust proxy', true);+app.use(express.json());+app.use('/', handlers);++const ids = (txns: Array<{ transactionId: string }>) =>+ txns.map(t => t.transactionId);++describe('POST /transactions', () => {+ beforeEach(() => {+ vi.spyOn(enableBankingService, 'getBalances').mockResolvedValue({+ balances: [],+ });+ });++ afterEach(() => {+ vi.restoreAllMocks();+ });++ it('skips a date-less pending transaction while keeping valid booked and pending ones', async () => {+ // mockPendingTransactionNoDate normalizes to date '' — the record that would+ // otherwise abort the whole sync when Actual's client tries to insert it.+ vi.spyOn(enableBankingService, 'getAllTransactions').mockResolvedValue([+ mockDebitTransaction,+ mockPendingTransaction,+ mockPendingTransactionNoDate,+ ]);++ const res = await request(app)+ .post('/transactions')+ .send({ accountId: 'acc-1', startDate: '2026-03-01' });++ expect(res.body.status).toBe('ok');++ const { all, booked, pending } = res.body.data.transactions;+ // The unimportable record is dropped (proves the fix)...+ expect(ids(all)).toEqual(['ref-002', 'tx-003']);+ expect(ids(all)).not.toContain('tx-no-date');+ // ...while the valid booked and valid (dated) pending entries still import+ // and bucket correctly (no regression).+ expect(ids(booked)).toEqual(['ref-002']);+ expect(ids(pending)).toEqual(['tx-003']);+ });++ it('keeps every transaction when all are importable', async () => {+ vi.spyOn(enableBankingService, 'getAllTransactions').mockResolvedValue([+ mockDebitTransaction,+ mockPendingTransaction,+ ]);++ const res = await request(app)+ .post('/transactions')+ .send({ accountId: 'acc-1', startDate: '2026-03-01' });++ expect(res.body.data.transactions.all).toHaveLength(2);+ expect(ids(res.body.data.transactions.all)).toEqual(['ref-002', 'tx-003']);+ });+});diff --git a/upcoming-release-notes/8086.md b/upcoming-release-notes/8086.mdnew file mode 100644index 0000000000..664a5280a6--- /dev/null+++ b/upcoming-release-notes/8086.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [mheiland]+---++Enable Banking: skip transactions that can't be imported (no date, or a non-numeric amount) instead of failing the entire account sync.