diff --git a/app/Services/User/UserService.php b/app/Services/User/UserService.php index 465f4af..f595a91 100644 --- a/app/Services/User/UserService.php +++ b/app/Services/User/UserService.php @@ -156,14 +156,15 @@ class UserService implements UserServiceInterface $column = $type === 'sent' ? 'sent' : 'received'; - $users = User::where('status', User::STATUS_ACTIVE) + $allActiveUsers = User::where('status', User::STATUS_ACTIVE) ->addSelect([ 'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)') ->whereColumn('msnv', 'user.msnv') ->whereBetween('date', [$startOfMonth, $endOfMonth]) ]) - ->get() - ->filter(fn($u) => $u->score > 0) + ->get(); + + $rankedUsers = $allActiveUsers->filter(fn($u) => $u->score > 0) ->sortBy([ ['score', 'desc'], ['msnv', 'asc'] @@ -177,7 +178,7 @@ class UserService implements UserServiceInterface $rankers = []; $currentUserRankItem = null; - foreach ($users as $index => $userItem) { + foreach ($rankedUsers as $index => $userItem) { $score = (int) $userItem->score; if ($score !== $prevScore) { $denseRank = $rank; @@ -197,17 +198,18 @@ class UserService implements UserServiceInterface $rank++; } - $showCurrentUserAtBottom = false; - if ($currentUserRankItem) { - $currentUserIndex = $users->search(fn($u) => $u->msnv == $currentUser->msnv); - if ($currentUserIndex !== false && $currentUserIndex >= 4) { - $showCurrentUserAtBottom = true; + if (!$currentUserRankItem) { + $currentUserModel = $allActiveUsers->first(fn($u) => $u->msnv == $currentUser->msnv); + if ($currentUserModel) { + $currentUserRankItem = clone $currentUserModel; + $currentUserRankItem->score = 0; + $currentUserRankItem->rank = 0; } } return [ 'rankers' => $rankers, - 'currentUserRankItem' => $showCurrentUserAtBottom ? $currentUserRankItem : null + 'currentUserRankItem' => $currentUserRankItem ]; } } \ No newline at end of file diff --git a/resources/views/user/partials/ranking_list.blade.php b/resources/views/user/partials/ranking_list.blade.php index f1a63a7..c6e883f 100644 --- a/resources/views/user/partials/ranking_list.blade.php +++ b/resources/views/user/partials/ranking_list.blade.php @@ -1,45 +1,51 @@ -@if(count($rankers) > 0) - @foreach($rankers as $item) - - @endforeach +
+ @if(count($rankers) > 0) +
+ @foreach($rankers as $item) + + @endforeach +
+ @else +
+ 🏆 +

Chưa có xếp hạng cho tháng này

+
+ @endif @if($currentUserRankItem) -
- +
+
+ +
@endif -@else -
- 🏆 -

Chưa có xếp hạng cho tháng này

-
-@endif +
diff --git a/tests/Feature/UserDashboardLayoutTest.php b/tests/Feature/UserDashboardLayoutTest.php index f3f58e4..3f34aa7 100644 --- a/tests/Feature/UserDashboardLayoutTest.php +++ b/tests/Feature/UserDashboardLayoutTest.php @@ -209,4 +209,36 @@ class UserDashboardLayoutTest extends TestCase $response->assertSee('Active User', false); $response->assertDontSee('Inactive User', false); } + + public function test_dashboard_ranking_current_user_always_shown_at_bottom(): void + { + $user = \App\Models\User::create([ + 'msnv' => 5001, + 'name' => 'User A', + 'mail' => 'usera@runsystem.net', + 'pass' => md5('password'), + 'departments' => 1, + 'role' => \App\Models\User::ROLE_MEMBER, + 'status' => \App\Models\User::STATUS_ACTIVE, + 'card' => 10, + 'flag_send' => \App\Models\User::FLAG_SEND_ENABLED, + 'first_login' => \App\Models\User::FIRST_LOGIN_FALSE, + ]); + + \App\Models\Administration::create([ + 'msnv' => 5001, + 'received' => 10, + 'sender' => 9999, + 'sent' => 0, + 'receiver' => null, + 'date' => \Carbon\Carbon::now()->format('Y-m-d') + ]); + + $this->actingAs($user); + + $response = $this->get('/my-page'); + $response->assertOk(); + + $this->assertGreaterThan(1, substr_count($response->getContent(), 'User A')); + } }