54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
// 更新用户部门
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '权限不足' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { userId, department } = await req.json()
|
|
|
|
console.log('[Update Department] Request:', { userId, department })
|
|
|
|
if (!userId) {
|
|
return NextResponse.json(
|
|
{ error: '缺少用户ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 使用事务更新用户和素材(如果需要同步更新素材信息)
|
|
const user = await prisma.user.update({
|
|
where: { id: userId },
|
|
data: { department: department || '' },
|
|
})
|
|
|
|
// 注意:部门信息通常不需要同步到素材,但如果将来需要,可以在这里添加
|
|
// 目前素材表中没有部门字段,所以不需要同步
|
|
|
|
console.log('[Update Department] Success:', {
|
|
userId: user.id,
|
|
username: user.username,
|
|
department: user.department
|
|
})
|
|
|
|
return NextResponse.json(user)
|
|
} catch (error) {
|
|
console.error('[Update Department] Error:', error)
|
|
return NextResponse.json(
|
|
{ error: '更新部门失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|