fix: Bug #88 - Use real Prisma IDs for smart entry trades

Smart entry timer was creating synthetic trade IDs (trade-${Date.now()})
instead of using real database IDs from createTrade() return value.

This caused SL verification to fail with Prisma error 'No record found
for update' when attempting to:
- Save SL recovery signatures (attemptSLPlacement)
- Mark trades as emergency closed (haltTradingAndClosePosition)

Fix:
- Capture createTrade() return value as savedTrade
- Use savedTrade.id for ActiveTrade object
- Add fallback for database save failures
- Log real database ID for verification

Impact: SL verification can now update smart entry trades successfully.
Both active recovery and emergency shutdown will work correctly.

Related: Bug #87 Phase 2 (active SL recovery)
This commit is contained in:
mindesbunister
2025-12-16 15:58:54 +01:00
parent cd2171386d
commit 674743c30f

View File

@@ -537,8 +537,9 @@ export class SmartEntryTimer {
}
// Save to database
let savedTrade
try {
await createTrade({
savedTrade = await createTrade({
positionId: openResult.transactionSignature,
symbol: signal.symbol,
direction: signal.direction,
@@ -576,7 +577,7 @@ export class SmartEntryTimer {
}
})
logger.log(`💾 Smart Entry: Trade saved to database`)
logger.log(`💾 Smart Entry: Trade saved to database (ID: ${savedTrade.id})`)
} catch (dbError) {
console.error(`❌ Smart Entry: Failed to save trade:`, dbError)
const { logCriticalError } = await import('../utils/persistent-logger')
@@ -587,6 +588,8 @@ export class SmartEntryTimer {
entryPrice: fillPrice,
transactionSignature: openResult.transactionSignature
})
// If database save fails, generate synthetic ID as fallback
savedTrade = { id: `trade-${Date.now()}` } as any
}
// Add to Position Manager
@@ -600,7 +603,7 @@ export class SmartEntryTimer {
)
const activeTrade: import('./position-manager').ActiveTrade = {
id: `trade-${Date.now()}`,
id: savedTrade.id, // 🔧 BUG #88 FIX: Use real Prisma ID from database
positionId: openResult.transactionSignature,
symbol: signal.symbol,
direction: signal.direction,