76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { auth } from '@/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user || session.user.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const page = parseInt(searchParams.get('page') || '1')
|
|
const limit = parseInt(searchParams.get('limit') || '10')
|
|
const search = searchParams.get('search')
|
|
const role = searchParams.get('role')
|
|
const skip = (page - 1) * limit
|
|
|
|
const where: any = {}
|
|
|
|
if (search) {
|
|
where.OR = [
|
|
{ name: { contains: search, mode: 'insensitive' } },
|
|
{ email: { contains: search, mode: 'insensitive' } },
|
|
]
|
|
}
|
|
|
|
if (role) {
|
|
where.role = role
|
|
}
|
|
|
|
const [users, total] = await Promise.all([
|
|
prisma.user.findMany({
|
|
where,
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
role: true,
|
|
isActive: true,
|
|
joinedAt: true,
|
|
referralCode: true,
|
|
referrer: {
|
|
select: {
|
|
name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
_count: {
|
|
select: {
|
|
referrals: true,
|
|
orders: true,
|
|
},
|
|
},
|
|
},
|
|
skip,
|
|
take: limit,
|
|
orderBy: { joinedAt: 'desc' },
|
|
}),
|
|
prisma.user.count({ where }),
|
|
])
|
|
|
|
return NextResponse.json({
|
|
users,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
pages: Math.ceil(total / limit),
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('Admin users API error:', error)
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
} |