Skip to content

Bug: listCourses only counts enrollments for first course on page #1

Description

@DeFiVC

Description

In src/modules/courses/course.service.ts, the listCourses method fetches enrollment counts for each course on the page to display in the course catalog. However, the enrollment count query only filters by the first course ID, so all other courses on the page show 0 enrollments.

The bug (line 48-51)

// Current (buggy) code:constcounts=awaitdb.select({courseId: enrollments.courseId,value: count(),}).from(enrollments).where(eq(enrollments.courseId,courseIds[0])// BUG: only queries first ID).groupBy(enrollments.courseId);

The fix

Import inArray from drizzle-orm and use it instead of eq:

import{eq,and,count,desc,inArray}from"drizzle-orm";// ....where(inArray(enrollments.courseId,courseIds))

The inArray function generates a SQL WHERE course_id IN (...) clause that queries all course IDs at once.

Impact

On a page with 10 courses, only the first course shows its real enrollment count. All others show 0. This is visible in the course catalog where the "enrolled" count appears incorrect.

Files to change

  • src/modules/courses/course.service.ts — line 1 (add inArray import) and line 50 (replace eq with inArray)

How to verify

  1. Create 3 courses in the database
  2. Enroll 5 users in course 1, 3 users in course 2, 0 users in course 3
  3. Call GET /api/courses?page=1&limit=10
  4. Verify: course 1 shows enrolledCount: 5, course 2 shows enrolledCount: 3, course 3 shows enrolledCount: 0

Before the fix, only course 1 would show 5, and courses 2 and 3 would both show 0.

Additional context

The getCourseDetail method (line 114) also has a related TODO: enrolledCount: 0, // TODO: aggregate. That should also be fixed using a similar count() query, but it's a separate concern from this bug.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official CampaignbugSomething isn't workinggood first issueGood for newcomerstypescriptTypeScript language

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions