project-standalo-todo-super/app/api/tasks/[id]/complete/route.ts

109 lines
2.9 KiB
TypeScript

/**
* POST /api/tasks/[id]/complete - Complete a specific task
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth } from '@/app/lib/auth';
import { findTask, hasUserCompletedTask, createUserTask } from '@/app/lib/db/store';
import { addPoints } from '@/app/lib/points';
import { checkAndAwardBadges } from '@/app/lib/badges';
import { TaskType, UserTask, ApiResponse } from '@/app/lib/types';
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
// Authenticate user
const user = requireAuth(request);
const { id: taskId } = await params;
// Find the task
const task = findTask(taskId);
if (!task) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Task not found'
},
{ status: 404 }
);
}
// Check if task is active
if (!task.isActive) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Task is not active'
},
{ status: 400 }
);
}
// Check if user has already completed this task
// For certain task types, allow multiple completions
const allowMultiple = [TaskType.AD, TaskType.VIDEO, TaskType.READING].includes(task.type);
if (!allowMultiple && hasUserCompletedTask(user.id, taskId)) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'You have already completed this task'
},
{ status: 409 }
);
}
// Handle quiz tasks separately (they need quiz submission)
if (task.type === TaskType.QUIZ) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Quiz tasks must be completed via /api/quizzes/[taskId]/submit'
},
{ status: 400 }
);
}
// Complete the task
const userTask = createUserTask(user.id, taskId, task.pointsReward);
// Award points
addPoints(user.id, task.pointsReward, `task:${taskId}`);
// Check and award badges
const newBadges = checkAndAwardBadges(user.id);
return NextResponse.json<ApiResponse<UserTask>>(
{
success: true,
data: userTask,
message: `Task completed! You earned ${task.pointsReward} points${
newBadges.length > 0 ? ` and ${newBadges.length} new badge(s)!` : ''
}`
},
{ status: 201 }
);
} catch (error) {
if (error instanceof Error && error.message === 'Unauthorized') {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Unauthorized'
},
{ status: 401 }
);
}
console.error('Task completion error:', error);
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Internal server error'
},
{ status: 500 }
);
}
}