feature: main page - always show current user at the bottom of ranking

This commit is contained in:
antv
2026-07-06 16:54:41 +07:00
parent e163d2f349
commit 1e050233b9
3 changed files with 92 additions and 52 deletions
+12 -10
View File
@@ -156,14 +156,15 @@ class UserService implements UserServiceInterface
$column = $type === 'sent' ? 'sent' : 'received'; $column = $type === 'sent' ? 'sent' : 'received';
$users = User::where('status', User::STATUS_ACTIVE) $allActiveUsers = User::where('status', User::STATUS_ACTIVE)
->addSelect([ ->addSelect([
'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)') 'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)')
->whereColumn('msnv', 'user.msnv') ->whereColumn('msnv', 'user.msnv')
->whereBetween('date', [$startOfMonth, $endOfMonth]) ->whereBetween('date', [$startOfMonth, $endOfMonth])
]) ])
->get() ->get();
->filter(fn($u) => $u->score > 0)
$rankedUsers = $allActiveUsers->filter(fn($u) => $u->score > 0)
->sortBy([ ->sortBy([
['score', 'desc'], ['score', 'desc'],
['msnv', 'asc'] ['msnv', 'asc']
@@ -177,7 +178,7 @@ class UserService implements UserServiceInterface
$rankers = []; $rankers = [];
$currentUserRankItem = null; $currentUserRankItem = null;
foreach ($users as $index => $userItem) { foreach ($rankedUsers as $index => $userItem) {
$score = (int) $userItem->score; $score = (int) $userItem->score;
if ($score !== $prevScore) { if ($score !== $prevScore) {
$denseRank = $rank; $denseRank = $rank;
@@ -197,17 +198,18 @@ class UserService implements UserServiceInterface
$rank++; $rank++;
} }
$showCurrentUserAtBottom = false; if (!$currentUserRankItem) {
if ($currentUserRankItem) { $currentUserModel = $allActiveUsers->first(fn($u) => $u->msnv == $currentUser->msnv);
$currentUserIndex = $users->search(fn($u) => $u->msnv == $currentUser->msnv); if ($currentUserModel) {
if ($currentUserIndex !== false && $currentUserIndex >= 4) { $currentUserRankItem = clone $currentUserModel;
$showCurrentUserAtBottom = true; $currentUserRankItem->score = 0;
$currentUserRankItem->rank = 0;
} }
} }
return [ return [
'rankers' => $rankers, 'rankers' => $rankers,
'currentUserRankItem' => $showCurrentUserAtBottom ? $currentUserRankItem : null 'currentUserRankItem' => $currentUserRankItem
]; ];
} }
} }
@@ -1,45 +1,51 @@
@if(count($rankers) > 0) <div class="flex-1 flex flex-col justify-between min-h-[340px]">
@foreach($rankers as $item) @if(count($rankers) > 0)
<x-ranking-item <div class="flex flex-col">
rank="{{ $item->rank }}" @foreach($rankers as $item)
name="{{ $item->name }}" <x-ranking-item
team="{{ match((int) $item->departments) { rank="{{ $item->rank }}"
1 => 'Dev Team', name="{{ $item->name }}"
2 => 'HR Team', team="{{ match((int) $item->departments) {
3 => 'Sale Team', 1 => 'Dev Team',
4 => 'Board Team', 2 => 'HR Team',
5 => 'Marketing Team', 3 => 'Sale Team',
default => 'Team' 4 => 'Board Team',
} }}" 5 => 'Marketing Team',
avatar="{{ $item->avatar ?? 'https://api.dicebear.com/7.x/notionists/svg?seed=' . urlencode($item->name) . '&backgroundColor=bfdbfe' }}" default => 'Team'
score="{{ $item->score }}" } }}"
unit="thẻ" avatar="{{ $item->avatar ?? 'https://api.dicebear.com/7.x/notionists/svg?seed=' . urlencode($item->name) . '&backgroundColor=bfdbfe' }}"
:isCurrent="$item->msnv == Auth::user()->msnv" score="{{ $item->score }}"
/> unit="thẻ"
@endforeach :isCurrent="$item->msnv == Auth::user()->msnv"
/>
@endforeach
</div>
@else
<div class="flex-1 flex flex-col justify-center items-center py-6 text-center">
<span class="text-[36px]">🏆</span>
<p class="text-gray-400 font-medium text-[13px] mt-2">Chưa xếp hạng cho tháng này</p>
</div>
@endif
@if($currentUserRankItem) @if($currentUserRankItem)
<div class="my-3 border-t border-dashed border-gray-100 mx-2"></div> <div class="mt-auto">
<x-ranking-item <div class="my-3 border-t border-dashed border-gray-100 mx-2"></div>
rank="{{ $currentUserRankItem->rank }}" <x-ranking-item
name="{{ $currentUserRankItem->name }}" rank="{{ $currentUserRankItem->rank > 0 ? $currentUserRankItem->rank : '-' }}"
team="{{ match((int) $currentUserRankItem->departments) { name="{{ $currentUserRankItem->name }}"
1 => 'Dev Team', team="{{ match((int) $currentUserRankItem->departments) {
2 => 'HR Team', 1 => 'Dev Team',
3 => 'Sale Team', 2 => 'HR Team',
4 => 'Board Team', 3 => 'Sale Team',
5 => 'Marketing Team', 4 => 'Board Team',
default => 'Team' 5 => 'Marketing Team',
} }}" default => 'Team'
avatar="{{ $currentUserRankItem->avatar ?? 'https://api.dicebear.com/7.x/notionists/svg?seed=' . urlencode($currentUserRankItem->name) . '&backgroundColor=bfdbfe' }}" } }}"
score="{{ $currentUserRankItem->score }}" avatar="{{ $currentUserRankItem->avatar ?? 'https://api.dicebear.com/7.x/notionists/svg?seed=' . urlencode($currentUserRankItem->name) . '&backgroundColor=bfdbfe' }}"
unit="thẻ" score="{{ $currentUserRankItem->score }}"
:isCurrent="true" unit="thẻ"
/> :isCurrent="true"
/>
</div>
@endif @endif
@else </div>
<div class="py-12 text-center">
<span class="text-[36px]">🏆</span>
<p class="text-gray-400 font-medium text-[13px] mt-2">Chưa xếp hạng cho tháng này</p>
</div>
@endif
+32
View File
@@ -209,4 +209,36 @@ class UserDashboardLayoutTest extends TestCase
$response->assertSee('Active User', false); $response->assertSee('Active User', false);
$response->assertDontSee('Inactive 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'));
}
} }