diff --git a/app/api/automation/live-decisions/route.js b/app/api/automation/live-decisions/route.js index 0a021c1..e86a9f2 100644 --- a/app/api/automation/live-decisions/route.js +++ b/app/api/automation/live-decisions/route.js @@ -1,12 +1,57 @@ import { NextResponse } from 'next/server' +import { PrismaClient } from '@prisma/client' -// In-memory store for live trading decisions and risk management blocks -let liveDecisions = [] -let maxDecisions = 10 // Keep last 10 decisions +const prisma = new PrismaClient() export async function GET() { try { - // Return the most recent decisions with full context + // Get recent AI decisions from database (last 10) + const decisions = await prisma.ai_analysis_sessions.findMany({ + where: { + // Get sessions with actual analysis data + lastAnalysisData: { + not: null + } + }, + orderBy: { + createdAt: 'desc' + }, + take: 10, + select: { + id: true, + symbol: true, + confidence: true, + lastAnalysisData: true, + createdAt: true, + sessionId: true + } + }) + + // Transform database records to match expected format + const liveDecisions = decisions.map(session => { + let analysisData = {} + try { + analysisData = JSON.parse(session.lastAnalysisData || '{}') + } catch (e) { + analysisData = {} + } + + return { + type: 'AI_DECISION', + action: analysisData.recommendation?.toUpperCase() || 'HOLD', + symbol: session.symbol, + blocked: !(analysisData.recommendation?.toLowerCase().includes('buy') || analysisData.recommendation?.toLowerCase().includes('sell')), + executed: false, // Always false for analysis decisions + confidence: session.confidence || analysisData.confidence || 0, + entryPrice: analysisData.entry?.price || analysisData.currentPrice || 0, + stopLoss: analysisData.stopLoss?.price || analysisData.stopLoss || null, + takeProfit: analysisData.takeProfits?.tp1?.price || analysisData.takeProfit || null, + reasoning: analysisData.reasoning || analysisData.summary || 'AI market analysis', + timestamp: session.createdAt.toISOString(), + sessionId: session.sessionId + } + }) + const response = { success: true, decisions: liveDecisions, @@ -26,31 +71,27 @@ export async function GET() { export async function POST(request) { try { - const decision = await request.json() + const data = await request.json() - // Add timestamp if not provided - if (!decision.timestamp) { - decision.timestamp = new Date().toISOString() - } - - // Add to the beginning of the array (most recent first) - liveDecisions.unshift(decision) - - // Keep only the last maxDecisions - if (liveDecisions.length > maxDecisions) { - liveDecisions = liveDecisions.slice(0, maxDecisions) - } - - console.log('📊 Live decision recorded:', { - type: decision.type, - action: decision.action, - blocked: decision.blocked, - confidence: decision.confidence + // Store AI decision in database for persistence + await prisma.ai_analysis_sessions.create({ + data: { + id: `live_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, + symbol: data.symbol || 'SOLUSD', + confidence: data.confidence || 0, + lastAnalysisData: JSON.stringify(data), + sessionId: data.sessionId || `session_${Date.now()}`, + createdAt: new Date() + } }) - return NextResponse.json({ success: true, total: liveDecisions.length }) + return NextResponse.json({ + success: true, + message: 'Decision stored persistently', + timestamp: new Date().toISOString() + }) } catch (error) { - console.error('❌ Live decisions POST error:', error) + console.error('❌ Error storing live decision:', error) return NextResponse.json( { success: false, error: error.message }, { status: 500 } diff --git a/prisma/prisma/dev.db b/prisma/prisma/dev.db index bbd0ebe..f5be870 100644 Binary files a/prisma/prisma/dev.db and b/prisma/prisma/dev.db differ