OCR & Portfolio Import
#5 OCR Import Broke My Journal: Fixing Merge Logic Without Deleting History
· Build Log
OCR import moved from destructive replace behavior to append/merge with dedup safety.
OCR merge logictrade journal preservationdedup key
1) TL;DR
- OCR import originally overwrote existing journal-linked records.
- The root cause was replace-style localStorage/state handling.
- Append/merge with dedup preserved manual and OCR history together.
2) What I Tried
The first OCR flow optimized for quick import throughput.
3) What Broke
Existing journal and coach data disappeared after OCR imports.
4) Root Cause
Import logic replaced arrays in storage instead of merging into current state.
5) Before (Code Path)
public/buysell.html
- trades = ocrTrades
- storage overwrite with OCR array
- render path lost prior records
6) After (Code Path)
public/buysell.html
+ normalizeOcrRowToTrade()
+ mergeTradesPreservingExisting()
+ mergeJournalPreservingExisting()
7) Evidence (Git History)
- 78527b3 fix(main): preserve OCR journal data.
- 2cccca4 fix(buysell): average unit price for OCR imports.
- 3a44eba fix(buysell): duplicate choice flow during OCR import.
8) What I Learned
Import-by-default should be append-safe when users keep historical trading journals.
9) Frequently Asked Questions
Why is replace dangerous here?
It destroys context used by downstream journal and AI coach widgets.
How is duplicate prevention handled?
By composite keys across symbol, quantity, price, date, and source.
What changed in UX?
Existing records remain visible while imported records are added safely.