feature: keep sidebar and add service layer
This commit is contained in:
@@ -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