project-standalo-todo-super/app/api/tasks/checkin/route.ts

89 lines
2.5 KiB
TypeScript

/**
* POST /api/tasks/checkin - Daily check-in task completion
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth } from '@/app/lib/auth';
import { getActiveTasks, getUserCompletedTasks, 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) {
try {
// Authenticate user
const user = requireAuth(request);
// Find the daily check-in task
const tasks = getActiveTasks();
const checkinTask = tasks.find(t => t.type === TaskType.CHECKIN);
if (!checkinTask) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Daily check-in task not found'
},
{ status: 404 }
);
}
// Check if user already checked in today
const completedTasks = getUserCompletedTasks(user.id);
const today = new Date().toISOString().split('T')[0];
const alreadyCheckedIn = completedTasks.some(ut => {
const completedDate = ut.completedAt.toISOString().split('T')[0];
return ut.taskId === checkinTask.id && completedDate === today;
});
if (alreadyCheckedIn) {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'You have already checked in today'
},
{ status: 409 }
);
}
// Complete the check-in task
const userTask = createUserTask(user.id, checkinTask.id, checkinTask.pointsReward);
// Award points
addPoints(user.id, checkinTask.pointsReward, `task:${checkinTask.id}`);
// Check and award badges
const newBadges = checkAndAwardBadges(user.id);
return NextResponse.json<ApiResponse<UserTask>>(
{
success: true,
data: userTask,
message: `Check-in successful! You earned ${checkinTask.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('Check-in error:', error);
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Internal server error'
},
{ status: 500 }
);
}
}