72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { isCurrentSuperAdmin } from '@/lib/admin-permissions'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
/**
|
|
* 重置素材排行榜相关数据:
|
|
* - 清空点赞记录表 material_likes
|
|
* - 清空下载日志表 material_download_logs
|
|
* - 将 materials 表中的 likeCount / downloadCount 归零
|
|
*
|
|
* 仅最高权限账号可用。
|
|
*/
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || !(await isCurrentSuperAdmin(session))) {
|
|
return NextResponse.json(
|
|
{ error: '仅最高权限账号可重置素材排行榜数据' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
// 使用事务,确保一致性
|
|
const result = await prisma.$transaction(async (tx) => {
|
|
// 清空点赞记录
|
|
const clearedLikes = await tx.$executeRawUnsafe(`
|
|
TRUNCATE TABLE material_likes RESTART IDENTITY CASCADE
|
|
`)
|
|
|
|
// 清空下载日志
|
|
const clearedDownloadLogs = await tx.$executeRawUnsafe(`
|
|
TRUNCATE TABLE material_download_logs RESTART IDENTITY CASCADE
|
|
`)
|
|
|
|
// 归零计数(全部素材统一归零)
|
|
const resetCounts = await tx.$executeRawUnsafe(`
|
|
UPDATE materials
|
|
SET "likeCount" = 0,
|
|
"downloadCount" = 0
|
|
`)
|
|
|
|
return {
|
|
clearedLikes: Number(clearedLikes),
|
|
clearedDownloadLogs: Number(clearedDownloadLogs),
|
|
resetCounts: Number(resetCounts),
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '素材点赞/下载数据已重置',
|
|
details: result,
|
|
})
|
|
} catch (error) {
|
|
console.error('[Admin Reset Material Rankings] Error:', error)
|
|
return NextResponse.json(
|
|
{
|
|
error: '重置素材排行榜数据失败',
|
|
details: error instanceof Error ? error.message : '未知错误',
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
|