Skip to content

Commit

Permalink
Don't show Users' without XP on Leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmellor committed Jul 13, 2024
1 parent db03ab9 commit 05fed85
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 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
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 05fed85

Please sign in to comment.