115 lines
3.8 KiB
JavaScript
115 lines
3.8 KiB
JavaScript
import { NextResponse } from 'next/server'
|
|
import { PrismaClient } from '@prisma/client'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
export async function GET() {
|
|
try {
|
|
// Get recent AI decisions from database (last 10)
|
|
const decisions = await prisma.ai_learning_data.findMany({
|
|
where: {
|
|
// Get learning data with actual analysis
|
|
analysisData: {
|
|
not: null
|
|
}
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
},
|
|
take: 10,
|
|
select: {
|
|
id: true,
|
|
symbol: true,
|
|
confidenceScore: true,
|
|
analysisData: true,
|
|
createdAt: true,
|
|
sessionId: true,
|
|
tradeId: true
|
|
}
|
|
})
|
|
|
|
// Transform database records to match expected format
|
|
const liveDecisions = decisions.map(record => {
|
|
let analysisData = {}
|
|
try {
|
|
analysisData = typeof record.analysisData === 'string'
|
|
? JSON.parse(record.analysisData)
|
|
: record.analysisData || {}
|
|
} catch (e) {
|
|
analysisData = {}
|
|
}
|
|
|
|
return {
|
|
type: 'AI_DECISION',
|
|
action: analysisData.recommendation?.toUpperCase() || analysisData.decision?.toUpperCase() || 'HOLD',
|
|
symbol: record.symbol,
|
|
blocked: !(analysisData.recommendation?.toLowerCase().includes('buy') ||
|
|
analysisData.recommendation?.toLowerCase().includes('sell') ||
|
|
analysisData.decision?.toLowerCase().includes('execute')),
|
|
executed: false, // Always false for analysis decisions
|
|
confidence: record.confidenceScore || analysisData.confidence || 0,
|
|
entryPrice: analysisData.aiLevels?.entry || analysisData.entry?.price || analysisData.currentPrice || 0,
|
|
stopLoss: analysisData.aiLevels?.stopLoss || analysisData.stopLoss?.price || analysisData.stopLoss || null,
|
|
takeProfit: analysisData.aiLevels?.takeProfit || analysisData.takeProfits?.tp1?.price || analysisData.takeProfit || null,
|
|
reasoning: analysisData.reasoning || analysisData.summary || 'AI market analysis',
|
|
timestamp: record.createdAt.toISOString(),
|
|
sessionId: record.sessionId,
|
|
tradeId: record.tradeId
|
|
}
|
|
})
|
|
|
|
const response = {
|
|
success: true,
|
|
decisions: liveDecisions,
|
|
latest: liveDecisions[0] || null,
|
|
timestamp: new Date().toISOString()
|
|
}
|
|
|
|
return NextResponse.json(response)
|
|
} catch (error) {
|
|
console.error('❌ Live decisions API error:', error)
|
|
return NextResponse.json(
|
|
{ success: false, error: error.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function POST(request) {
|
|
try {
|
|
const data = await request.json()
|
|
|
|
// Store AI decision in database for persistence using ai_learning_data table
|
|
await prisma.ai_learning_data.create({
|
|
data: {
|
|
id: `live_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
userId: 'system', // Use system user for automated decisions
|
|
symbol: data.symbol || 'SOLUSD',
|
|
confidenceScore: data.confidence || 0,
|
|
analysisData: data, // Store full decision data as JSON
|
|
marketConditions: {
|
|
timeframes: data.timeframes || ['1h', '4h'],
|
|
strategy: 'automation',
|
|
timestamp: new Date().toISOString()
|
|
},
|
|
sessionId: data.sessionId || `session_${Date.now()}`,
|
|
tradeId: data.learningDecisionId || data.tradeId || `trade_${Date.now()}`,
|
|
timeframe: (data.timeframes && data.timeframes[0]) || '1h',
|
|
createdAt: new Date()
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Decision stored persistently in database',
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
} catch (error) {
|
|
console.error('❌ Error storing live decision:', error)
|
|
return NextResponse.json(
|
|
{ success: false, error: error.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|