55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
/**
|
|
* 更新用户部门
|
|
* POST /api/admin/users/update-department
|
|
*/
|
|
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, department } = await request.json();
|
|
|
|
console.log('[Update Department] Request:', { userId, department });
|
|
|
|
if (!userId) {
|
|
return NextResponse.json(
|
|
{ error: '缺少用户ID' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// 更新用户部门
|
|
const user = await prisma.user.update({
|
|
where: { id: userId },
|
|
data: {
|
|
department: department || ''
|
|
}
|
|
});
|
|
|
|
console.log('[Update Department] Success:', {
|
|
userId: user.id,
|
|
username: user.username,
|
|
department: user.department
|
|
});
|
|
|
|
return NextResponse.json(user);
|
|
} catch (error) {
|
|
console.error('[Update Department] Error:', error);
|
|
return NextResponse.json(
|
|
{ error: '更新部门失败' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|