import { db } from "../../db/index"; import { courses, topics, userProgress } from "../../db/schema"; import { eq, and } from "drizzle-orm"; export default defineEventHandler(async () => { const allCourses = await db.query.courses.findMany({ orderBy: (c, { desc }) => desc(c.createdAt), }); const result = []; for (const course of allCourses) { const topicRows = await db.query.topics.findMany({ where: eq(topics.courseId, course.id), }); const topicCount = topicRows.length; let completedCount = 0; if (topicCount > 0) { const progressRows = await db.query.userProgress.findMany({ where: eq(userProgress.courseId, course.id), }); completedCount = progressRows.filter((p) => p.lessonComplete).length; } result.push({ ...course, topicCount, completedCount, }); } return result; });