63 lines
1.8 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 { createMaterialLog } from '@/lib/material-log';
// POST /api/materials/restore - 恢复已删除的素材
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: '无权限' }, { status: 403 });
}
const { id } = await request.json();
if (!id) {
return NextResponse.json({ error: '缺少素材ID' }, { status: 400 });
}
// 检查素材是否存在
const material = await prisma.material.findUnique({
where: { id },
});
if (!material) {
return NextResponse.json({ error: '素材不存在' }, { status: 404 });
}
// 恢复素材(将 deletedAt 设为 null)
await prisma.material.update({
where: { id },
data: {
deletedAt: null,
},
});
// 记录操作日志
const userId = (session.user as any).id;
await createMaterialLog({
materialId: id,
userId,
action: 'RESTORE',
changes: {
deletedAt: {
oldValue: material.deletedAt?.toISOString() || null,
newValue: null,
},
},
});
return NextResponse.json({ message: '恢复成功' });
} catch (error: any) {
// 兼容旧版本数据库(没有 deletedAt 字段)
if (error?.meta?.column?.includes('deletedAt')) {
return NextResponse.json({ error: '恢复功能需要先运行数据库迁移' }, { status: 400 });
}
console.error('[Materials] RESTORE error:', error);
return NextResponse.json({ error: '恢复失败' }, { status: 500 });
}
}