Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix: OFX parsing logic (#2)

gmiranda13f168485e14b8
diff --git a/.gitignore b/.gitignore
index 6c848a3a..c1ad6614 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
.worktrees/
.DS_Store
.playwright-mcp/
+.claude
docs/plans/
diff --git a/backend/app/schemas/transaction.py b/backend/app/schemas/transaction.py
index 21572aaa..9bd851d4 100644
--- a/backend/app/schemas/transaction.py
+++ b/backend/app/schemas/transaction.py
@@ -13,6 +13,7 @@ class TransactionBase(BaseModel):
amount: Decimal
date: _Date
type: str # debit, credit
+ external_id: Optional[str] = None
class TransactionCreate(TransactionBase):
@@ -38,7 +39,6 @@ class TransactionRead(TransactionBase):
account_id: Optional[uuid.UUID] = None
category_id: Optional[uuid.UUID] = None
category: Optional[CategoryRead] = None
- external_id: Optional[str] = None
currency: str = "BRL"
source: str
status: str = "posted"
diff --git a/backend/app/services/import_service.py b/backend/app/services/import_service.py
index b8788fd6..cce4616d 100644
--- a/backend/app/services/import_service.py
+++ b/backend/app/services/import_service.py
@@ -27,6 +27,7 @@ def parse_ofx(content: bytes) -> list[TransactionBase]:
amount=abs(Decimal(str(txn.amount))),
date=txn.date.date() if hasattr(txn.date, 'date') else txn.date,
type="credit" if txn.amount > 0 else "debit",
+ external_id=getattr(txn, 'id', None),
))
return transactions
@@ -322,16 +323,25 @@ async def import_transactions(
imported = 0
skipped = 0
for txn_data in transactions:
- # Duplicate detection: check for existing transaction with same key fields
- existing = await session.execute(
- select(Transaction).where(
- Transaction.account_id == account_id,
- Transaction.date == txn_data.date,
- Transaction.amount == txn_data.amount,
- Transaction.type == txn_data.type,
- Transaction.description == txn_data.description,
+ # Duplicate detection: use external_id when available (OFX FITID),
+ # fall back to field-based matching for formats without unique IDs
+ if txn_data.external_id:
+ existing = await session.execute(
+ select(Transaction).where(
+ Transaction.account_id == account_id,
+ Transaction.external_id == txn_data.external_id,
+ )
+ )
+ else:
+ existing = await session.execute(
+ select(Transaction).where(
+ Transaction.account_id == account_id,
+ Transaction.date == txn_data.date,
+ Transaction.amount == txn_data.amount,
+ Transaction.type == txn_data.type,
+ Transaction.description == txn_data.description,
+ )
)
- )
if existing.scalar_one_or_none():
skipped += 1
continue
@@ -345,6 +355,7 @@ async def import_transactions(
type=txn_data.type,
source=source,
import_id=import_log.id,
+ external_id=txn_data.external_id,
)
session.add(transaction)
await session.flush()
diff --git a/backend/tests/test_import_service.py b/backend/tests/test_import_service.py
index 8441e502..35e396ea 100644
--- a/backend/tests/test_import_service.py
+++ b/backend/tests/test_import_service.py
@@ -3,7 +3,7 @@ from decimal import Decimal
import pytest
-from app.services.import_service import parse_csv, parse_qif, parse_camt
+from app.services.import_service import parse_csv, parse_ofx, parse_qif, parse_camt
class TestParseCsv:
@@ -412,3 +412,94 @@ class TestParseCamt:
assert len(transactions) == 1
assert transactions[0].description == "No NS"
assert transactions[0].amount == Decimal("300.00")
+
+
+class TestParseOfx:
+ """Tests for the parse_ofx function."""
+
+ def _make_ofx(self, transactions_sgml: str) -> bytes:
+ """Helper to wrap transaction SGML in a valid OFX structure."""
+ return (
+ "OFXHEADER:100\n"
+ "DATA:OFXSGML\n"
+ "VERSION:102\n"
+ "SECURITY:NONE\n"
+ "ENCODING:USASCII\n"
+ "CHARSET:1252\n"
+ "COMPRESSION:NONE\n"
+ "OLDFILEUID:NONE\n"
+ "NEWFILEUID:NONE\n"
+ "\n"
+ "<OFX>\n"
+ "<SIGNONMSGSRSV1>\n"
+ "<SONRS>\n"
+ "<STATUS><CODE>0<SEVERITY>INFO</STATUS>\n"
+ "<DTSERVER>20260101\n"
+ "<LANGUAGE>POR\n"
+ "</SONRS>\n"
+ "</SIGNONMSGSRSV1>\n"
+ "<BANKMSGSRSV1>\n"
+ "<STMTTRNRS>\n"
+ "<TRNUID>1001\n"
+ "<STATUS><CODE>0<SEVERITY>INFO</STATUS>\n"
+ "<STMTRS>\n"
+ "<CURDEF>BRL\n"
+ "<BANKACCTFROM>\n"
+ "<BANKID>0001\n"
+ "<ACCTID>12345\n"
+ "<ACCTTYPE>CHECKING\n"
+ "</BANKACCTFROM>\n"
+ "<BANKTRANLIST>\n"
+ "<DTSTART>20260101\n"
+ "<DTEND>20260131\n"
+ f"{transactions_sgml}\n"
+ "</BANKTRANLIST>\n"
+ "</STMTRS>\n"
+ "</STMTTRNRS>\n"
+ "</BANKMSGSRSV1>\n"
+ "</OFX>\n"
+ ).encode("ascii")
+
+ def test_parse_ofx_extracts_fitid(self):
+ """FITID from OFX transactions populates external_id."""
+ ofx = self._make_ofx(
+ "<STMTTRN>\n"
+ "<TRNTYPE>DEBIT\n"
+ "<DTPOSTED>20260115\n"
+ "<TRNAMT>-985.50\n"
+ "<FITID>TXN001ABC\n"
+ "<MEMO>PIX ENVIADO - FULANO\n"
+ "</STMTTRN>\n"
+ )
+ transactions = parse_ofx(ofx)
+
+ assert len(transactions) == 1
+ assert transactions[0].external_id == "TXN001ABC"
+ assert transactions[0].amount == Decimal("985.50")
+ assert transactions[0].type == "debit"
+
+ def test_parse_ofx_keeps_duplicate_looking_transactions(self):
+ """Transactions with same fields but different FITIDs are both kept."""
+ ofx = self._make_ofx(
+ "<STMTTRN>\n"
+ "<TRNTYPE>DEBIT\n"
+ "<DTPOSTED>20260115\n"
+ "<TRNAMT>-985.50\n"
+ "<FITID>FITID_001\n"
+ "<MEMO>PIX ENVIADO - FULANO\n"
+ "</STMTTRN>\n"
+ "<STMTTRN>\n"
+ "<TRNTYPE>DEBIT\n"
+ "<DTPOSTED>20260115\n"
+ "<TRNAMT>-985.50\n"
+ "<FITID>FITID_002\n"
+ "<MEMO>PIX ENVIADO - FULANO\n"
+ "</STMTTRN>\n"
+ )
+ transactions = parse_ofx(ofx)
+
+ assert len(transactions) == 2
+ assert transactions[0].external_id == "FITID_001"
+ assert transactions[1].external_id == "FITID_002"
+ assert transactions[0].amount == transactions[1].amount
+ assert transactions[0].description == transactions[1].description