Initial commit: FL-Akademie LMS mit Docker, Admin, Portal und Dokumentation.
Made-with: Cursor
This commit is contained in:
57
app/api/progress/route.ts
Normal file
57
app/api/progress/route.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/lib/auth-options";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { syncCertificateForCourse } from "@/lib/certificates";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Nicht angemeldet." }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = (await req.json().catch(() => null)) as {
|
||||
lessonId?: string;
|
||||
completed?: boolean;
|
||||
} | null;
|
||||
|
||||
const lessonId = body?.lessonId?.trim();
|
||||
if (!lessonId) {
|
||||
return NextResponse.json({ error: "lessonId fehlt." }, { status: 400 });
|
||||
}
|
||||
|
||||
const lesson = await prisma.lesson.findUnique({
|
||||
where: { id: lessonId },
|
||||
include: { module: { include: { course: true } } },
|
||||
});
|
||||
if (!lesson) {
|
||||
return NextResponse.json({ error: "Lektion nicht gefunden." }, { status: 404 });
|
||||
}
|
||||
|
||||
const courseId = lesson.module.course.id;
|
||||
const enrollment = await prisma.enrollment.findUnique({
|
||||
where: { userId_courseId: { userId: session.user.id, courseId } },
|
||||
});
|
||||
if (!enrollment) {
|
||||
return NextResponse.json({ error: "Nicht eingeschrieben." }, { status: 403 });
|
||||
}
|
||||
|
||||
const completed =
|
||||
typeof body?.completed === "boolean" ? body.completed : true;
|
||||
|
||||
if (completed) {
|
||||
await prisma.lessonProgress.upsert({
|
||||
where: { userId_lessonId: { userId: session.user.id, lessonId } },
|
||||
update: { completedAt: new Date() },
|
||||
create: { userId: session.user.id, lessonId, completedAt: new Date() },
|
||||
});
|
||||
await syncCertificateForCourse(session.user.id, courseId);
|
||||
} else {
|
||||
await prisma.lessonProgress.deleteMany({
|
||||
where: { userId: session.user.id, lessonId },
|
||||
});
|
||||
await syncCertificateForCourse(session.user.id, courseId);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
Reference in New Issue
Block a user