|
| 1 | +import { db } from "@/config/db"; |
| 2 | +import { |
| 3 | + completedExercisesTable, |
| 4 | + usersTable, |
| 5 | + courseChaptersTable, |
| 6 | + enrolledCoursesTable, |
| 7 | +} from "@/config/schema"; |
| 8 | +import { currentUser } from "@clerk/nextjs/server"; |
| 9 | +import { and, eq, sql } from "drizzle-orm"; |
| 10 | +import { NextRequest, NextResponse } from "next/server"; |
| 11 | + |
| 12 | +export async function POST(req: NextRequest) { |
| 13 | + try { |
| 14 | + const { courseId, chapterId, exerciseId, xpEarned } = await req.json(); |
| 15 | + const user = await currentUser(); |
| 16 | + |
| 17 | + if (!user?.primaryEmailAddress?.emailAddress) { |
| 18 | + return NextResponse.json( |
| 19 | + { error: "User not authenticated" }, |
| 20 | + { status: 401 } |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + // Get or create user in database |
| 25 | + let dbUser = await db |
| 26 | + .select() |
| 27 | + .from(usersTable) |
| 28 | + .where(eq(usersTable.email, user.primaryEmailAddress.emailAddress)) |
| 29 | + .limit(1); |
| 30 | + |
| 31 | + if (!dbUser || dbUser.length === 0) { |
| 32 | + const newUser = await db |
| 33 | + .insert(usersTable) |
| 34 | + .values({ |
| 35 | + email: user.primaryEmailAddress.emailAddress, |
| 36 | + name: user.fullName || user.firstName || "User", |
| 37 | + }) |
| 38 | + .returning(); |
| 39 | + dbUser = newUser; |
| 40 | + } |
| 41 | + |
| 42 | + // Get the actual chapter table ID (not logical chapterId) |
| 43 | + const chapter = await db |
| 44 | + .select() |
| 45 | + .from(courseChaptersTable) |
| 46 | + .where( |
| 47 | + and( |
| 48 | + eq(courseChaptersTable.courseId, courseId), |
| 49 | + eq(courseChaptersTable.chapterId, chapterId) |
| 50 | + ) |
| 51 | + ) |
| 52 | + .limit(1); |
| 53 | + |
| 54 | + if (!chapter || chapter.length === 0) { |
| 55 | + return NextResponse.json({ error: "Chapter not found" }, { status: 404 }); |
| 56 | + } |
| 57 | + |
| 58 | + // Check if already completed |
| 59 | + const existing = await db |
| 60 | + .select() |
| 61 | + .from(completedExercisesTable) |
| 62 | + .where( |
| 63 | + and( |
| 64 | + eq(completedExercisesTable.userId, dbUser[0].id), |
| 65 | + eq(completedExercisesTable.courseId, courseId), |
| 66 | + eq(completedExercisesTable.chapterId, chapter[0].id), |
| 67 | + eq(completedExercisesTable.exerciseId, exerciseId) |
| 68 | + ) |
| 69 | + ) |
| 70 | + .limit(1); |
| 71 | + |
| 72 | + if (existing && existing.length > 0) { |
| 73 | + return NextResponse.json({ |
| 74 | + alreadyCompleted: true, |
| 75 | + message: "Exercise already completed", |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + // Insert completion record |
| 80 | + const result = await db |
| 81 | + .insert(completedExercisesTable) |
| 82 | + .values({ |
| 83 | + chapterId: chapter[0].id, // Use actual DB id, not logical chapterId |
| 84 | + courseId: courseId, |
| 85 | + exerciseId: exerciseId, |
| 86 | + userId: dbUser[0].id, |
| 87 | + }) |
| 88 | + .returning(); |
| 89 | + |
| 90 | + //Update Course XP Earned |
| 91 | + await db |
| 92 | + .update(enrolledCoursesTable) |
| 93 | + .set({ xpEarned: sql`${enrolledCoursesTable.xpEarned}+${xpEarned}` }) |
| 94 | + .where(eq(enrolledCoursesTable?.courseId, courseId)); |
| 95 | + |
| 96 | + //Update User XP Earn Points |
| 97 | + await db |
| 98 | + .update(usersTable) |
| 99 | + .set({ |
| 100 | + points: sql`${usersTable.points}+${xpEarned}`, |
| 101 | + }) |
| 102 | + .where(eq(usersTable.email, user?.primaryEmailAddress?.emailAddress)); |
| 103 | + return NextResponse.json({ |
| 104 | + success: true, |
| 105 | + alreadyCompleted: false, |
| 106 | + data: result, |
| 107 | + }); |
| 108 | + } catch (error: any) { |
| 109 | + console.error("Error completing exercise:", error); |
| 110 | + return NextResponse.json( |
| 111 | + { |
| 112 | + error: "Failed to complete exercise", |
| 113 | + details: error?.message || "Unknown error", |
| 114 | + }, |
| 115 | + { status: 500 } |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments