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

133 lines
4.0 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 { 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, departmentName } = await req.json()
if (!userId || !departmentName) {
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 existingManager = await prisma.user.findFirst({
where: {
managedDepartment: departmentName,
NOT: { id: userId }
}
})
if (existingManager) {
return NextResponse.json({
error: `部门"${departmentName}"已有管理员:${existingManager.username}`
}, { status: 400 })
}
// 更新用户为部门管理员
// 同时将用户的department字段设置为该部门,确保部门管理员显示在部门列表中
// 并将该部门下其他用户的直属上级设置为新管理员
const user = await prisma.$transaction(async (tx) => {
// 更新用户为部门管理员
const updatedUser = await tx.user.update({
where: { id: userId },
data: {
managedDepartment: departmentName,
department: departmentName, // 确保部门管理员也属于该部门
role: 'ADMIN' // 设置为管理员角色
}
})
// 将该部门下其他用户的直属上级设置为新管理员
await tx.user.updateMany({
where: {
department: departmentName,
id: { not: userId }, // 排除新管理员自己
},
data: {
parentId: userId
}
})
return updatedUser
})
return NextResponse.json({
message: '部门管理员设置成功',
user
})
} catch (error) {
console.error('Set department manager error:', error)
return NextResponse.json({ error: '设置失败' }, { status: 500 })
}
}
// 取消部门管理员
export async function DELETE(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session || !(await isCurrentSuperAdmin(session))) {
return NextResponse.json({ error: '仅最高权限账号可取消部门管理员' }, { status: 403 })
}
const { searchParams } = new URL(req.url)
const userId = searchParams.get('userId')
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: {
managedDepartment: null
}
})
return NextResponse.json({
message: '已取消部门管理员',
user
})
} catch (error) {
console.error('Remove department manager error:', error)
return NextResponse.json({ error: '取消失败' }, { status: 500 })
}
}