實作管理者用戶管理、邀請註冊功能
This commit is contained in:
@@ -3,71 +3,71 @@ import { UserService } from '@/lib/services/database-service'
|
||||
|
||||
const userService = new UserService()
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await userService.findById(params.id)
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ error: '用戶不存在' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// 獲取用戶統計
|
||||
const stats = await userService.getUserStatistics(params.id)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
user,
|
||||
stats
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('獲取用戶詳情錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ error: '獲取用戶詳情時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const updates = await request.json()
|
||||
|
||||
// 移除不允許更新的欄位
|
||||
delete updates.id
|
||||
delete updates.created_at
|
||||
delete updates.password_hash
|
||||
const { id: userId } = await params
|
||||
const body = await request.json()
|
||||
const { name, department, role, status } = body
|
||||
|
||||
const updatedUser = await userService.update(params.id, updates)
|
||||
|
||||
if (!updatedUser) {
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ error: '用戶不存在或更新失敗' },
|
||||
{ status: 404 }
|
||||
{ success: false, error: '用戶 ID 是必需的' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '用戶資料已更新',
|
||||
data: updatedUser
|
||||
if (!name || !department || !role || !status) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '請填寫所有必填欄位' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 驗證狀態值
|
||||
const validStatuses = ['active', 'inactive', 'invited']
|
||||
if (!validStatuses.includes(status)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '無效的狀態值' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 驗證角色值
|
||||
const validRoles = ['user', 'developer', 'admin']
|
||||
if (!validRoles.includes(role)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '無效的角色值' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const result = await userService.updateUser(userId, {
|
||||
name,
|
||||
department,
|
||||
role,
|
||||
status
|
||||
})
|
||||
|
||||
if (result.success) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '用戶資料更新成功',
|
||||
data: result.user
|
||||
})
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: result.error },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('更新用戶錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ error: '更新用戶時發生錯誤' },
|
||||
{ success: false, error: '更新用戶時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
@@ -75,29 +75,37 @@ export async function PUT(
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
// 軟刪除:將 is_active 設為 false
|
||||
const result = await userService.update(params.id, { is_active: false })
|
||||
|
||||
if (!result) {
|
||||
const { id: userId } = await params
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ error: '用戶不存在或刪除失敗' },
|
||||
{ status: 404 }
|
||||
{ success: false, error: '用戶 ID 是必需的' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '用戶已刪除'
|
||||
})
|
||||
const result = await userService.deleteUser(userId)
|
||||
|
||||
if (result.success) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '用戶刪除成功'
|
||||
})
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: result.error },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('刪除用戶錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ error: '刪除用戶時發生錯誤' },
|
||||
{ success: false, error: '刪除用戶時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user