diff --git a/app/api/trading/trades/route.ts b/app/api/trading/trades/route.ts index 266f639..8dbb504 100644 --- a/app/api/trading/trades/route.ts +++ b/app/api/trading/trades/route.ts @@ -21,19 +21,34 @@ export async function POST(request: NextRequest) { try { const body = await request.json() + // Convert camelCase to snake_case for Supabase + const trade = { + pair: body.pair || body.pair, + direction: body.direction, + entry_price: body.entryPrice || body.entry_price, + exit_price: body.exitPrice || body.exit_price, + status: body.status || 'open', + is_demo: body.isDemo ?? body.is_demo ?? true, + trader_style: body.traderStyle || body.trader_style, + setup: body.setup, + timeframe: body.timeframe, + pnl: body.pnl, + opened_at: new Date().toISOString(), + } + const { data, error } = await supabase .from('trades') - .insert([{ - ...body, - opened_at: new Date().toISOString(), - }]) + .insert([trade]) .select() - if (error) throw error + if (error) { + console.error('Supabase insert error:', error) + return NextResponse.json({ error: error.message }, { status: 500 }) + } return NextResponse.json({ success: true, trade: data?.[0] }) } catch (error) { - console.error('Supabase error:', error) + console.error('Error:', error) return NextResponse.json({ error: 'Failed to save' }, { status: 500 }) } }