feature: user list
This commit is contained in:
@@ -35,8 +35,21 @@ class AdminService implements AdminServiceInterface
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
]);
|
||||
|
||||
$topReceivedUser = (clone $usersQuery)->where('status', User::STATUS_ACTIVE)->orderByDesc('total_received')->first();
|
||||
$topSentUser = (clone $usersQuery)->where('status', User::STATUS_ACTIVE)->orderByDesc('total_sent')->first();
|
||||
$activeUsers = (clone $usersQuery)->where('status', config('constants.STATUS_ACTIVE'))->get();
|
||||
|
||||
$maxReceived = $activeUsers->max('total_received') ?? 0;
|
||||
$maxSent = $activeUsers->max('total_sent') ?? 0;
|
||||
|
||||
$topReceivedUsers = $maxReceived > 0
|
||||
? $activeUsers->filter(fn($u) => $u->total_received == $maxReceived)->values()
|
||||
: collect();
|
||||
|
||||
$topSentUsers = $maxSent > 0
|
||||
? $activeUsers->filter(fn($u) => $u->total_sent == $maxSent)->values()
|
||||
: collect();
|
||||
|
||||
$topReceivedUser = $topReceivedUsers->first();
|
||||
$topSentUser = $topSentUsers->first();
|
||||
|
||||
if (!is_null($status) && $status !== '') {
|
||||
$usersQuery->where('status', intval($status));
|
||||
@@ -47,7 +60,7 @@ class AdminService implements AdminServiceInterface
|
||||
}
|
||||
|
||||
if (!is_null($department) && $department !== '') {
|
||||
$usersQuery->where('departments', intval($department));
|
||||
$usersQuery->where('departments', $department);
|
||||
}
|
||||
|
||||
if (!is_null($flagSend) && $flagSend !== '') {
|
||||
@@ -62,10 +75,9 @@ class AdminService implements AdminServiceInterface
|
||||
->orWhereRaw('LOWER(mail) LIKE ?', ['%' . strtolower($search) . '%']);
|
||||
});
|
||||
}
|
||||
|
||||
$users = $usersQuery->paginate(20)->withQueryString();
|
||||
|
||||
return compact('users', 'topReceivedUser', 'topSentUser');
|
||||
return compact('users', 'topReceivedUser', 'topSentUser', 'topReceivedUsers', 'topSentUsers');
|
||||
}
|
||||
|
||||
public function getUserTransactions(string $msnv, string $selectedMonth): LengthAwarePaginator
|
||||
@@ -89,10 +101,10 @@ class AdminService implements AdminServiceInterface
|
||||
'pass' => md5($data['password']),
|
||||
'departments' => $data['departments'],
|
||||
'role' => $data['role'],
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'status' => config('constants.STATUS_ACTIVE'),
|
||||
'card' => 0,
|
||||
'flag_send' => User::FLAG_SEND_DISABLED,
|
||||
'first_login' => $data['role'] == User::ROLE_ADMIN ? User::FIRST_LOGIN_FALSE : User::FIRST_LOGIN_TRUE,
|
||||
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
|
||||
'first_login' => $data['role'] == config('constants.ROLE_ADMIN') ? config('constants.FIRST_LOGIN_FALSE') : config('constants.FIRST_LOGIN_TRUE'),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -101,17 +113,24 @@ class AdminService implements AdminServiceInterface
|
||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
||||
|
||||
DB::transaction(function () use ($data, $user) {
|
||||
if (!empty($data['num_card'])) {
|
||||
$newCard = intval($data['card']);
|
||||
$oldCard = intval($user->card);
|
||||
|
||||
if ($newCard > $oldCard) {
|
||||
$diff = $newCard - $oldCard;
|
||||
AddCard::create([
|
||||
'buyer' => $user->msnv,
|
||||
'num_card' => $data['num_card'],
|
||||
'num_card' => $diff,
|
||||
'seller' => Auth::user()->msnv,
|
||||
'date' => Carbon::today(),
|
||||
]);
|
||||
$user->card += $data['num_card'];
|
||||
}
|
||||
|
||||
$user->flag_send = isset($data['flag_send']) ? User::FLAG_SEND_ENABLED : User::FLAG_SEND_DISABLED;
|
||||
$user->card = $newCard;
|
||||
$user->role = intval($data['role']);
|
||||
$user->status = intval($data['status']);
|
||||
$user->flag_send = intval($data['flag_send']);
|
||||
$user->first_login = intval($data['first_login']);
|
||||
$user->save();
|
||||
});
|
||||
}
|
||||
@@ -119,12 +138,50 @@ class AdminService implements AdminServiceInterface
|
||||
public function deactivateUser(string $msnv): void
|
||||
{
|
||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
||||
$user->status = User::STATUS_INACTIVE;
|
||||
$user->status = config('constants.STATUS_INACTIVE');
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function resetAllCards(): void
|
||||
{
|
||||
User::where('status', User::STATUS_ACTIVE)->update(['card' => 0]);
|
||||
User::where('status', config('constants.STATUS_ACTIVE'))->update(['card' => 0]);
|
||||
}
|
||||
|
||||
public function updateAddCard(int $id, array $data): void
|
||||
{
|
||||
$addCard = AddCard::findOrFail($id);
|
||||
|
||||
|
||||
$currentMonth = Carbon::now()->format('Y-m');
|
||||
$recordMonth = Carbon::parse($addCard->date)->format('Y-m');
|
||||
|
||||
if ($recordMonth !== $currentMonth) {
|
||||
throw new \Exception('Không cho phép chỉnh sửa dữ liệu card của các tháng trước.');
|
||||
}
|
||||
|
||||
$newDateMonth = Carbon::parse($data['date'])->format('Y-m');
|
||||
if ($newDateMonth !== $currentMonth) {
|
||||
throw new \Exception('Chỉ được phép chỉnh sửa dữ liệu của tháng hiện tại.');
|
||||
}
|
||||
|
||||
$user = User::where('msnv', $addCard->buyer)->firstOrFail();
|
||||
|
||||
DB::transaction(function () use ($addCard, $user, $data) {
|
||||
$oldNumCard = intval($addCard->num_card);
|
||||
$newNumCard = intval($data['num_card']);
|
||||
$diff = $newNumCard - $oldNumCard;
|
||||
|
||||
if ($user->card + $diff < 0) {
|
||||
throw new \Exception('Số lượng card cập nhật không hợp lệ vì tổng số card của user không được nhỏ hơn 0.');
|
||||
}
|
||||
|
||||
$user->card += $diff;
|
||||
$user->save();
|
||||
|
||||
$addCard->num_card = $newNumCard;
|
||||
$addCard->seller = $data['seller'];
|
||||
$addCard->date = $data['date'];
|
||||
$addCard->save();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,6 @@ interface AdminServiceInterface
|
||||
public function deactivateUser(string $msnv): void;
|
||||
|
||||
public function resetAllCards(): void;
|
||||
|
||||
public function updateAddCard(int $id, array $data): void;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class AuthService implements AuthServiceInterface
|
||||
// Log the user in manually
|
||||
Auth::login($user);
|
||||
$request->session()->regenerate();
|
||||
if ($user->status != User::STATUS_ACTIVE) {
|
||||
if ($user->status != config('constants.STATUS_ACTIVE')) {
|
||||
Auth::logout();
|
||||
return ['success' => false, 'error_key' => 'permission', 'error_msg' => 'Bạn không có quyền truy cập. Vui lòng liên hệ quản trị viên'];
|
||||
}
|
||||
@@ -37,11 +37,11 @@ class AuthService implements AuthServiceInterface
|
||||
|
||||
public function getRedirectRouteForUser(User $user): string
|
||||
{
|
||||
if ($user->role == User::ROLE_ADMIN) {
|
||||
if ($user->role == config('constants.ROLE_ADMIN')) {
|
||||
return route('admin.dashboard');
|
||||
}
|
||||
|
||||
if ($user->first_login == User::FIRST_LOGIN_TRUE) {
|
||||
if ($user->first_login == config('constants.FIRST_LOGIN_TRUE')) {
|
||||
return route('user.change_password');
|
||||
}
|
||||
|
||||
|
||||
@@ -27,19 +27,19 @@ class UserService implements UserServiceInterface
|
||||
|
||||
public function getOtherActiveUsers(User $currentUser): Collection
|
||||
{
|
||||
return User::where('status', User::STATUS_ACTIVE)
|
||||
return User::where('status', config('constants.STATUS_ACTIVE'))
|
||||
->where('msnv', '!=', $currentUser->msnv)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void
|
||||
{
|
||||
if ($sender->flag_send == User::FLAG_SEND_DISABLED) {
|
||||
if ($sender->flag_send == config('constants.FLAG_SEND_DISABLED')) {
|
||||
throw new \RuntimeException(__('messages.error.no_send_permission'));
|
||||
}
|
||||
|
||||
$receiver = User::where('msnv', $receiverMsnv)->first();
|
||||
if (!$receiver || $receiver->status != User::STATUS_ACTIVE) {
|
||||
if (!$receiver || $receiver->status != config('constants.STATUS_ACTIVE')) {
|
||||
throw new \RuntimeException(__('messages.error.receiver_invalid'));
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class UserService implements UserServiceInterface
|
||||
public function updatePassword(User $user, string $newPassword): void
|
||||
{
|
||||
$user->pass = md5($newPassword);
|
||||
$user->first_login = User::FIRST_LOGIN_FALSE;
|
||||
$user->first_login = config('constants.FIRST_LOGIN_FALSE');
|
||||
$user->save();
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ class UserService implements UserServiceInterface
|
||||
->sum('sent');
|
||||
|
||||
// Calculate current rank (based on received)
|
||||
$rankings = User::where('status', User::STATUS_ACTIVE)
|
||||
$rankings = User::where('status', config('constants.STATUS_ACTIVE'))
|
||||
->addSelect([
|
||||
'score' => Administration::selectRaw('COALESCE(SUM(received), 0)')
|
||||
->whereColumn('msnv', 'user.msnv')
|
||||
@@ -156,7 +156,7 @@ class UserService implements UserServiceInterface
|
||||
|
||||
$column = $type === 'sent' ? 'sent' : 'received';
|
||||
|
||||
$allActiveUsers = User::where('status', User::STATUS_ACTIVE)
|
||||
$allActiveUsers = User::where('status', config('constants.STATUS_ACTIVE'))
|
||||
->addSelect([
|
||||
'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)')
|
||||
->whereColumn('msnv', 'user.msnv')
|
||||
|
||||
Reference in New Issue
Block a user