feature: keep sidebar and add service layer
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Admin;
|
||||
|
||||
use App\Models\AddCard;
|
||||
use App\Models\Administration;
|
||||
use App\Models\User;
|
||||
use App\Services\Admin\Contracts\AdminServiceInterface;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class AdminService implements AdminServiceInterface
|
||||
{
|
||||
public function getUserListWithStats(string $selectedMonth): array
|
||||
{
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
$allActiveUsers = User::where('status', User::STATUS_ACTIVE)->get()->map(function ($user) use ($startOfMonth, $endOfMonth) {
|
||||
$user->total_received = Administration::where('receiver', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('received');
|
||||
|
||||
$user->total_sent = Administration::where('sender', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('sent');
|
||||
|
||||
return $user;
|
||||
});
|
||||
|
||||
$topReceivedUser = $allActiveUsers->sortByDesc('total_received')->first();
|
||||
$topSentUser = $allActiveUsers->sortByDesc('total_sent')->first();
|
||||
|
||||
$users = User::where('status', User::STATUS_ACTIVE)->paginate(10)->withQueryString();
|
||||
|
||||
$users->getCollection()->transform(function ($user) use ($startOfMonth, $endOfMonth) {
|
||||
$user->total_received = Administration::where('receiver', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('received');
|
||||
|
||||
$user->total_sent = Administration::where('sender', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('sent');
|
||||
|
||||
return $user;
|
||||
});
|
||||
|
||||
return compact('users', 'topReceivedUser', 'topSentUser');
|
||||
}
|
||||
|
||||
public function getUserTransactions(string $msnv, string $selectedMonth): LengthAwarePaginator
|
||||
{
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
return Administration::where(function ($q) use ($msnv) {
|
||||
$q->where('sender', $msnv)->orWhere('receiver', $msnv);
|
||||
})
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->orderBy('date', 'desc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
}
|
||||
|
||||
public function createUser(array $data): void
|
||||
{
|
||||
User::create([
|
||||
'msnv' => $data['msnv'],
|
||||
'mail' => $data['mail'],
|
||||
'pass' => Hash::make($data['password']),
|
||||
'role' => $data['role'],
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'card' => 0,
|
||||
'flag_send' => User::FLAG_SEND_DISABLED,
|
||||
'first_login' => User::FIRST_LOGIN_TRUE,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateUser(string $msnv, array $data): void
|
||||
{
|
||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
||||
|
||||
DB::transaction(function () use ($data, $user) {
|
||||
if (!empty($data['num_card'])) {
|
||||
AddCard::create([
|
||||
'buyer' => $user->msnv,
|
||||
'num_card' => $data['num_card'],
|
||||
'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->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function deactivateUser(string $msnv): void
|
||||
{
|
||||
$user = User::where('msnv', $msnv)->firstOrFail();
|
||||
$user->status = User::STATUS_INACTIVE;
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function resetAllCards(): void
|
||||
{
|
||||
User::where('status', User::STATUS_ACTIVE)->update(['card' => 0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Admin\Contracts;
|
||||
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface AdminServiceInterface
|
||||
{
|
||||
public function getUserListWithStats(string $selectedMonth): array;
|
||||
|
||||
public function getUserTransactions(string $msnv, string $selectedMonth): LengthAwarePaginator;
|
||||
|
||||
public function createUser(array $data): void;
|
||||
|
||||
public function updateUser(string $msnv, array $data): void;
|
||||
|
||||
public function deactivateUser(string $msnv): void;
|
||||
|
||||
public function resetAllCards(): void;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Auth\Contracts\AuthServiceInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class AuthService implements AuthServiceInterface
|
||||
{
|
||||
public function attemptLogin(array $credentials, Request $request): array
|
||||
{
|
||||
if (!Auth::attempt(['mail' => $credentials['mail'], 'password' => $credentials['password']])) {
|
||||
return ['success' => false, 'error_key' => 'mail', 'error_msg' => __('auth.failed')];
|
||||
}
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->status != User::STATUS_ACTIVE) {
|
||||
Auth::logout();
|
||||
return ['success' => false, 'error_key' => 'mail', 'error_msg' => __('auth.inactive')];
|
||||
}
|
||||
|
||||
return ['success' => true, 'user' => $user];
|
||||
}
|
||||
|
||||
public function logout(Request $request): void
|
||||
{
|
||||
Auth::logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
}
|
||||
|
||||
public function getRedirectRouteForUser(User $user): string
|
||||
{
|
||||
if ($user->first_login == User::FIRST_LOGIN_TRUE) {
|
||||
return route('user.change_password');
|
||||
}
|
||||
|
||||
if ($user->role == User::ROLE_ADMIN) {
|
||||
return route('admin.dashboard');
|
||||
}
|
||||
|
||||
return route('user.dashboard');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth\Contracts;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
interface AuthServiceInterface
|
||||
{
|
||||
public function attemptLogin(array $credentials, Request $request): array;
|
||||
|
||||
public function logout(Request $request): void;
|
||||
|
||||
public function getRedirectRouteForUser(User $user): string;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\User\Contracts;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface UserServiceInterface
|
||||
{
|
||||
public function getDashboard(User $user, string $selectedMonth): LengthAwarePaginator;
|
||||
|
||||
public function getOtherActiveUsers(User $currentUser): \Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void;
|
||||
|
||||
public function updatePassword(User $user, string $newPassword): void;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\User;
|
||||
|
||||
use App\Models\Administration;
|
||||
use App\Models\User;
|
||||
use App\Services\User\Contracts\UserServiceInterface;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserService implements UserServiceInterface
|
||||
{
|
||||
public function getDashboard(User $user, string $selectedMonth): LengthAwarePaginator
|
||||
{
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
return Administration::where('msnv', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->orderBy('date', 'desc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
}
|
||||
|
||||
public function getOtherActiveUsers(User $currentUser): Collection
|
||||
{
|
||||
return User::where('status', User::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) {
|
||||
throw new \RuntimeException(__('messages.error.no_send_permission'));
|
||||
}
|
||||
|
||||
$startOfMonth = Carbon::now()->startOfMonth();
|
||||
$endOfMonth = Carbon::now()->endOfMonth();
|
||||
|
||||
$cardsSentToThisUserThisMonth = Administration::where('msnv', $sender->msnv)
|
||||
->where('receiver', $receiverMsnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('sent');
|
||||
|
||||
if ($cardsSentToThisUserThisMonth + $amount > Administration::MAX_SEND_CARD_PER_MONTH) {
|
||||
throw new \RuntimeException(
|
||||
__('messages.error.max_send_limit_template', [
|
||||
'max' => Administration::MAX_SEND_CARD_PER_MONTH,
|
||||
'sent' => $cardsSentToThisUserThisMonth
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
if ($sender->card < $amount) {
|
||||
throw new \RuntimeException(__('messages.error.not_enough_cards'));
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($sender, $receiverMsnv, $amount) {
|
||||
Administration::create([
|
||||
'msnv' => $receiverMsnv,
|
||||
'received' => $amount,
|
||||
'sender' => $sender->msnv,
|
||||
'sent' => 0,
|
||||
'receiver' => null,
|
||||
'date' => Carbon::today(),
|
||||
]);
|
||||
|
||||
Administration::create([
|
||||
'msnv' => $sender->msnv,
|
||||
'received' => 0,
|
||||
'sender' => null,
|
||||
'sent' => $amount,
|
||||
'receiver' => $receiverMsnv,
|
||||
'date' => Carbon::today(),
|
||||
]);
|
||||
|
||||
$sender->card -= $amount;
|
||||
$sender->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function updatePassword(User $user, string $newPassword): void
|
||||
{
|
||||
$user->pass = Hash::make($newPassword);
|
||||
$user->first_login = User::FIRST_LOGIN_FALSE;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user