WuYongJie d1c6efc79d
Some checks failed
CI Check / check (push) Has been cancelled
feat: NAS 部署与 Harbor 推送流程
2025-12-03 14:39:06 +08:00

78 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
import { ensureMaterialDownloadLogsTable } from '@/lib/material-download-log'
export const dynamic = 'force-dynamic'
/**
* 重置素材排行榜相关数据:
* - 清空点赞记录表 material_likes
* - 清空下载日志表 material_download_logs
* - 将 materials 表中的 likeCount / downloadCount 归零
*
* 仅管理员可用(ADMIN 或 isSuperAdmin)
*/
export async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
const user = session?.user as any
const isAdmin = user?.role === 'ADMIN' || Boolean(user?.isSuperAdmin)
if (!session || !isAdmin) {
return NextResponse.json(
{ error: '需要管理员权限' },
{ status: 403 }
)
}
// 确保下载日志表存在(避免 TRUNCATE 报错)
await ensureMaterialDownloadLogsTable()
// 使用事务,确保一致性
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 }
)
}
}