import Link from "next/link"; import { notFound, redirect } from "next/navigation"; import { db } from "@/lib/db"; import { getCurrentUser } from "@/lib/auth"; import { getCourseOutline, hasCourseAccess, isCourse } from "@/lib/courses"; import { playbackUrl } from "@/lib/video"; import { Alert } from "@/components/shop/Blocks"; import { CourseOutline } from "@/components/shop/CourseOutline"; import { LessonPlayer } from "@/components/shop/LessonPlayer"; export const metadata = { title: "Kurs", robots: { index: false } }; export default async function CoursePage({ params, searchParams }: { params: Promise<{ slug: string }>; searchParams: Promise<{ einheit?: string }> }) { const [{ slug }, { einheit }, user] = await Promise.all([params, searchParams, getCurrentUser()]); if (!user) redirect(`/konto/anmelden?weiter=/konto/kurse/${slug}`); const product = await db.product.findUnique({ where: { slug } }); if (!product || !isCourse(product)) notFound(); if (!(await hasCourseAccess(user.id, product.id))) redirect(`/produkt/${slug}`); const [outline, progress] = await Promise.all([ getCourseOutline(product.id), db.lessonProgress.findMany({ where: { userId: user.id, lesson: { chapter: { productId: product.id } } } }), ]); // Abspielbar ist eine Einheit, sobald ein fertig umgewandeltes Video da ist const lessons = outline.flatMap((c) => c.lessons).filter((l) => l.videoId); const byLesson = new Map(progress.map((p) => [p.lessonId, p])); const done = new Set(progress.filter((p) => p.completed).map((p) => p.lessonId)); // Gewählte Einheit, sonst die zuletzt angefangene, sonst die erste noch offene const lastStarted = [...progress].filter((p) => !p.completed && p.position > 0).sort((a, b) => +b.updatedAt - +a.updatedAt)[0]; const lesson = lessons.find((l) => l.id === einheit) ?? lessons.find((l) => l.id === lastStarted?.lessonId) ?? lessons.find((l) => !done.has(l.id)) ?? lessons[0]; const index = lesson ? lessons.indexOf(lesson) : -1; const next = index >= 0 ? lessons[index + 1] : undefined; return (
{!lesson ? ( Dieser Kurs hat noch keine fertigen Einheiten. Schau bald wieder vorbei. ) : ( <> {lesson.description &&

{lesson.description}

} )}
); }