"use server"; import { prisma } from "@/lib/prisma"; import { auth } from "@/lib/auth"; import { headers } from "next/headers"; import { revalidatePath } from "next/cache"; export async function toggleLessonProgress(lessonId: string, slug: string) { const session = await auth.api.getSession({ headers: await headers() }); if (!session) throw new Error("Unauthorized"); const existing = await prisma.lessonProgress.findUnique({ where: { userId_lessonId: { userId: session.user.id, lessonId } }, }); if (existing) { await prisma.lessonProgress.delete({ where: { userId_lessonId: { userId: session.user.id, lessonId } }, }); } else { await prisma.lessonProgress.create({ data: { userId: session.user.id, lessonId }, }); } revalidatePath(`/courses/${slug}/lessons/${lessonId}`); revalidatePath(`/courses/${slug}`); revalidatePath("/dashboard"); }