Some checks failed
CI Check / check (push) Has been cancelled
- 移除高亮卡片效果,避免与虚位卡片视觉重复 - 松开鼠标后立即更新布局,不再等待动画完成 - 优化拖拽动画效果,使用弹性缓动函数 - 更新 CHANGELOG.md 记录本次优化
310 lines
7.8 KiB
TypeScript
310 lines
7.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
// Get all departments
|
|
export async function GET() {
|
|
try {
|
|
const departments = await prisma.department.findMany({
|
|
orderBy: {
|
|
createdAt: 'asc',
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(departments)
|
|
} catch (error) {
|
|
console.error('Error fetching departments:', error)
|
|
return NextResponse.json(
|
|
{ error: '获取部门列表失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// Create a new department (admin only)
|
|
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 { name } = await req.json()
|
|
|
|
if (!name || name.trim() === '') {
|
|
return NextResponse.json(
|
|
{ error: '部门名称不能为空' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Check if department already exists
|
|
const existing = await prisma.department.findUnique({
|
|
where: { name },
|
|
})
|
|
|
|
if (existing) {
|
|
return NextResponse.json(
|
|
{ error: '部门已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const department = await prisma.department.create({
|
|
data: { name },
|
|
})
|
|
|
|
return NextResponse.json(department, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Error creating department:', error)
|
|
return NextResponse.json(
|
|
{ error: '创建部门失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// Rename a department (admin only)
|
|
export async function PATCH(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any).role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '无权限' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { id, name } = await req.json()
|
|
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: '缺少部门ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (!name || name.trim() === '') {
|
|
return NextResponse.json(
|
|
{ error: '部门名称不能为空' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const targetDepartment = await prisma.department.findUnique({
|
|
where: { id },
|
|
})
|
|
|
|
if (!targetDepartment) {
|
|
return NextResponse.json(
|
|
{ error: '未找到对应的部门' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
const trimmedName = name.trim()
|
|
|
|
if (targetDepartment.name === trimmedName) {
|
|
return NextResponse.json(targetDepartment)
|
|
}
|
|
|
|
// 构建所有需要重命名的部门(自身 + 子级)
|
|
const oldName = targetDepartment.name
|
|
const oldPrefix = `${oldName}-`
|
|
const newPrefix = `${trimmedName}-`
|
|
|
|
const descendantDepartments = await prisma.department.findMany({
|
|
where: {
|
|
name: {
|
|
startsWith: oldPrefix,
|
|
},
|
|
},
|
|
})
|
|
|
|
type RenameItem = { id: string; oldName: string; newName: string }
|
|
const renameItems: RenameItem[] = [
|
|
{ id: targetDepartment.id, oldName, newName: trimmedName },
|
|
...descendantDepartments.map((dept) => ({
|
|
id: dept.id,
|
|
oldName: dept.name,
|
|
newName: `${newPrefix}${dept.name.slice(oldPrefix.length)}`,
|
|
})),
|
|
]
|
|
|
|
// 检查是否会与其他部门重名
|
|
const newNameSet = Array.from(new Set(renameItems.map((item) => item.newName)))
|
|
const renameIds = renameItems.map((item) => item.id)
|
|
const conflicting = await prisma.department.findMany({
|
|
where: {
|
|
name: { in: newNameSet },
|
|
NOT: {
|
|
id: { in: renameIds },
|
|
},
|
|
},
|
|
select: { name: true },
|
|
})
|
|
|
|
if (conflicting.length > 0) {
|
|
return NextResponse.json(
|
|
{ error: `存在同名分支:${conflicting.map((item) => item.name).join('、')}` },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const renameValue = (value?: string | null) => {
|
|
if (!value) return value
|
|
if (value === oldName) return trimmedName
|
|
if (value.startsWith(oldPrefix)) {
|
|
return `${newPrefix}${value.slice(oldPrefix.length)}`
|
|
}
|
|
return value
|
|
}
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
for (const item of renameItems) {
|
|
await tx.department.update({
|
|
where: { id: item.id },
|
|
data: { name: item.newName },
|
|
})
|
|
}
|
|
|
|
const affectedUsers = await tx.user.findMany({
|
|
where: {
|
|
OR: [
|
|
{ department: oldName },
|
|
{ department: { startsWith: oldPrefix } },
|
|
{ managedDepartment: oldName },
|
|
{ managedDepartment: { startsWith: oldPrefix } },
|
|
],
|
|
},
|
|
select: {
|
|
id: true,
|
|
department: true,
|
|
managedDepartment: true,
|
|
},
|
|
})
|
|
|
|
for (const user of affectedUsers) {
|
|
const nextDepartment = renameValue(user.department) ?? ''
|
|
const nextManaged = renameValue(user.managedDepartment)
|
|
const data: { department?: string; managedDepartment?: string | null } = {}
|
|
|
|
if (nextDepartment !== user.department) {
|
|
data.department = nextDepartment
|
|
}
|
|
|
|
if (nextManaged !== user.managedDepartment) {
|
|
data.managedDepartment = nextManaged ?? null
|
|
}
|
|
|
|
if (Object.keys(data).length > 0) {
|
|
await tx.user.update({
|
|
where: { id: user.id },
|
|
data,
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
const refreshedDepartment = await prisma.department.findUnique({
|
|
where: { id },
|
|
})
|
|
|
|
return NextResponse.json(refreshedDepartment)
|
|
} catch (error) {
|
|
console.error('Error updating department:', error)
|
|
return NextResponse.json(
|
|
{ error: '更新部门失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// Delete a department (admin only)
|
|
export async function DELETE(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any).role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '无权限' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
const id = searchParams.get('id')
|
|
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: '缺少部门ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const department = await prisma.department.findUnique({
|
|
where: { id },
|
|
select: { id: true, name: true },
|
|
})
|
|
|
|
if (!department) {
|
|
return NextResponse.json(
|
|
{ error: '未找到对应的部门' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
const prefix = `${department.name}-`
|
|
const relatedDepartments = await prisma.department.findMany({
|
|
where: {
|
|
OR: [
|
|
{ name: department.name },
|
|
{ name: { startsWith: prefix } },
|
|
],
|
|
},
|
|
select: { id: true, name: true },
|
|
})
|
|
|
|
const targetIds = relatedDepartments.map((dept) => dept.id)
|
|
const targetNames = relatedDepartments.map((dept) => dept.name)
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
if (targetNames.length > 0) {
|
|
await tx.user.updateMany({
|
|
where: { department: { in: targetNames } },
|
|
data: { department: '' },
|
|
})
|
|
|
|
await tx.user.updateMany({
|
|
where: { managedDepartment: { in: targetNames } },
|
|
data: { managedDepartment: null },
|
|
})
|
|
}
|
|
|
|
if (targetIds.length > 0) {
|
|
await tx.department.deleteMany({
|
|
where: { id: { in: targetIds } },
|
|
})
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
message: `已删除 ${targetIds.length} 个分支`,
|
|
removedDepartments: targetNames,
|
|
})
|
|
} catch (error) {
|
|
console.error('Error deleting department:', error)
|
|
return NextResponse.json(
|
|
{ error: '删除失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|