# Duplicate Detection Enhancement
## Summary
Updated the import conflict detection system to report **ALL duplicate entries**, including exact duplicates, providing full transparency when users reimport the same file.
## Changes Made
### 1. Tradebook Import (`equity/app/api/import/tradebook/route.ts`)
**Previous Behavior:**
- Only created conflicts when duplicate trade_id had **different** data
- Silently skipped exact duplicates
**New Behavior:**
- Creates conflicts for **both** different data AND exact duplicates
- Conflict types:
- `duplicate_trade_id` - Same trade_id but different quantity/price/symbol
- `exact_duplicate` - Identical trade already exists (likely reimport)
### 2. Ledger Import (`equity/app/api/import/ledger/route.ts`)
**Previous Behavior:**
- Silently skipped exact duplicate entries
- Only reported duplicates with different amounts
**New Behavior:**
- Creates conflicts for **all** duplicate entries
- Conflict types:
- `exact_duplicate` - Identical entry (same date, particular, debit, credit)
- `duplicate_entry_different_amount` - Same date/particular but different amounts
### 3. Conflicts Page UI (`equity/app/conflicts/page.tsx`)
**Enhancements:**
- Added visual indicator for exact duplicates (blue alert icon vs yellow warning)
- Clear "Exact Duplicate Entry" banner explaining the situation
- User-friendly conflict type labels:
- "Exact Duplicate (Reimport)"
- "Duplicate Trade ID (Different Data)"
- "Duplicate Entry (Different Amount)"
- Recommendation text for exact duplicates suggesting safe actions
- Shows side-by-side comparison for transparency
## User Benefits
### 1. Full Transparency
Users now see ALL conflicts, including when they accidentally reimport the same file, preventing confusion about what happened to their data.
### 2. Better Decision Making
- **Exact Duplicates**: Clearly marked with recommendation to "Keep Existing" or "Ignore"
- **Different Data**: Highlighted as needing user review to decide which version is correct
### 3. Prevent Data Issues
Catches scenarios like:
- Accidentally reimporting the same file multiple times
- Importing partially overlapping date ranges
- Broker providing corrected data for existing trades
## How It Works
### Import Flow
1. User uploads CSV file
2. System checks each entry against existing data
3. **Any match found** → Creates conflict (new behavior)
4. User reviews all conflicts on `/conflicts` page
5. User resolves each conflict:
- Keep Existing
- Use New (CSV)
- Ignore
- Delete conflict record
### Conflict Types
#### Exact Duplicate
```
✓ Same trade_id/date+particular
✓ Same quantity/amounts
✓ Same price
✓ Same all fields
```
**Action:** Typically "Keep Existing" or "Ignore"
#### Data Mismatch
```
✓ Same trade_id/date+particular
✗ Different quantity/amounts OR
✗ Different price/symbol
```
**Action:** Review and choose correct version
## Testing Recommendations
1. **Test Exact Duplicate Detection:**
- Import a tradebook CSV
- Import the same file again
- Verify conflicts are created with `exact_duplicate` type
2. **Test Modified Data:**
- Import a CSV
- Modify some quantities/prices in the CSV
- Reimport
- Verify conflicts show both versions side-by-side
3. **Test User Resolution:**
- Create conflicts
- Test all resolution actions (Keep, Use New, Ignore, Delete)
- Verify data state after each action
## Migration Notes
**Backward Compatible:** ✅
- No database schema changes required
- Existing conflict types still work
- Only adds new `exact_duplicate` conflict type
- No migration script needed
## Related Files
- `/equity/app/api/import/tradebook/route.ts` - Tradebook import logic
- `/equity/app/api/import/ledger/route.ts` - Ledger import logic
- `/equity/app/conflicts/page.tsx` - Conflicts review UI
- `/equity/app/api/conflicts/[id]/route.ts` - Conflict resolution API
- `/equity/app/api/conflicts/route.ts` - Conflicts list API
## Future Enhancements
Potential improvements:
1. Bulk resolution actions (e.g., "Ignore all exact duplicates")
2. Smart suggestions based on import dates
3. Conflict statistics on dashboard
4. Email notifications for conflicts
5. Auto-resolve exact duplicates after N days