124 lines
3.6 KiB
TypeScript
124 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import {
|
|
getDingtalkProfileCookieCandidates,
|
|
getDingtalkProfileCookiePath,
|
|
verifyDingtalkProfileToken,
|
|
} from '@/lib/dingtalk-profile-token'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
const sessionUserId = (session?.user as any)?.id
|
|
const profileCookieNames = getDingtalkProfileCookieCandidates(request)
|
|
let profileUserId: string | null = null
|
|
for (const cookieName of profileCookieNames) {
|
|
profileUserId = await verifyDingtalkProfileToken(request.cookies.get(cookieName)?.value)
|
|
if (profileUserId) break
|
|
}
|
|
const authorizedUserId = String(sessionUserId || profileUserId || '')
|
|
|
|
if (!authorizedUserId) {
|
|
return NextResponse.json(
|
|
{ success: false, message: '未登录' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const { userId: requestedUserId, department } = await request.json()
|
|
const normalizedDepartment = typeof department === 'string' ? department.trim() : ''
|
|
|
|
if (!normalizedDepartment) {
|
|
return NextResponse.json(
|
|
{ success: false, message: '缺少必要参数' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (requestedUserId && requestedUserId !== authorizedUserId) {
|
|
return NextResponse.json(
|
|
{ success: false, message: '无权限完善其他用户资料' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
console.log('完善用户信息(选择部门):', { userId: authorizedUserId, department: normalizedDepartment })
|
|
|
|
// 获取当前钉钉用户信息
|
|
const currentUser = await prisma.user.findUnique({
|
|
where: { id: authorizedUserId }
|
|
})
|
|
|
|
if (!currentUser) {
|
|
return NextResponse.json(
|
|
{ success: false, message: '用户不存在' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
const departmentRecord = await prisma.department.findUnique({
|
|
where: { name: normalizedDepartment },
|
|
select: { id: true, name: true },
|
|
})
|
|
|
|
if (!departmentRecord) {
|
|
return NextResponse.json(
|
|
{ success: false, message: '部门不存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 更新用户部门信息
|
|
const user = await prisma.user.update({
|
|
where: { id: authorizedUserId },
|
|
data: {
|
|
department: departmentRecord.name,
|
|
departmentId: departmentRecord.id,
|
|
},
|
|
include: {
|
|
departmentRef: true // 包含部门关联信息
|
|
}
|
|
})
|
|
|
|
console.log('用户信息更新成功:', user.username)
|
|
|
|
// 返回用户信息(不包含敏感信息)
|
|
const response = NextResponse.json({
|
|
success: true,
|
|
message: '信息完善成功',
|
|
user: {
|
|
id: user.id,
|
|
username: user.username,
|
|
email: user.email,
|
|
realName: user.realName,
|
|
avatar: user.avatar,
|
|
role: user.role,
|
|
department: user.department,
|
|
isActive: user.isActive,
|
|
status: (user as any).status || (user.isActive ? 'active' : 'pending') // 阶段一新增
|
|
}
|
|
})
|
|
for (const cookieName of profileCookieNames) {
|
|
response.cookies.set(cookieName, '', {
|
|
httpOnly: true,
|
|
secure: cookieName.startsWith('__Secure-'),
|
|
path: getDingtalkProfileCookiePath(),
|
|
maxAge: 0,
|
|
})
|
|
}
|
|
return response
|
|
|
|
} catch (error) {
|
|
console.error('完善用户信息失败:', error)
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: '信息提交失败,请稍后再试'
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|