feature: main page - always show current user at the bottom of ranking
This commit is contained in:
@@ -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
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
<div class="flex-1 flex flex-col justify-between min-h-[340px]">
|
||||
@if(count($rankers) > 0)
|
||||
<div class="flex flex-col">
|
||||
@foreach($rankers as $item)
|
||||
<x-ranking-item
|
||||
rank="{{ $item->rank }}"
|
||||
@@ -17,11 +19,19 @@
|
||||
: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 có xếp hạng cho tháng này</p>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if($currentUserRankItem)
|
||||
<div class="mt-auto">
|
||||
<div class="my-3 border-t border-dashed border-gray-100 mx-2"></div>
|
||||
<x-ranking-item
|
||||
rank="{{ $currentUserRankItem->rank }}"
|
||||
rank="{{ $currentUserRankItem->rank > 0 ? $currentUserRankItem->rank : '-' }}"
|
||||
name="{{ $currentUserRankItem->name }}"
|
||||
team="{{ match((int) $currentUserRankItem->departments) {
|
||||
1 => 'Dev Team',
|
||||
@@ -36,10 +46,6 @@
|
||||
unit="thẻ"
|
||||
:isCurrent="true"
|
||||
/>
|
||||
@endif
|
||||
@else
|
||||
<div class="py-12 text-center">
|
||||
<span class="text-[36px]">🏆</span>
|
||||
<p class="text-gray-400 font-medium text-[13px] mt-2">Chưa có xếp hạng cho tháng này</p>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user