project-standalo-todo-super/app/api/quizzes/[taskId]/route.ts

88 lines
2.0 KiB
TypeScript

/**
* GET /api/quizzes/[taskId] - Get quiz questions for a task
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth } from '@/app/lib/auth';
import { findTask, findQuizByTaskId } from '@/app/lib/db/store';
import { TaskType, ApiResponse } from '@/app/lib/types';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ taskId: string }> }
) {
try {
// Authenticate user
requireAuth(request);
const { taskId } = await params;
// Find the task
const task = findTask(taskId);
if (!task) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Task not found'
},
{ status: 404 }
);
}
// Verify it's a quiz task
if (task.type !== TaskType.QUIZ) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'This is not a quiz task'
},
{ status: 400 }
);
}
// Find the quiz
const quiz = findQuizByTaskId(taskId);
if (!quiz) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Quiz not found for this task'
},
{ status: 404 }
);
}
// Return quiz without the correct answer
const { correctAnswer, ...quizData } = quiz;
return NextResponse.json<ApiResponse>(
{
success: true,
data: {
...quizData,
pointsReward: task.pointsReward
}
},
{ status: 200 }
);
} catch (error) {
if (error instanceof Error && error.message === 'Unauthorized') {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Unauthorized'
},
{ status: 401 }
);
}
console.error('Get quiz error:', error);
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Internal server error'
},
{ status: 500 }
);
}
}