feat: 支持配置微信用户已通关关卡
This commit is contained in:
@@ -4,7 +4,7 @@ import { auth } from '@/lib/auth'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
// GET /api/wx-users/[id] - Get single wx user with level progress
|
||||
// GET /api/wx-users/[id] - Get a wx user and all levels with completion state
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
@@ -22,14 +22,32 @@ export async function GET(
|
||||
|
||||
const user = await prisma.wxUser.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
select: {
|
||||
id: true,
|
||||
openid: true,
|
||||
sessionKey: true,
|
||||
nickname: true,
|
||||
avatarUrl: true,
|
||||
stamina: true,
|
||||
staminaUpdatedAt: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
levelProgress: {
|
||||
orderBy: { completedAt: 'desc' },
|
||||
include: {
|
||||
orderBy: [
|
||||
{ level: { sortOrder: 'asc' } },
|
||||
{ completedAt: 'desc' },
|
||||
],
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
levelId: true,
|
||||
completedAt: true,
|
||||
timeSpent: true,
|
||||
level: {
|
||||
select: {
|
||||
id: true,
|
||||
answer: true,
|
||||
sortOrder: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -38,13 +56,46 @@ export async function GET(
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ error: 'User not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
return NextResponse.json({ error: 'User not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(user)
|
||||
const levels = await prisma.level.findMany({
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
select: {
|
||||
id: true,
|
||||
answer: true,
|
||||
sortOrder: true,
|
||||
},
|
||||
})
|
||||
|
||||
const progressByLevelId = new Map(
|
||||
user.levelProgress.map((progress) => [progress.levelId, progress])
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
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.levelProgress.length,
|
||||
assignedLevels: levels.map((level) => {
|
||||
const progress = progressByLevelId.get(level.id)
|
||||
|
||||
return {
|
||||
id: level.id,
|
||||
answer: level.answer,
|
||||
sortOrder: level.sortOrder,
|
||||
completed: Boolean(progress),
|
||||
progressId: progress?.id || null,
|
||||
completedAt: progress?.completedAt || null,
|
||||
}
|
||||
}),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching wx user:', error)
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
// DELETE /api/wx-users/level-progress - Batch delete level progress
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
) {
|
||||
// PUT /api/wx-users/level-progress - Replace a user's completed levels
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
@@ -17,28 +16,101 @@ export async function DELETE(
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { ids } = await request.json()
|
||||
const body = await request.json()
|
||||
const { userId, levelIds } = body
|
||||
|
||||
if (!Array.isArray(ids) || ids.length === 0 || !ids.every((id) => typeof id === 'string')) {
|
||||
if (typeof userId !== 'string' || !userId) {
|
||||
return NextResponse.json({ error: 'userId is required' }, { status: 400 })
|
||||
}
|
||||
|
||||
if (!Array.isArray(levelIds) || !levelIds.every((id) => typeof id === 'string')) {
|
||||
return NextResponse.json(
|
||||
{ error: 'ids must be a non-empty array of strings' },
|
||||
{ error: 'levelIds must be an array of strings' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const result = await prisma.wxUserLevelProgress.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: ids,
|
||||
},
|
||||
},
|
||||
const uniqueLevelIds = Array.from(new Set(levelIds))
|
||||
|
||||
const [user, levels] = await Promise.all([
|
||||
prisma.wxUser.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true },
|
||||
}),
|
||||
prisma.level.findMany({
|
||||
where: { id: { in: uniqueLevelIds } },
|
||||
select: { id: true },
|
||||
}),
|
||||
])
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'User not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
if (levels.length !== uniqueLevelIds.length) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Some levels do not exist' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.wxUserLevelProgress.deleteMany({
|
||||
where: { userId },
|
||||
})
|
||||
|
||||
if (uniqueLevelIds.length > 0) {
|
||||
await tx.wxUserLevelProgress.createMany({
|
||||
data: uniqueLevelIds.map((levelId) => ({
|
||||
id: uuidv4(),
|
||||
userId,
|
||||
levelId,
|
||||
timeSpent: 0,
|
||||
})),
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ deleted: result.count })
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
completedLevelCount: uniqueLevelIds.length,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error deleting level progress:', error)
|
||||
console.error('Error updating level progress:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete level progress' },
|
||||
{ error: 'Failed to update level progress' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/wx-users/level-progress - Clear all completed levels for a user
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { userId } = body
|
||||
|
||||
if (typeof userId !== 'string' || !userId) {
|
||||
return NextResponse.json({ error: 'userId is required' }, { status: 400 })
|
||||
}
|
||||
|
||||
const result = await prisma.wxUserLevelProgress.deleteMany({
|
||||
where: { userId },
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, deleted: result.count })
|
||||
} catch (error) {
|
||||
console.error('Error clearing level progress:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to clear level progress' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { auth } from '@/lib/auth'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
// GET /api/wx-users - Get all wx users with optional search and pagination
|
||||
// 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({
|
||||
@@ -17,9 +17,6 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const search = searchParams.get('search') || ''
|
||||
const page = parseInt(searchParams.get('page') || '1', 10)
|
||||
const limit = parseInt(searchParams.get('limit') || '20', 10)
|
||||
const skip = (page - 1) * limit
|
||||
|
||||
const where = search
|
||||
? {
|
||||
@@ -30,32 +27,42 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
: {}
|
||||
|
||||
const [users, total] = await Promise.all([
|
||||
prisma.wxUser.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
openid: true,
|
||||
nickname: true,
|
||||
avatarUrl: true,
|
||||
points: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
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,
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.wxUser.count({ where }),
|
||||
])
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
users,
|
||||
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,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
total: users.length,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user