52 lines
1.3 KiB
TypeScript

// 切换用户激活状态API
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
// POST - 切换用户激活状态(仅管理员)
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 { userId, isActive } = await request.json();
if (!userId || typeof isActive !== 'boolean') {
return NextResponse.json(
{ error: '参数错误' },
{ status: 400 }
);
}
// 不能禁用自己
if (userId === session.user.id) {
return NextResponse.json(
{ error: '不能禁用自己' },
{ status: 400 }
);
}
// 更新激活状态
await prisma.user.update({
where: { id: userId },
data: { isActive },
});
return NextResponse.json({
message: isActive ? '用户已激活' : '用户已禁用',
});
} catch (error) {
console.error('Toggle active error:', error);
return NextResponse.json(
{ error: '操作失败' },
{ status: 500 }
);
}
}