import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { auth } from '@/lib/auth' export const dynamic = 'force-dynamic' // GET /api/wx-users - Get all wx users with optional search and completion counts export async function GET(request: NextRequest) { try { const session = await auth.api.getSession({ headers: request.headers, }) if (!session) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { searchParams } = new URL(request.url) const search = searchParams.get('search') || '' const where = search ? { OR: [ { nickname: { contains: search } }, { openid: { contains: search } }, ], } : {} const users = await prisma.wxUser.findMany({ where, orderBy: { createdAt: 'desc' }, select: { id: true, openid: true, sessionKey: true, nickname: true, avatarUrl: true, stamina: true, staminaUpdatedAt: true, createdAt: true, updatedAt: true, _count: { select: { levelProgress: true, }, }, }, }) return NextResponse.json({ users: users.map((user) => ({ id: user.id, openid: user.openid, sessionKey: user.sessionKey, nickname: user.nickname, avatarUrl: user.avatarUrl, stamina: user.stamina, staminaUpdatedAt: user.staminaUpdatedAt, createdAt: user.createdAt, updatedAt: user.updatedAt, completedLevelCount: user._count.levelProgress, })), meta: { total: users.length, }, }) } catch (error) { console.error('Error fetching wx users:', error) return NextResponse.json( { error: 'Failed to fetch wx users' }, { status: 500 } ) } }