2026-05-25 15:13:49 +08:00

66 lines
1.7 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 async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session || !(await isCurrentSuperAdmin(session))) {
return NextResponse.json(
{ error: '仅最高权限账号可调整用户部门' },
{ status: 403 }
)
}
const { userId, department } = await req.json()
if (!userId) {
return NextResponse.json(
{ error: '缺少用户ID' },
{ status: 400 }
)
}
const targetUser = await prisma.user.findUnique({
where: { id: userId },
select: { isSuperAdmin: true },
})
if (!targetUser) {
return NextResponse.json(
{ error: '用户不存在' },
{ status: 404 }
)
}
if (targetUser.isSuperAdmin) {
return NextResponse.json(
{ error: '不能调整最高权限账号的部门' },
{ status: 403 }
)
}
// 使用事务更新用户和素材(如果需要同步更新素材信息)
const user = await prisma.user.update({
where: { id: userId },
data: { department: department || '' },
})
// 注意:部门信息通常不需要同步到素材,但如果将来需要,可以在这里添加
// 目前素材表中没有部门字段,所以不需要同步
return NextResponse.json(user)
} catch (error) {
console.error('[Update Department] Error:', error)
return NextResponse.json(
{ error: '更新部门失败' },
{ status: 500 }
)
}
}