m
This commit is contained in:
@@ -31,22 +31,52 @@ class UserController extends Controller
|
||||
$user = Auth::user();
|
||||
|
||||
$administrations = $this->userService->getDashboard($user, $selectedMonth);
|
||||
$stats = $this->userService->getPersonalStats($user, $selectedMonth);
|
||||
|
||||
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
||||
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
|
||||
$isAdmin = $user->role == config('constants.ROLE_ADMIN');
|
||||
|
||||
if ($isAdmin) {
|
||||
$stats = $this->userService->getPersonalStats($user, $selectedMonth);
|
||||
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
||||
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
|
||||
|
||||
$receivedCount = (int) $stats['receivedCount'];
|
||||
$sentCount = (int) $stats['sentCount'];
|
||||
$currentRank = (int) $stats['currentRank'];
|
||||
$receivedRankers = $receivedRanking['rankers'];
|
||||
$receivedCurrentUserRankItem = $receivedRanking['currentUserRankItem'];
|
||||
$sentRankers = $sentRanking['rankers'];
|
||||
$sentCurrentUserRankItem = $sentRanking['currentUserRankItem'];
|
||||
} else {
|
||||
// For member, do NOT fetch ranking lists or calculate rank to ensure security and improve DB performance
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
$receivedCount = (int) \App\Models\Administration::where('msnv', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('received');
|
||||
|
||||
$sentCount = (int) \App\Models\Administration::where('msnv', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('sent');
|
||||
|
||||
$currentRank = 0;
|
||||
$receivedRankers = [];
|
||||
$receivedCurrentUserRankItem = null;
|
||||
$sentRankers = [];
|
||||
$sentCurrentUserRankItem = null;
|
||||
}
|
||||
|
||||
$dashboardData = new DashboardDataDto(
|
||||
administrations: $administrations,
|
||||
selectedMonth: $selectedMonth,
|
||||
user: $user,
|
||||
receivedRankers: $receivedRanking['rankers'],
|
||||
receivedCurrentUserRankItem: $receivedRanking['currentUserRankItem'],
|
||||
sentRankers: $sentRanking['rankers'],
|
||||
sentCurrentUserRankItem: $sentRanking['currentUserRankItem'],
|
||||
receivedCount: (int) $stats['receivedCount'],
|
||||
sentCount: (int) $stats['sentCount'],
|
||||
currentRank: (int) $stats['currentRank']
|
||||
receivedRankers: $receivedRankers,
|
||||
receivedCurrentUserRankItem: $receivedCurrentUserRankItem,
|
||||
sentRankers: $sentRankers,
|
||||
sentCurrentUserRankItem: $sentCurrentUserRankItem,
|
||||
receivedCount: $receivedCount,
|
||||
sentCount: $sentCount,
|
||||
currentRank: $currentRank
|
||||
);
|
||||
|
||||
return view('user.dashboard', $dashboardData->toArray());
|
||||
@@ -86,7 +116,8 @@ class UserController extends Controller
|
||||
$this->userService->sendThankcards(
|
||||
Auth::user(),
|
||||
(string) $request->input('receiver'),
|
||||
(int) $request->input('amount')
|
||||
(int) $request->input('amount'),
|
||||
(int) $request->input('template_id', 1)
|
||||
);
|
||||
} catch (ThankCardException $e) {
|
||||
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
|
||||
@@ -175,8 +206,12 @@ class UserController extends Controller
|
||||
}
|
||||
|
||||
// New page: ranking (both received and sent) with tabs
|
||||
public function ranking(Request $request): View
|
||||
public function ranking(Request $request): \Illuminate\View\View
|
||||
{
|
||||
if (Auth::user()->role !== config('constants.ROLE_ADMIN')) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
|
||||
$user = Auth::user();
|
||||
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
||||
|
||||
@@ -27,6 +27,7 @@ class SendThankCardRequest extends FormRequest
|
||||
Rule::exists('user', 'msnv')->where('status', config('constants.STATUS_ACTIVE'))
|
||||
],
|
||||
'amount' => 'required|integer|min:1|max:' . Administration::MAX_SEND_CARD_PER_MONTH,
|
||||
'template_id' => 'nullable|integer|in:1,2,3,4',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ class Administration extends Model
|
||||
'sent',
|
||||
'receiver',
|
||||
'date',
|
||||
'template_id',
|
||||
];
|
||||
|
||||
public function senderUser()
|
||||
|
||||
@@ -11,7 +11,7 @@ interface UserServiceInterface
|
||||
|
||||
public function getOtherActiveUsers(User $currentUser): \Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void;
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount, ?int $templateId = 1): void;
|
||||
|
||||
public function updatePassword(User $user, string $newPassword): void;
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ class UserService implements UserServiceInterface
|
||||
->get();
|
||||
}
|
||||
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount, ?int $templateId = 1): void
|
||||
{
|
||||
if ($sender->flag_send == config('constants.FLAG_SEND_DISABLED')) {
|
||||
throw new \App\Exceptions\ThankCardException(__('messages.error.no_send_permission'));
|
||||
@@ -47,7 +47,7 @@ class UserService implements UserServiceInterface
|
||||
$startOfMonth = Carbon::now()->startOfMonth();
|
||||
$endOfMonth = Carbon::now()->endOfMonth();
|
||||
|
||||
DB::transaction(function () use ($sender, $receiverMsnv, $amount, $startOfMonth, $endOfMonth) {
|
||||
DB::transaction(function () use ($sender, $receiverMsnv, $amount, $startOfMonth, $endOfMonth, $templateId) {
|
||||
// Lock sender record to prevent concurrent transaction modifications
|
||||
$lockedSender = User::where('id', $sender->id)->lockForUpdate()->first();
|
||||
|
||||
@@ -76,21 +76,23 @@ class UserService implements UserServiceInterface
|
||||
}
|
||||
|
||||
Administration::create([
|
||||
'msnv' => $receiverMsnv,
|
||||
'received' => $amount,
|
||||
'sender' => $lockedSender->msnv,
|
||||
'sent' => 0,
|
||||
'receiver' => null,
|
||||
'date' => Carbon::today(),
|
||||
'msnv' => $receiverMsnv,
|
||||
'received' => $amount,
|
||||
'sender' => $lockedSender->msnv,
|
||||
'sent' => 0,
|
||||
'receiver' => null,
|
||||
'date' => Carbon::today(),
|
||||
'template_id' => $templateId,
|
||||
]);
|
||||
|
||||
Administration::create([
|
||||
'msnv' => $lockedSender->msnv,
|
||||
'received' => 0,
|
||||
'sender' => null,
|
||||
'sent' => $amount,
|
||||
'receiver' => $receiverMsnv,
|
||||
'date' => Carbon::today(),
|
||||
'msnv' => $lockedSender->msnv,
|
||||
'received' => 0,
|
||||
'sender' => null,
|
||||
'sent' => $amount,
|
||||
'receiver' => $receiverMsnv,
|
||||
'date' => Carbon::today(),
|
||||
'template_id' => $templateId,
|
||||
]);
|
||||
|
||||
$lockedSender->card -= $amount;
|
||||
|
||||
Reference in New Issue
Block a user