chenpei ea9076df45 feat: 实现统一的下载错误处理系统并修复产品全案数据库Schema
- 创建统一的下载错误处理模块 (lib/download-errors.ts)
- 实现13种错误类型分类和中文错误消息
- 后端统一使用 createDownloadError 创建标准化错误响应
- 前端统一使用 parseDownloadError 解析错误并显示
- 修复正式系统数据库Schema,添加产品全案相关字段和表
- 执行数据库迁移:添加 task_no, task_status, task_fetched_at 字段
- 创建 material_assets, api_keys, api_call_logs 表
2026-01-05 17:52:42 +08:00

135 lines
4.1 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';
/**
* DELETE - 删除部门的所有自定义权限配置,使其恢复为继承父部门权限
*/
export async function DELETE(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 { searchParams } = new URL(request.url);
const department = searchParams.get('department');
if (!department) {
return NextResponse.json({ error: '部门名称不能为空' }, { status: 400 });
}
// 删除该部门的所有权限配置(不包括关联的,因为关联的需要先取消关联)
await prisma.departmentMaterialPermission.deleteMany({
where: {
department,
linkedToDepartment: null, // 只删除非关联的权限配置
},
});
return NextResponse.json({
message: '已还原为继承父部门权限',
department,
});
} catch (error) {
console.error('[Department Permissions Inherit] DELETE Error:', error);
return NextResponse.json({ error: '还原失败' }, { status: 500 });
}
}
/**
* 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 body = await request.json();
const { department } = body;
if (!department) {
return NextResponse.json({ error: '部门名称不能为空' }, { status: 400 });
}
// 获取部门信息(包括父部门)
const dept = await prisma.department.findFirst({
where: { name: department },
include: { parent: true },
});
if (!dept) {
return NextResponse.json({ error: '部门不存在' }, { status: 404 });
}
if (!dept.parentId) {
return NextResponse.json({ error: '该部门没有父部门,无法继承' }, { status: 400 });
}
// 获取父部门的权限配置
const { getDepartmentPermissions, getAllMaterialTypes } = await import('@/lib/department-permission');
const allMaterialTypes = await getAllMaterialTypes();
// 获取所有部门用于权限继承检查
const allDepartments = await prisma.department.findMany({
select: { id: true, name: true, parentId: true },
});
const parentPermissions = await getDepartmentPermissions(
dept.parent!.name,
allDepartments,
allMaterialTypes
);
// 检查是否已经有自定义权限配置
const existingCount = await prisma.departmentMaterialPermission.count({
where: {
department,
linkedToDepartment: null,
},
});
if (existingCount > 0) {
return NextResponse.json(
{ error: '该部门已有自定义权限配置,请先还原为继承后再操作' },
{ status: 400 }
);
}
// 基于父部门权限创建独立配置
const newPermissions = allMaterialTypes.map((materialType) => {
const parentPerm = parentPermissions[materialType];
return {
department,
materialType,
canView: parentPerm?.canView ?? false,
canDownload: parentPerm?.canDownload ?? false,
canShare: parentPerm?.canShare ?? true,
linkedToDepartment: null,
};
});
await prisma.departmentMaterialPermission.createMany({
data: newPermissions,
});
return NextResponse.json({
message: '已创建独立配置',
department,
createdCount: newPermissions.length,
});
} catch (error) {
console.error('[Department Permissions Inherit] POST Error:', error);
return NextResponse.json({ error: '创建独立配置失败' }, { status: 500 });
}
}