Skip to content

Commit

Permalink
Fix filtering of started courses
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 committed Jan 17, 2025
1 parent 2a534a2 commit fe635ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
39 changes: 24 additions & 15 deletions src/Services/ProgressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,30 @@ private function filterForPlannedCourses(Builder $query, int $userId): Builder
private function filterForStartedCourses(Builder $query, int $userId): Builder
{
return $query
->whereExists(function (QueryBuilder $query) use ($userId) {
$query->select(DB::raw(1))
->from('topics')
->join('lessons', 'lessons.id', '=', 'topics.lesson_id')
->leftJoin('course_progress', function (JoinClause $join) use ($userId) {
$join->on('topics.id', '=', 'course_progress.topic_id')
->where('course_progress.user_id', $userId);
})
->whereColumn('lessons.course_id', 'courses.id')
->where('course_progress.status', ProgressStatus::IN_PROGRESS)
->orWhere(function (QueryBuilder $query) {
$query->groupBy('topic.id')
->selectRaw('COUNT(CASE WHEN status = ? THEN 1 END) as completed_count', [ProgressStatus::COMPLETE])
->selectRaw('COUNT(CASE WHEN status = ? THEN 1 END) as in_completed_count', [ProgressStatus::INCOMPLETE])
->havingRaw('completed_count > 0 AND in_completed_count > 0');
->where(function (Builder $query) use ($userId) {
$query->whereExists(function (QueryBuilder $query) use ($userId) {
$query->select(DB::raw(1))
->from('topics')
->join('lessons', 'lessons.id', '=', 'topics.lesson_id')
->leftJoin('course_progress', function (JoinClause $join) use ($userId) {
$join->on('topics.id', '=', 'course_progress.topic_id')
->where('course_progress.user_id', $userId);
})
->whereColumn('lessons.course_id', 'courses.id')
->where('course_progress.status', ProgressStatus::IN_PROGRESS);
})
->orWhereExists(function (QueryBuilder $query) use ($userId) {
$query->select(DB::raw(1))
->from('topics')
->join('lessons', 'lessons.id', '=', 'topics.lesson_id')
->leftJoin('course_progress', function (JoinClause $join) use ($userId) {
$join->on('topics.id', '=', 'course_progress.topic_id')
->where('course_progress.user_id', $userId);
})
->whereColumn('lessons.course_id', 'courses.id')
->whereIn('course_progress.status', [ProgressStatus::COMPLETE, ProgressStatus::INCOMPLETE])
->groupBy('lessons.course_id')
->havingRaw('COUNT(DISTINCT course_progress.status) = 2');
});
});
}
Expand Down
30 changes: 29 additions & 1 deletion tests/APIs/CourseProgressApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,34 @@ public function test_show_progress_courses_paginated_filtered_started(): void
'status' => ProgressStatus::COMPLETE,
]);

$course3 = Course::factory()->create([
'status' => CourseStatusEnum::PUBLISHED,
'title' => 'B Course',
]);
$lesson3 = Lesson::factory()->create(['course_id' => $course2->getKey()]);
$topic4 = Topic::factory()->create(['lesson_id' => $lesson3->getKey(), 'active' => true]);
$topic5 = Topic::factory()->create(['lesson_id' => $lesson3->getKey(), 'active' => true]);

CourseProgress::factory()->create([
'user_id' => $user->getKey(),
'topic_id' => $topic4->getKey(),
'finished_at' => now(),
'seconds' => 50,
'status' => ProgressStatus::COMPLETE,
]);

CourseProgress::factory()->create([
'user_id' => $user->getKey(),
'topic_id' => $topic5->getKey(),
'finished_at' => now(),
'seconds' => 50,
'status' => ProgressStatus::INCOMPLETE,
]);


$user->courses()->save($course1); //Course A
$user->courses()->save($course2); //Course B
$user->courses()->save($course3); //Course C

$this->response = $this->actingAs($user, 'api')->json(
'GET',
Expand All @@ -389,9 +414,12 @@ public function test_show_progress_courses_paginated_filtered_started(): void
);

$this->response
->assertJsonCount(1, 'data')
->assertJsonCount(2, 'data')
->assertJsonFragment([
'title' => $course1->title,
])
->assertJsonFragment([
'title' => $course3->title,
]);
}

Expand Down

0 comments on commit fe635ac

Please sign in to comment.