fix: Bug #88 Extended - Use real Prisma IDs in execute endpoint
Main trade execution endpoint (TradingView signals) was creating synthetic
IDs (trade-${Date.now()}) instead of using real database IDs returned by
createTrade().
This caused identical failure pattern as smart-entry-timer:
- SL verification couldn't find trades in database
- Active recovery attempts failed (no record found)
- Emergency shutdown failed (no record found for update)
- Position left unprotected after system gave up
Fix in app/api/trading/execute/route.ts:
- Line 1044: Capture createTrade() return value as savedTrade
- Line 1127+: Update activeTrade.id = savedTrade.id (real Prisma ID)
- Added logging for database ID verification
- Added fallback handling for database save failures
Impact: All database operations (SL verification, recovery, emergency close)
now work correctly for TradingView signal trades.
Related: Bug #88 Phase 1 (smart-entry-timer fix, commit 674743c)
This commit is contained in:
@@ -1034,12 +1034,13 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
|||||||
console.log('🔍 DEBUG: Exit orders section complete, about to calculate quality score...')
|
console.log('🔍 DEBUG: Exit orders section complete, about to calculate quality score...')
|
||||||
|
|
||||||
// Save trade to database FIRST (CRITICAL: Must succeed before Position Manager)
|
// Save trade to database FIRST (CRITICAL: Must succeed before Position Manager)
|
||||||
|
let savedTrade
|
||||||
try {
|
try {
|
||||||
// Quality score already calculated earlier for adaptive leverage
|
// Quality score already calculated earlier for adaptive leverage
|
||||||
console.log('🔍 DEBUG: Using quality score from earlier calculation:', qualityResult.score)
|
console.log('🔍 DEBUG: Using quality score from earlier calculation:', qualityResult.score)
|
||||||
console.log('🔍 DEBUG: About to call createTrade()...')
|
console.log('🔍 DEBUG: About to call createTrade()...')
|
||||||
|
|
||||||
await createTrade({
|
savedTrade = await createTrade({
|
||||||
positionId: openResult.transactionSignature!,
|
positionId: openResult.transactionSignature!,
|
||||||
symbol: driftSymbol,
|
symbol: driftSymbol,
|
||||||
direction: body.direction,
|
direction: body.direction,
|
||||||
@@ -1074,6 +1075,7 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
|||||||
})
|
})
|
||||||
|
|
||||||
console.log('🔍 DEBUG: createTrade() completed successfully')
|
console.log('🔍 DEBUG: createTrade() completed successfully')
|
||||||
|
console.log(`💾 Execute: Trade saved to database (ID: ${savedTrade.id})`)
|
||||||
console.log(`💾 Trade saved with quality score: ${qualityResult.score}/100`)
|
console.log(`💾 Trade saved with quality score: ${qualityResult.score}/100`)
|
||||||
console.log(`📊 Quality reasons: ${qualityResult.reasons.join(', ')}`)
|
console.log(`📊 Quality reasons: ${qualityResult.reasons.join(', ')}`)
|
||||||
|
|
||||||
@@ -1110,6 +1112,10 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
|||||||
error: dbError instanceof Error ? dbError.message : 'Database save failed'
|
error: dbError instanceof Error ? dbError.message : 'Database save failed'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Fallback: Create placeholder with synthetic ID (prevents crash)
|
||||||
|
savedTrade = { id: `trade-${Date.now()}` } as any
|
||||||
|
console.log('⚠️ Using synthetic ID as fallback after DB error')
|
||||||
|
|
||||||
// CRITICAL: If database save fails, we MUST NOT add to Position Manager
|
// CRITICAL: If database save fails, we MUST NOT add to Position Manager
|
||||||
// Return error to user so they know to close manually
|
// Return error to user so they know to close manually
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -1122,6 +1128,10 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BUG #88 FIX: Use real database ID for Position Manager tracking
|
||||||
|
activeTrade.id = savedTrade.id
|
||||||
|
console.log(`📊 ADDTRADE: Using real database ID: ${savedTrade.id}`)
|
||||||
|
|
||||||
// Add to position manager for monitoring ONLY AFTER database save succeeds
|
// Add to position manager for monitoring ONLY AFTER database save succeeds
|
||||||
await positionManager.addTrade(activeTrade)
|
await positionManager.addTrade(activeTrade)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user