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';
|
$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,4 +1,6 @@
|
|||||||
@if(count($rankers) > 0)
|
<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)
|
@foreach($rankers as $item)
|
||||||
<x-ranking-item
|
<x-ranking-item
|
||||||
rank="{{ $item->rank }}"
|
rank="{{ $item->rank }}"
|
||||||
@@ -17,11 +19,19 @@
|
|||||||
:isCurrent="$item->msnv == Auth::user()->msnv"
|
:isCurrent="$item->msnv == Auth::user()->msnv"
|
||||||
/>
|
/>
|
||||||
@endforeach
|
@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)
|
@if($currentUserRankItem)
|
||||||
|
<div class="mt-auto">
|
||||||
<div class="my-3 border-t border-dashed border-gray-100 mx-2"></div>
|
<div class="my-3 border-t border-dashed border-gray-100 mx-2"></div>
|
||||||
<x-ranking-item
|
<x-ranking-item
|
||||||
rank="{{ $currentUserRankItem->rank }}"
|
rank="{{ $currentUserRankItem->rank > 0 ? $currentUserRankItem->rank : '-' }}"
|
||||||
name="{{ $currentUserRankItem->name }}"
|
name="{{ $currentUserRankItem->name }}"
|
||||||
team="{{ match((int) $currentUserRankItem->departments) {
|
team="{{ match((int) $currentUserRankItem->departments) {
|
||||||
1 => 'Dev Team',
|
1 => 'Dev Team',
|
||||||
@@ -36,10 +46,6 @@
|
|||||||
unit="thẻ"
|
unit="thẻ"
|
||||||
:isCurrent="true"
|
: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>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
</div>
|
||||||
|
|||||||
@@ -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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user