refactor: refactor controllers, services, validation and introduce DTOs, FormRequests and domain exceptions
This commit is contained in:
@@ -12,18 +12,33 @@ use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
use App\DTOs\UserFilterDto;
|
||||
|
||||
class AdminService implements AdminServiceInterface
|
||||
{
|
||||
public function getUserListWithStats(
|
||||
string $selectedMonth,
|
||||
?string $search = null,
|
||||
string|UserFilterDto $monthOrFilters,
|
||||
?string $search = null,
|
||||
?string $status = '1',
|
||||
?string $role = null,
|
||||
?string $department = null,
|
||||
?string $flagSend = null
|
||||
): array {
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
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();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
$usersQuery = User::select('user.*')
|
||||
->addSelect([
|
||||
@@ -51,22 +66,27 @@ class AdminService implements AdminServiceInterface
|
||||
$topReceivedUser = $topReceivedUsers->first();
|
||||
$topSentUser = $topSentUsers->first();
|
||||
|
||||
$status = $filters->status;
|
||||
if (!is_null($status) && $status !== '') {
|
||||
$usersQuery->where('status', intval($status));
|
||||
}
|
||||
|
||||
$role = $filters->role;
|
||||
if (!is_null($role) && $role !== '') {
|
||||
$usersQuery->where('role', intval($role));
|
||||
}
|
||||
|
||||
$department = $filters->department;
|
||||
if (!is_null($department) && $department !== '') {
|
||||
$usersQuery->where('departments', $department);
|
||||
}
|
||||
|
||||
$flagSend = $filters->flagSend;
|
||||
if (!is_null($flagSend) && $flagSend !== '') {
|
||||
$usersQuery->where('flag_send', intval($flagSend));
|
||||
}
|
||||
|
||||
$search = $filters->search;
|
||||
if (!is_null($search) && trim($search) !== '') {
|
||||
$search = trim($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) {
|
||||
$newCard = intval($data['card']);
|
||||
$newCard = $user->card;
|
||||
if (isset($data['num_card'])) {
|
||||
$newCard = $user->card + intval($data['num_card']);
|
||||
} elseif (isset($data['card'])) {
|
||||
$newCard = intval($data['card']);
|
||||
}
|
||||
|
||||
$oldCard = intval($user->card);
|
||||
|
||||
if ($newCard > $oldCard) {
|
||||
@@ -128,10 +156,10 @@ class AdminService implements AdminServiceInterface
|
||||
}
|
||||
|
||||
$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->role = isset($data['role']) ? intval($data['role']) : $user->role;
|
||||
$user->status = isset($data['status']) ? intval($data['status']) : $user->status;
|
||||
$user->flag_send = isset($data['flag_send']) ? intval($data['flag_send']) : $user->flag_send;
|
||||
$user->first_login = isset($data['first_login']) ? intval($data['first_login']) : $user->first_login;
|
||||
$user->save();
|
||||
});
|
||||
}
|
||||
@@ -185,4 +213,25 @@ class AdminService implements AdminServiceInterface
|
||||
$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,8 +7,8 @@ use Illuminate\Pagination\LengthAwarePaginator;
|
||||
interface AdminServiceInterface
|
||||
{
|
||||
public function getUserListWithStats(
|
||||
string $selectedMonth,
|
||||
?string $search = null,
|
||||
string|\App\DTOs\UserFilterDto $monthOrFilters,
|
||||
?string $search = null,
|
||||
?string $status = '1',
|
||||
?string $role = null,
|
||||
?string $department = null,
|
||||
@@ -19,11 +19,17 @@ interface AdminServiceInterface
|
||||
|
||||
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 resetAllCards(): 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 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
|
||||
{
|
||||
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();
|
||||
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();
|
||||
@@ -53,7 +53,7 @@ class UserService implements UserServiceInterface
|
||||
->sum('sent');
|
||||
|
||||
if ($cardsSentToThisUserThisMonth + $amount > Administration::MAX_SEND_CARD_PER_MONTH) {
|
||||
throw new \RuntimeException(
|
||||
throw new \App\Exceptions\ThankCardException(
|
||||
__('messages.error.max_send_limit_template', [
|
||||
'max' => Administration::MAX_SEND_CARD_PER_MONTH,
|
||||
'sent' => $cardsSentToThisUserThisMonth
|
||||
@@ -62,7 +62,7 @@ class UserService implements UserServiceInterface
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -213,4 +213,19 @@ class UserService implements UserServiceInterface
|
||||
'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