From 7ffd36c42705ee37f1141b65fe88e20acca8528e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 1 Aug 2026 13:42:56 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20scope=20lesson=20progress?= =?UTF-8?q?=20queries=20to=20specific=20lesson=20IDs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Limits the database queries in 'Dashboard', 'Courses' index, and 'Certificate Pending' pages to only fetch progress records matching the retrieved or relevant lesson IDs instead of loading the user's entire historical lesson progress. Co-authored-by: projectamazonph <286085559+projectamazonph@users.noreply.github.com> --- .../courses/[courseSlug]/certificate/page.tsx | 20 +++++++++++++------ src/app/(dashboard)/courses/page.tsx | 16 ++++++++++----- src/app/(dashboard)/dashboard/page.tsx | 16 ++++++++++----- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/app/(dashboard)/courses/[courseSlug]/certificate/page.tsx b/src/app/(dashboard)/courses/[courseSlug]/certificate/page.tsx index 3640169..de3f8b6 100644 --- a/src/app/(dashboard)/courses/[courseSlug]/certificate/page.tsx +++ b/src/app/(dashboard)/courses/[courseSlug]/certificate/page.tsx @@ -194,13 +194,21 @@ async function PendingLessons({ take: 50, }); + const lessonIds = allLessons.map((l) => l.id); const completedSet = new Set( - ( - await db.lessonProgress.findMany({ - where: { userId, status: 'COMPLETED', deletedAt: null }, - select: { lessonId: true }, - }) - ).map((p) => p.lessonId), + lessonIds.length > 0 + ? ( + await db.lessonProgress.findMany({ + where: { + userId, + lessonId: { in: lessonIds }, + status: 'COMPLETED', + deletedAt: null, + }, + select: { lessonId: true }, + }) + ).map((p) => p.lessonId) + : [], ); const pending = allLessons.filter((l) => !completedSet.has(l.id)).slice(0, 8); diff --git a/src/app/(dashboard)/courses/page.tsx b/src/app/(dashboard)/courses/page.tsx index 1d0c6c3..cb18f76 100644 --- a/src/app/(dashboard)/courses/page.tsx +++ b/src/app/(dashboard)/courses/page.tsx @@ -37,11 +37,17 @@ export default async function CoursesIndexPage() { }, }); - // Get user's lesson progress - const lessonProgress = await db.lessonProgress.findMany({ - where: { userId: user.id, deletedAt: null }, - select: { lessonId: true, status: true }, - }); + // Get user's lesson progress (optimized: scope query to published lessons only to avoid loading full history) + const allPublishedLessonIds = courses.flatMap((c) => + c.modules.flatMap((m) => m.lessons.map((l) => l.id)) + ); + + const lessonProgress = allPublishedLessonIds.length > 0 + ? await db.lessonProgress.findMany({ + where: { userId: user.id, lessonId: { in: allPublishedLessonIds }, deletedAt: null }, + select: { lessonId: true, status: true }, + }) + : []; const progressMap = new Map(lessonProgress.map((p) => [p.lessonId, p.status])); return ( diff --git a/src/app/(dashboard)/dashboard/page.tsx b/src/app/(dashboard)/dashboard/page.tsx index 546298d..7939ee7 100644 --- a/src/app/(dashboard)/dashboard/page.tsx +++ b/src/app/(dashboard)/dashboard/page.tsx @@ -30,11 +30,17 @@ export default async function DashboardPage() { }, }); - // Get user's lesson progress - const lessonProgress = await db.lessonProgress.findMany({ - where: { userId: user.id, deletedAt: null }, - select: { lessonId: true, status: true }, - }); + // Get user's lesson progress (optimized: scope query to published lessons only to avoid loading full history) + const allPublishedLessonIds = courses.flatMap((c) => + c.modules.flatMap((m) => m.lessons.map((l) => l.id)) + ); + + const lessonProgress = allPublishedLessonIds.length > 0 + ? await db.lessonProgress.findMany({ + where: { userId: user.id, lessonId: { in: allPublishedLessonIds }, deletedAt: null }, + select: { lessonId: true, status: true }, + }) + : []; const progressMap = new Map(lessonProgress.map((p) => [p.lessonId, p.status])); // Compute aggregate stats