refactor: refactor controllers, services, validation and introduce DTOs, FormRequests and domain exceptions
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\DTOs;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
|
|
||||||
|
class DashboardDataDto
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public readonly LengthAwarePaginator $administrations,
|
||||||
|
public readonly string $selectedMonth,
|
||||||
|
public readonly User $user,
|
||||||
|
public readonly array $receivedRankers,
|
||||||
|
public readonly ?object $receivedCurrentUserRankItem,
|
||||||
|
public readonly array $sentRankers,
|
||||||
|
public readonly ?object $sentCurrentUserRankItem,
|
||||||
|
public readonly int $receivedCount,
|
||||||
|
public readonly int $sentCount,
|
||||||
|
public readonly int $currentRank
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the DTO to an associative array for view rendering.
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'administrations' => $this->administrations,
|
||||||
|
'selectedMonth' => $this->selectedMonth,
|
||||||
|
'user' => $this->user,
|
||||||
|
'receivedRankers' => $this->receivedRankers,
|
||||||
|
'receivedCurrentUserRankItem' => $this->receivedCurrentUserRankItem,
|
||||||
|
'sentRankers' => $this->sentRankers,
|
||||||
|
'sentCurrentUserRankItem' => $this->sentCurrentUserRankItem,
|
||||||
|
'receivedCount' => $this->receivedCount,
|
||||||
|
'sentCount' => $this->sentCount,
|
||||||
|
'currentRank' => $this->currentRank,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\DTOs;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class UserFilterDto
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public readonly string $selectedMonth,
|
||||||
|
public readonly ?string $search = null,
|
||||||
|
public readonly ?string $status = '1',
|
||||||
|
public readonly ?string $role = null,
|
||||||
|
public readonly ?string $department = null,
|
||||||
|
public readonly ?string $flagSend = null
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a DTO instance from an HTTP Request.
|
||||||
|
*/
|
||||||
|
public static function fromRequest(Request $request): self
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
selectedMonth: (string) $request->input('month', Carbon::now()->format('Y-m')),
|
||||||
|
search: $request->input('search'),
|
||||||
|
status: $request->has('status') ? (string) $request->input('status') : '1',
|
||||||
|
role: $request->input('role'),
|
||||||
|
department: $request->input('department'),
|
||||||
|
flagSend: $request->input('flag_send')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class ThankCardException extends RuntimeException
|
||||||
|
{
|
||||||
|
// Custom domain exception for thank card operations
|
||||||
|
}
|
||||||
@@ -3,25 +3,29 @@
|
|||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\User;
|
use App\Http\Requests\Admin\StoreUserRequest;
|
||||||
|
use App\Http\Requests\Admin\UpdateUserRequest;
|
||||||
|
use App\Http\Requests\Admin\UpdateAddCardRequest;
|
||||||
|
use App\DTOs\UserFilterDto;
|
||||||
use App\Services\Admin\Contracts\AdminServiceInterface;
|
use App\Services\Admin\Contracts\AdminServiceInterface;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
class AdminController extends Controller
|
class AdminController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private AdminServiceInterface $adminService
|
private readonly AdminServiceInterface $adminService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function index(Request $request)
|
/**
|
||||||
|
* Display a listing of the users with statistics.
|
||||||
|
*/
|
||||||
|
public function index(Request $request): View|JsonResponse
|
||||||
{
|
{
|
||||||
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
|
$filters = UserFilterDto::fromRequest($request);
|
||||||
$search = $request->input('search');
|
|
||||||
$status = $request->input('status', (string)config('constants.STATUS_ACTIVE'));
|
|
||||||
$role = $request->input('role');
|
|
||||||
$department = $request->input('department');
|
|
||||||
$flagSend = $request->input('flag_send');
|
|
||||||
|
|
||||||
[
|
[
|
||||||
'users' => $users,
|
'users' => $users,
|
||||||
@@ -29,11 +33,11 @@ class AdminController extends Controller
|
|||||||
'topSentUser' => $topSentUser,
|
'topSentUser' => $topSentUser,
|
||||||
'topReceivedUsers' => $topReceivedUsers,
|
'topReceivedUsers' => $topReceivedUsers,
|
||||||
'topSentUsers' => $topSentUsers
|
'topSentUsers' => $topSentUsers
|
||||||
] = $this->adminService->getUserListWithStats($selectedMonth, $search, $status, $role, $department, $flagSend);
|
] = $this->adminService->getUserListWithStats($filters);
|
||||||
|
|
||||||
if ($request->ajax() || $request->wantsJson()) {
|
if ($request->ajax() || $request->wantsJson()) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'title' => view('admin.users.partials.title', compact('selectedMonth'))->render(),
|
'title' => view('admin.users.partials.title', ['selectedMonth' => $filters->selectedMonth])->render(),
|
||||||
'stats' => view('admin.users.partials.stats', compact('topReceivedUser', 'topSentUser', 'topReceivedUsers', 'topSentUsers'))->render(),
|
'stats' => view('admin.users.partials.stats', compact('topReceivedUser', 'topSentUser', 'topReceivedUsers', 'topSentUsers'))->render(),
|
||||||
'table' => view('admin.users.partials.table', compact('users'))->render(),
|
'table' => view('admin.users.partials.table', compact('users'))->render(),
|
||||||
]);
|
]);
|
||||||
@@ -41,117 +45,91 @@ class AdminController extends Controller
|
|||||||
|
|
||||||
return view('admin.users.index', compact(
|
return view('admin.users.index', compact(
|
||||||
'users',
|
'users',
|
||||||
'selectedMonth',
|
|
||||||
'topReceivedUser',
|
'topReceivedUser',
|
||||||
'topSentUser',
|
'topSentUser',
|
||||||
'topReceivedUsers',
|
'topReceivedUsers',
|
||||||
'topSentUsers',
|
'topSentUsers'
|
||||||
'search',
|
))->with([
|
||||||
'status',
|
'selectedMonth' => $filters->selectedMonth,
|
||||||
'role',
|
'search' => $filters->search,
|
||||||
'department',
|
'status' => $filters->status,
|
||||||
'flagSend'
|
'role' => $filters->role,
|
||||||
));
|
'department' => $filters->department,
|
||||||
|
'flagSend' => $filters->flagSend,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
/**
|
||||||
|
* Show the form for creating a new user.
|
||||||
|
*/
|
||||||
|
public function create(): View
|
||||||
{
|
{
|
||||||
return view('admin.users.create');
|
return view('admin.users.create');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
/**
|
||||||
|
* Store a newly created user in storage.
|
||||||
|
*/
|
||||||
|
public function store(StoreUserRequest $request): RedirectResponse
|
||||||
{
|
{
|
||||||
$request->validate([
|
$this->adminService->createUser($request->validated());
|
||||||
'msnv' => 'required|integer|unique:user,msnv',
|
|
||||||
'name' => 'required|string|max:255',
|
|
||||||
'mail' => 'required|email|unique:user,mail',
|
|
||||||
'departments' => 'required|string|in:' . implode(',', config('constants.DEPARTMENTS')),
|
|
||||||
'password' => 'required|string',
|
|
||||||
'role' => 'required|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->adminService->createUser($request->only('msnv', 'name', 'mail', 'departments', 'password', 'role'));
|
|
||||||
|
|
||||||
return redirect()->route('admin.users.index')->with('success', __('messages.user_create_success'));
|
return redirect()->route('admin.users.index')->with('success', __('messages.user_create_success'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit(Request $request, $msnv)
|
/**
|
||||||
|
* Show the form for editing the specified user.
|
||||||
|
*/
|
||||||
|
public function edit(Request $request, string $msnv): View
|
||||||
{
|
{
|
||||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
$user = $this->adminService->getUserByMsnv($msnv);
|
||||||
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
|
$selectedMonth = (string) $request->input('month', Carbon::now()->format('Y-m'));
|
||||||
|
|
||||||
$administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth);
|
$administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth);
|
||||||
|
$addCards = $this->adminService->getAddCardsHistory($user->msnv);
|
||||||
$addCards = \App\Models\AddCard::with('sellerUser')
|
$admins = $this->adminService->getActiveAdmins();
|
||||||
->where('buyer', $user->msnv)
|
|
||||||
->orderBy('date', 'desc')
|
|
||||||
->orderBy('id', 'desc')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$admins = User::where('role', config('constants.ROLE_ADMIN'))
|
|
||||||
->where('status', config('constants.STATUS_ACTIVE'))
|
|
||||||
->get();
|
|
||||||
|
|
||||||
return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'));
|
return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Request $request, $msnv)
|
/**
|
||||||
|
* Update the specified user in storage.
|
||||||
|
*/
|
||||||
|
public function update(UpdateUserRequest $request, string $msnv): RedirectResponse
|
||||||
{
|
{
|
||||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
$user = $this->adminService->getUserByMsnv($msnv);
|
||||||
|
|
||||||
$request->validate([
|
$this->adminService->updateUser($user, $request->validated());
|
||||||
'card' => 'nullable|integer|min:0',
|
|
||||||
'num_card' => 'nullable|integer|min:1',
|
|
||||||
'role' => 'nullable|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN'),
|
|
||||||
'status' => 'nullable|in:' . config('constants.STATUS_INACTIVE') . ',' . config('constants.STATUS_ACTIVE'),
|
|
||||||
'flag_send' => 'nullable|in:0,1',
|
|
||||||
'first_login' => 'nullable|in:' . config('constants.FIRST_LOGIN_FALSE') . ',' . config('constants.FIRST_LOGIN_TRUE'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$cardValue = $user->card;
|
|
||||||
if ($request->has('num_card') && !is_null($request->num_card)) {
|
|
||||||
$cardValue = $user->card + intval($request->num_card);
|
|
||||||
} elseif ($request->has('card')) {
|
|
||||||
$cardValue = intval($request->card);
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = [
|
|
||||||
'card' => $cardValue,
|
|
||||||
'role' => $request->input('role', $user->role),
|
|
||||||
'status' => $request->input('status', $user->status),
|
|
||||||
'flag_send' => $request->has('flag_send') ? config('constants.FLAG_SEND_ENABLED') : config('constants.FLAG_SEND_DISABLED'),
|
|
||||||
'first_login' => $request->input('first_login', $user->first_login),
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->adminService->updateUser($msnv, $data);
|
|
||||||
|
|
||||||
return redirect()->back()->with('success', __('messages.user_update_success'));
|
return redirect()->back()->with('success', __('messages.user_update_success'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function destroy($msnv)
|
/**
|
||||||
|
* Remove (deactivate) the specified user from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(string $msnv): RedirectResponse
|
||||||
{
|
{
|
||||||
$this->adminService->deactivateUser($msnv);
|
$this->adminService->deactivateUser($msnv);
|
||||||
|
|
||||||
return redirect()->route('admin.users.index')->with('success', __('messages.user_deactivate_success'));
|
return redirect()->route('admin.users.index')->with('success', __('messages.user_deactivate_success'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateAddCard(Request $request, $id)
|
/**
|
||||||
|
* Update card allocation history record.
|
||||||
|
*/
|
||||||
|
public function updateAddCard(UpdateAddCardRequest $request, int $id): RedirectResponse
|
||||||
{
|
{
|
||||||
$request->validate([
|
|
||||||
'num_card' => 'required|integer|min:1',
|
|
||||||
'seller' => 'required|exists:user,msnv',
|
|
||||||
'date' => 'required|date',
|
|
||||||
]);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->adminService->updateAddCard(intval($id), $request->only('num_card', 'seller', 'date'));
|
$this->adminService->updateAddCard($id, $request->validated());
|
||||||
return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.');
|
return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return redirect()->back()->withErrors(['error' => $e->getMessage()]);
|
return redirect()->back()->withErrors(['error' => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resetCards()
|
/**
|
||||||
|
* Reset all cards for active users.
|
||||||
|
*/
|
||||||
|
public function resetCards(): RedirectResponse
|
||||||
{
|
{
|
||||||
$this->adminService->resetAllCards();
|
$this->adminService->resetAllCards();
|
||||||
|
|
||||||
|
|||||||
@@ -3,21 +3,31 @@
|
|||||||
namespace App\Http\Controllers\User;
|
namespace App\Http\Controllers\User;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Administration;
|
use App\Http\Requests\User\SendThankCardRequest;
|
||||||
|
use App\Http\Requests\User\UpdateProfileRequest;
|
||||||
|
use App\Http\Requests\User\UpdatePasswordRequest;
|
||||||
|
use App\DTOs\DashboardDataDto;
|
||||||
|
use App\Exceptions\ThankCardException;
|
||||||
use App\Services\User\Contracts\UserServiceInterface;
|
use App\Services\User\Contracts\UserServiceInterface;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private UserServiceInterface $userService
|
private readonly UserServiceInterface $userService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function index(Request $request)
|
/**
|
||||||
|
* Display the user dashboard with transaction history, stats, and rankings.
|
||||||
|
*/
|
||||||
|
public function index(Request $request): View
|
||||||
{
|
{
|
||||||
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
|
$selectedMonth = (string) $request->input('month', Carbon::now()->format('Y-m'));
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
|
||||||
$administrations = $this->userService->getDashboard($user, $selectedMonth);
|
$administrations = $this->userService->getDashboard($user, $selectedMonth);
|
||||||
@@ -26,79 +36,89 @@ class UserController extends Controller
|
|||||||
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
||||||
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
|
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
|
||||||
|
|
||||||
return view('user.dashboard', array_merge([
|
$dashboardData = new DashboardDataDto(
|
||||||
'administrations' => $administrations,
|
administrations: $administrations,
|
||||||
'selectedMonth' => $selectedMonth,
|
selectedMonth: $selectedMonth,
|
||||||
'user' => $user,
|
user: $user,
|
||||||
'receivedRankers' => $receivedRanking['rankers'],
|
receivedRankers: $receivedRanking['rankers'],
|
||||||
'receivedCurrentUserRankItem' => $receivedRanking['currentUserRankItem'],
|
receivedCurrentUserRankItem: $receivedRanking['currentUserRankItem'],
|
||||||
'sentRankers' => $sentRanking['rankers'],
|
sentRankers: $sentRanking['rankers'],
|
||||||
'sentCurrentUserRankItem' => $sentRanking['currentUserRankItem']
|
sentCurrentUserRankItem: $sentRanking['currentUserRankItem'],
|
||||||
], $stats));
|
receivedCount: (int) $stats['receivedCount'],
|
||||||
|
sentCount: (int) $stats['sentCount'],
|
||||||
|
currentRank: (int) $stats['currentRank']
|
||||||
|
);
|
||||||
|
|
||||||
|
return view('user.dashboard', $dashboardData->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sendThankcards()
|
/**
|
||||||
|
* Show the form to send thank cards.
|
||||||
|
*/
|
||||||
|
public function sendThankcards(): View
|
||||||
{
|
{
|
||||||
$users = $this->userService->getOtherActiveUsers(Auth::user());
|
$users = $this->userService->getOtherActiveUsers(Auth::user());
|
||||||
return view('user.send', compact('users'));
|
return view('user.send', compact('users'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function storeThankcards(Request $request)
|
/**
|
||||||
|
* Store and execute sending thank cards to another user.
|
||||||
|
*/
|
||||||
|
public function storeThankcards(SendThankCardRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$request->validate([
|
|
||||||
'receiver' => [
|
|
||||||
'required',
|
|
||||||
\Illuminate\Validation\Rule::exists('user', 'msnv')->where('status', config('constants.STATUS_ACTIVE'))
|
|
||||||
],
|
|
||||||
'amount' => 'required|integer|min:1|max:' . Administration::MAX_SEND_CARD_PER_MONTH,
|
|
||||||
]);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->userService->sendThankcards(
|
$this->userService->sendThankcards(
|
||||||
Auth::user(),
|
Auth::user(),
|
||||||
$request->receiver,
|
(string) $request->input('receiver'),
|
||||||
(int) $request->amount
|
(int) $request->input('amount')
|
||||||
);
|
);
|
||||||
} catch (\RuntimeException $e) {
|
} catch (ThankCardException $e) {
|
||||||
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
|
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(['success' => true, 'message' => __('messages.thank_card_send_success')]);
|
return response()->json(['success' => true, 'message' => __('messages.thank_card_send_success')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function changePasswordForm()
|
/**
|
||||||
|
* Show the change password form.
|
||||||
|
*/
|
||||||
|
public function changePasswordForm(): View
|
||||||
{
|
{
|
||||||
return view('user.change_password');
|
return view('user.change_password');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show edit profile page (avatar & password)
|
/**
|
||||||
public function editProfile()
|
* Handle user password change.
|
||||||
|
*/
|
||||||
|
public function updatePassword(UpdatePasswordRequest $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$user = Auth::user();
|
||||||
|
$this->userService->updatePassword($user, $request->input('password'));
|
||||||
|
|
||||||
|
$route = $user->role == config('constants.ROLE_ADMIN') ? 'admin.dashboard' : 'user.dashboard';
|
||||||
|
return redirect()->route($route)->with('success', __('messages.password_change_success'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the edit profile page (avatar & password).
|
||||||
|
*/
|
||||||
|
public function editProfile(): View
|
||||||
{
|
{
|
||||||
return view('user.edit');
|
return view('user.edit');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle avatar upload and optional password change
|
/**
|
||||||
public function updateProfile(Request $request)
|
* Handle avatar upload and optional password change.
|
||||||
|
*/
|
||||||
|
public function updateProfile(UpdateProfileRequest $request): RedirectResponse
|
||||||
{
|
{
|
||||||
$request->validate([
|
|
||||||
'avatar' => 'nullable|image|max:2048', // 2MB max
|
|
||||||
'new_password' => 'nullable|min:6|confirmed',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
|
||||||
// Avatar handling
|
$this->userService->updateProfile(
|
||||||
if ($request->hasFile('avatar')) {
|
$user,
|
||||||
$path = $request->file('avatar')->store('avatars', 'public');
|
$request->file('avatar'),
|
||||||
$user->avatar = 'storage/' . $path;
|
$request->filled('new_password') ? $request->input('new_password') : null
|
||||||
}
|
);
|
||||||
|
|
||||||
// Password handling (optional)
|
|
||||||
if ($request->filled('new_password')) {
|
|
||||||
$this->userService->updatePassword($user, $request->new_password);
|
|
||||||
}
|
|
||||||
|
|
||||||
$user->save();
|
|
||||||
|
|
||||||
return redirect()->route('user.edit')->with('success', 'Cập nhật hồ sơ thành công');
|
return redirect()->route('user.edit')->with('success', 'Cập nhật hồ sơ thành công');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class StoreUserRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true; // Middleware handles authorization
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'msnv' => 'required|integer|unique:user,msnv',
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'mail' => 'required|email|unique:user,mail',
|
||||||
|
'departments' => 'required|string|in:' . implode(',', config('constants.DEPARTMENTS')),
|
||||||
|
'password' => 'required|string',
|
||||||
|
'role' => 'required|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateAddCardRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'num_card' => 'required|integer|min:1',
|
||||||
|
'seller' => 'required|exists:user,msnv',
|
||||||
|
'date' => 'required|date',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Admin;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateUserRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function prepareForValidation(): void
|
||||||
|
{
|
||||||
|
// Only set default flag_send to 0 if the route is for user update and flag_send is not present
|
||||||
|
if (($this->isMethod('put') || $this->isMethod('patch')) && !$this->has('flag_send')) {
|
||||||
|
$this->merge([
|
||||||
|
'flag_send' => config('constants.FLAG_SEND_DISABLED'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'card' => 'nullable|integer|min:0',
|
||||||
|
'num_card' => 'nullable|integer|min:1',
|
||||||
|
'role' => 'nullable|in:' . config('constants.ROLE_MEMBER') . ',' . config('constants.ROLE_ADMIN'),
|
||||||
|
'status' => 'nullable|in:' . config('constants.STATUS_INACTIVE') . ',' . config('constants.STATUS_ACTIVE'),
|
||||||
|
'flag_send' => 'nullable|in:0,1',
|
||||||
|
'first_login' => 'nullable|in:' . config('constants.FIRST_LOGIN_FALSE') . ',' . config('constants.FIRST_LOGIN_TRUE'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\User;
|
||||||
|
|
||||||
|
use App\Models\Administration;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class SendThankCardRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'receiver' => [
|
||||||
|
'required',
|
||||||
|
Rule::exists('user', 'msnv')->where('status', config('constants.STATUS_ACTIVE'))
|
||||||
|
],
|
||||||
|
'amount' => 'required|integer|min:1|max:' . Administration::MAX_SEND_CARD_PER_MONTH,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\User;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdatePasswordRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'password' => 'required|min:6|confirmed',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the custom validation error messages.
|
||||||
|
*/
|
||||||
|
public function messages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'password.required' => 'Mật khẩu mới không được để trống.',
|
||||||
|
'password.min' => 'Mật khẩu mới phải có ít nhất 6 ký tự.',
|
||||||
|
'password.confirmed' => 'Mật khẩu xác nhận không trùng khớp.',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\User;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateProfileRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'avatar' => 'nullable|image|max:2048', // 2MB max
|
||||||
|
'new_password' => 'nullable|min:6|confirmed',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,16 +12,31 @@ use Illuminate\Support\Facades\Auth;
|
|||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
use App\DTOs\UserFilterDto;
|
||||||
|
|
||||||
class AdminService implements AdminServiceInterface
|
class AdminService implements AdminServiceInterface
|
||||||
{
|
{
|
||||||
public function getUserListWithStats(
|
public function getUserListWithStats(
|
||||||
string $selectedMonth,
|
string|UserFilterDto $monthOrFilters,
|
||||||
?string $search = null,
|
?string $search = null,
|
||||||
?string $status = '1',
|
?string $status = '1',
|
||||||
?string $role = null,
|
?string $role = null,
|
||||||
?string $department = null,
|
?string $department = null,
|
||||||
?string $flagSend = null
|
?string $flagSend = null
|
||||||
): array {
|
): array {
|
||||||
|
if ($monthOrFilters instanceof UserFilterDto) {
|
||||||
|
$filters = $monthOrFilters;
|
||||||
|
} else {
|
||||||
|
$filters = new UserFilterDto(
|
||||||
|
selectedMonth: $monthOrFilters,
|
||||||
|
search: $search,
|
||||||
|
status: $status,
|
||||||
|
role: $role,
|
||||||
|
department: $department,
|
||||||
|
flagSend: $flagSend
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$selectedMonth = $filters->selectedMonth;
|
||||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||||
|
|
||||||
@@ -51,22 +66,27 @@ class AdminService implements AdminServiceInterface
|
|||||||
$topReceivedUser = $topReceivedUsers->first();
|
$topReceivedUser = $topReceivedUsers->first();
|
||||||
$topSentUser = $topSentUsers->first();
|
$topSentUser = $topSentUsers->first();
|
||||||
|
|
||||||
|
$status = $filters->status;
|
||||||
if (!is_null($status) && $status !== '') {
|
if (!is_null($status) && $status !== '') {
|
||||||
$usersQuery->where('status', intval($status));
|
$usersQuery->where('status', intval($status));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$role = $filters->role;
|
||||||
if (!is_null($role) && $role !== '') {
|
if (!is_null($role) && $role !== '') {
|
||||||
$usersQuery->where('role', intval($role));
|
$usersQuery->where('role', intval($role));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$department = $filters->department;
|
||||||
if (!is_null($department) && $department !== '') {
|
if (!is_null($department) && $department !== '') {
|
||||||
$usersQuery->where('departments', $department);
|
$usersQuery->where('departments', $department);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$flagSend = $filters->flagSend;
|
||||||
if (!is_null($flagSend) && $flagSend !== '') {
|
if (!is_null($flagSend) && $flagSend !== '') {
|
||||||
$usersQuery->where('flag_send', intval($flagSend));
|
$usersQuery->where('flag_send', intval($flagSend));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$search = $filters->search;
|
||||||
if (!is_null($search) && trim($search) !== '') {
|
if (!is_null($search) && trim($search) !== '') {
|
||||||
$search = trim($search);
|
$search = trim($search);
|
||||||
$usersQuery->where(function ($q) use ($search) {
|
$usersQuery->where(function ($q) use ($search) {
|
||||||
@@ -109,12 +129,20 @@ class AdminService implements AdminServiceInterface
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateUser(string $msnv, array $data): void
|
public function updateUser(string|User $userOrMsnv, array $data): void
|
||||||
{
|
{
|
||||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
$user = $userOrMsnv instanceof User
|
||||||
|
? $userOrMsnv
|
||||||
|
: User::where('msnv', $userOrMsnv)->firstOrFail();
|
||||||
|
|
||||||
DB::transaction(function () use ($data, $user) {
|
DB::transaction(function () use ($data, $user) {
|
||||||
|
$newCard = $user->card;
|
||||||
|
if (isset($data['num_card'])) {
|
||||||
|
$newCard = $user->card + intval($data['num_card']);
|
||||||
|
} elseif (isset($data['card'])) {
|
||||||
$newCard = intval($data['card']);
|
$newCard = intval($data['card']);
|
||||||
|
}
|
||||||
|
|
||||||
$oldCard = intval($user->card);
|
$oldCard = intval($user->card);
|
||||||
|
|
||||||
if ($newCard > $oldCard) {
|
if ($newCard > $oldCard) {
|
||||||
@@ -128,10 +156,10 @@ class AdminService implements AdminServiceInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
$user->card = $newCard;
|
$user->card = $newCard;
|
||||||
$user->role = intval($data['role']);
|
$user->role = isset($data['role']) ? intval($data['role']) : $user->role;
|
||||||
$user->status = intval($data['status']);
|
$user->status = isset($data['status']) ? intval($data['status']) : $user->status;
|
||||||
$user->flag_send = intval($data['flag_send']);
|
$user->flag_send = isset($data['flag_send']) ? intval($data['flag_send']) : $user->flag_send;
|
||||||
$user->first_login = intval($data['first_login']);
|
$user->first_login = isset($data['first_login']) ? intval($data['first_login']) : $user->first_login;
|
||||||
$user->save();
|
$user->save();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -185,4 +213,25 @@ class AdminService implements AdminServiceInterface
|
|||||||
$addCard->save();
|
$addCard->save();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getUserByMsnv(string $msnv): User
|
||||||
|
{
|
||||||
|
return User::where('msnv', $msnv)->firstOrFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAddCardsHistory(string $buyerMsnv): \Illuminate\Database\Eloquent\Collection
|
||||||
|
{
|
||||||
|
return AddCard::with('sellerUser')
|
||||||
|
->where('buyer', $buyerMsnv)
|
||||||
|
->orderBy('date', 'desc')
|
||||||
|
->orderBy('id', 'desc')
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getActiveAdmins(): \Illuminate\Database\Eloquent\Collection
|
||||||
|
{
|
||||||
|
return User::where('role', config('constants.ROLE_ADMIN'))
|
||||||
|
->where('status', config('constants.STATUS_ACTIVE'))
|
||||||
|
->get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ use Illuminate\Pagination\LengthAwarePaginator;
|
|||||||
interface AdminServiceInterface
|
interface AdminServiceInterface
|
||||||
{
|
{
|
||||||
public function getUserListWithStats(
|
public function getUserListWithStats(
|
||||||
string $selectedMonth,
|
string|\App\DTOs\UserFilterDto $monthOrFilters,
|
||||||
?string $search = null,
|
?string $search = null,
|
||||||
?string $status = '1',
|
?string $status = '1',
|
||||||
?string $role = null,
|
?string $role = null,
|
||||||
@@ -19,11 +19,17 @@ interface AdminServiceInterface
|
|||||||
|
|
||||||
public function createUser(array $data): void;
|
public function createUser(array $data): void;
|
||||||
|
|
||||||
public function updateUser(string $msnv, array $data): void;
|
public function updateUser(string|\App\Models\User $userOrMsnv, array $data): void;
|
||||||
|
|
||||||
public function deactivateUser(string $msnv): void;
|
public function deactivateUser(string $msnv): void;
|
||||||
|
|
||||||
public function resetAllCards(): void;
|
public function resetAllCards(): void;
|
||||||
|
|
||||||
public function updateAddCard(int $id, array $data): void;
|
public function updateAddCard(int $id, array $data): void;
|
||||||
|
|
||||||
|
public function getUserByMsnv(string $msnv): \App\Models\User;
|
||||||
|
|
||||||
|
public function getAddCardsHistory(string $buyerMsnv): \Illuminate\Database\Eloquent\Collection;
|
||||||
|
|
||||||
|
public function getActiveAdmins(): \Illuminate\Database\Eloquent\Collection;
|
||||||
}
|
}
|
||||||
@@ -18,4 +18,6 @@ interface UserServiceInterface
|
|||||||
public function getPersonalStats(User $user, string $selectedMonth): array;
|
public function getPersonalStats(User $user, string $selectedMonth): array;
|
||||||
|
|
||||||
public function getRankingList(string $type, string $selectedMonth, User $currentUser): array;
|
public function getRankingList(string $type, string $selectedMonth, User $currentUser): array;
|
||||||
|
|
||||||
|
public function updateProfile(User $user, ?\Illuminate\Http\UploadedFile $avatarFile, ?string $newPassword): void;
|
||||||
}
|
}
|
||||||
@@ -36,12 +36,12 @@ class UserService implements UserServiceInterface
|
|||||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void
|
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void
|
||||||
{
|
{
|
||||||
if ($sender->flag_send == config('constants.FLAG_SEND_DISABLED')) {
|
if ($sender->flag_send == config('constants.FLAG_SEND_DISABLED')) {
|
||||||
throw new \RuntimeException(__('messages.error.no_send_permission'));
|
throw new \App\Exceptions\ThankCardException(__('messages.error.no_send_permission'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$receiver = User::where('msnv', $receiverMsnv)->first();
|
$receiver = User::where('msnv', $receiverMsnv)->first();
|
||||||
if (!$receiver || $receiver->status != config('constants.STATUS_ACTIVE')) {
|
if (!$receiver || $receiver->status != config('constants.STATUS_ACTIVE')) {
|
||||||
throw new \RuntimeException(__('messages.error.receiver_invalid'));
|
throw new \App\Exceptions\ThankCardException(__('messages.error.receiver_invalid'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$startOfMonth = Carbon::now()->startOfMonth();
|
$startOfMonth = Carbon::now()->startOfMonth();
|
||||||
@@ -53,7 +53,7 @@ class UserService implements UserServiceInterface
|
|||||||
->sum('sent');
|
->sum('sent');
|
||||||
|
|
||||||
if ($cardsSentToThisUserThisMonth + $amount > Administration::MAX_SEND_CARD_PER_MONTH) {
|
if ($cardsSentToThisUserThisMonth + $amount > Administration::MAX_SEND_CARD_PER_MONTH) {
|
||||||
throw new \RuntimeException(
|
throw new \App\Exceptions\ThankCardException(
|
||||||
__('messages.error.max_send_limit_template', [
|
__('messages.error.max_send_limit_template', [
|
||||||
'max' => Administration::MAX_SEND_CARD_PER_MONTH,
|
'max' => Administration::MAX_SEND_CARD_PER_MONTH,
|
||||||
'sent' => $cardsSentToThisUserThisMonth
|
'sent' => $cardsSentToThisUserThisMonth
|
||||||
@@ -62,7 +62,7 @@ class UserService implements UserServiceInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($sender->card < $amount) {
|
if ($sender->card < $amount) {
|
||||||
throw new \RuntimeException(__('messages.error.not_enough_cards'));
|
throw new \App\Exceptions\ThankCardException(__('messages.error.not_enough_cards'));
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::transaction(function () use ($sender, $receiverMsnv, $amount) {
|
DB::transaction(function () use ($sender, $receiverMsnv, $amount) {
|
||||||
@@ -213,4 +213,19 @@ class UserService implements UserServiceInterface
|
|||||||
'currentUserRankItem' => $currentUserRankItem
|
'currentUserRankItem' => $currentUserRankItem
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateProfile(User $user, ?\Illuminate\Http\UploadedFile $avatarFile, ?string $newPassword): void
|
||||||
|
{
|
||||||
|
if ($avatarFile) {
|
||||||
|
$path = $avatarFile->store('avatars', 'public');
|
||||||
|
$user->avatar = 'storage/' . $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($newPassword) {
|
||||||
|
$user->pass = md5($newPassword);
|
||||||
|
$user->first_login = config('constants.FIRST_LOGIN_FALSE');
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user