Skip to content

Commit

Permalink
Merge pull request #86 from cjmellor/fix/dont-show-users-without-xp-o…
Browse files Browse the repository at this point in the history
…n-leaderboard

fix: Users without XP don't show in Leaderboard
  • Loading branch information
cjmellor committed Jul 13, 2024
2 parents db03ab9 + c3b5a91 commit 1131797
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Services/LeaderboardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function generate(bool $paginate = false, ?int $limit = null): array|Coll
{
return $this->userModel::query()
->with(relations: ['experience'])
->whereHas('experience', fn (Builder $query) => $query->whereNotNull(columns: 'experience_points'))
->orderByDesc(
column: Experience::select('experience_points')
->whereColumn(config('level-up.user.foreign_key'), config('level-up.user.users_table').'.id')
Expand Down
2 changes: 1 addition & 1 deletion tests/Concerns/HasStreaksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
'user_id' => $this->user->id,
'activity_id' => $this->activity->id,
'count' => 1,
'activity_at' => now(),
// 'activity_at' => now(),
]);
});

Expand Down
13 changes: 10 additions & 3 deletions tests/Services/LeaderboardServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
});

it(description: 'returns the correct data in the correct order', closure: function () {
// A User is also created in Pest.php, so we have 5 Users in total.
tap(User::newFactory()->create())->addPoints(44);
tap(User::newFactory()->create())->addPoints(123);
tap(User::newFactory()->create())->addPoints(198);
Expand All @@ -22,6 +21,14 @@
->pluck(value: 'experience.experience_points')
->toArray()
)
->toBe([245, 198, 123, 44, null])
->toHaveCount(count: 5);
->toBe([245, 198, 123, 44])
->toHaveCount(count: 4);
});

it(description: 'only shows users with experience points', closure: function () {
tap(User::newFactory()->create());
$userWithPoints = tap(User::newFactory()->create())->addPoints(44);

expect(Leaderboard::generate())->toHaveCount(count: 1)
->and(Leaderboard::generate()->first()->id)->toEqual($userWithPoints->id);
});

0 comments on commit 1131797

Please sign in to comment.