pummmp.fun/app/api/user/route.ts
2026-07-04 12:49:09 -07:00

48 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { connectToDatabase } from '@/lib/mongodb'
import { ObjectId } from 'mongodb'
export async function GET() {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { db } = await connectToDatabase()
const user = await db.collection('users').findOne({ _id: new ObjectId(session.user.id) })
if (!user) {
return NextResponse.json({ error: 'User not found' }, { status: 404 })
}
// Fetch user's miners
const miners = await db.collection('miners').find({
userId: session.user.id,
isActive: true
}).toArray()
let portfolioValue = 0
if (user.portfolio && user.portfolio.length > 0) {
for (const item of user.portfolio) {
const coin = await db.collection('coins').findOne({ _id: new ObjectId(item.coinId) })
if (coin) {
portfolioValue += item.amount * coin.price
}
}
}
return NextResponse.json({
...user,
miners,
portfolioValue,
totalValue: user.balance + portfolioValue,
})
} catch (error) {
console.error('Error fetching user:', error)
return NextResponse.json({ error: 'Failed to fetch user' }, { status: 500 })
}
}