2026-05-14 14:58:12 +08:00

77 lines
2.3 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { isSuperAdmin } from '@/lib/department-permission';
/**
* POST - 复制权限配置
*/
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const { sourceDepartment, targetDepartments } = await request.json();
if (!sourceDepartment || !targetDepartments || !Array.isArray(targetDepartments)) {
return NextResponse.json(
{ error: '源部门和目标部门不能为空' },
{ status: 400 }
);
}
// 获取源部门的所有权限配置
const sourcePermissions = await prisma.departmentMaterialPermission.findMany({
where: { department: sourceDepartment },
});
if (sourcePermissions.length === 0) {
return NextResponse.json(
{ error: '源部门没有权限配置' },
{ status: 400 }
);
}
// 为每个目标部门复制权限
for (const targetDept of targetDepartments) {
if (targetDept === sourceDepartment) {
continue; // 跳过自己
}
// 先删除目标部门的所有权限配置
await prisma.departmentMaterialPermission.deleteMany({
where: { department: targetDept },
});
// 复制源部门的权限配置
const newPermissions = sourcePermissions.map((perm) => ({
department: targetDept,
materialType: perm.materialType,
canView: perm.canView,
canDownload: perm.canDownload,
canDownloadSource: perm.canDownloadSource,
canShare: perm.canShare,
linkedToDepartment: null, // 复制后不关联
}));
await prisma.departmentMaterialPermission.createMany({
data: newPermissions,
});
}
return NextResponse.json({
message: '权限复制成功',
copiedCount: targetDepartments.length,
});
} catch (error) {
console.error('[Department Permissions Copy] Error:', error);
return NextResponse.json({ error: '复制权限失败' }, { status: 500 });
}
}