29 lines
842 B
TypeScript
29 lines
842 B
TypeScript
import { prisma } from "@/lib/prisma";
|
|
|
|
export async function getCourseLessonStats(courseId: string) {
|
|
const lessons = await prisma.lesson.count({
|
|
where: { published: true, module: { courseId } },
|
|
});
|
|
return { totalLessons: lessons };
|
|
}
|
|
|
|
export async function getUserCourseProgress(userId: string, courseId: string) {
|
|
const lessons = await prisma.lesson.findMany({
|
|
where: { published: true, module: { courseId } },
|
|
select: { id: true },
|
|
});
|
|
const total = lessons.length;
|
|
if (total === 0) return { total: 0, completed: 0, percent: 0 };
|
|
|
|
const completed = await prisma.lessonProgress.count({
|
|
where: {
|
|
userId,
|
|
completedAt: { not: null },
|
|
lessonId: { in: lessons.map((l) => l.id) },
|
|
},
|
|
});
|
|
|
|
const percent = Math.round((completed / total) * 100);
|
|
return { total, completed, percent };
|
|
}
|