This commit is contained in:
Adrian Hopek 2022-03-01 12:25:07 +01:00
parent 92784846dc
commit a37be18da6
10 changed files with 85 additions and 62 deletions

View File

@ -6,9 +6,6 @@ namespace Toby\Architecture\Providers;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Toby\Eloquent\Models\Holiday;
use Toby\Eloquent\Models\VacationLimit;
use Toby\Eloquent\Scopes\SelectedYearPeriodScope;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -16,10 +13,5 @@ class AppServiceProvider extends ServiceProvider
{ {
Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y")); Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y"));
Carbon::macro("toDisplayDate", fn() => $this->translatedFormat("d.m.Y")); Carbon::macro("toDisplayDate", fn() => $this->translatedFormat("d.m.Y"));
$selectedYearPeriodScope = $this->app->make(SelectedYearPeriodScope::class);
VacationLimit::addGlobalScope($selectedYearPeriodScope);
Holiday::addGlobalScope($selectedYearPeriodScope);
} }
} }

View File

@ -59,6 +59,11 @@ class UserVacationStatsRetriever
->count(); ->count();
} }
public function getRemainingVacationDays(User $user, YearPeriod $yearPeriod): int
{
return $this->getVacationDaysLimit($user, $yearPeriod) - $this->getUsedVacationDays($user, $yearPeriod);
}
public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int public function getVacationDaysLimit(User $user, YearPeriod $yearPeriod): int
{ {
$limit = $user->vacationLimits() $limit = $user->vacationLimits()

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers\Api;
use Illuminate\Http\JsonResponse;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Http\Controllers\Controller;
use Toby\Infrastructure\Http\Requests\Api\CalculateVacationStatsRequest;
class CalculateUserVacationStatsController extends Controller
{
public function __invoke(
CalculateVacationStatsRequest $request,
UserVacationStatsRetriever $vacationStatsRetriever,
YearPeriodRetriever $yearPeriodRetriever,
): JsonResponse {
/** @var User $user */
$user = User::query()->find($request->get("user"));
$yearPeriod = $yearPeriodRetriever->selected();
$limit = $vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod);
$used = $vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod);
$pending = $vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod);
$other = $vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod);
return new JsonResponse([
"limit" => $limit,
"remaining" => $limit - $used - $pending,
"used" => $used,
"pending" => $pending,
"other" => $other,
]);
}
}

View File

@ -6,6 +6,7 @@ namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Inertia\Response; use Inertia\Response;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\Holiday; use Toby\Eloquent\Models\Holiday;
use Toby\Infrastructure\Http\Requests\HolidayRequest; use Toby\Infrastructure\Http\Requests\HolidayRequest;
use Toby\Infrastructure\Http\Resources\HolidayFormDataResource; use Toby\Infrastructure\Http\Resources\HolidayFormDataResource;
@ -13,9 +14,12 @@ use Toby\Infrastructure\Http\Resources\HolidayResource;
class HolidayController extends Controller class HolidayController extends Controller
{ {
public function index(): Response public function index(YearPeriodRetriever $yearPeriodRetriever): Response
{ {
$holidays = Holiday::query() $yearPeriod = $yearPeriodRetriever->selected();
$holidays = $yearPeriod
->holidays()
->orderBy("date") ->orderBy("date")
->get(); ->get();

View File

@ -6,22 +6,39 @@ namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Inertia\Response; use Inertia\Response;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\VacationLimit; use Toby\Eloquent\Models\VacationLimit;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Requests\VacationLimitRequest; use Toby\Infrastructure\Http\Requests\VacationLimitRequest;
use Toby\Infrastructure\Http\Resources\VacationLimitResource; use Toby\Infrastructure\Http\Resources\UserResource;
class VacationLimitController extends Controller class VacationLimitController extends Controller
{ {
public function edit(): Response public function edit(YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever): Response
{ {
$limits = VacationLimit::query() $yearPeriod = $yearPeriodRetriever->selected();
$previousYearPeriod = YearPeriod::findByYear($yearPeriod->year - 1);
$limits = $yearPeriod
->vacationLimits()
->with("user") ->with("user")
->orderByUserField("last_name") ->orderByUserField("last_name")
->orderByUserField("first_name") ->orderByUserField("first_name")
->get(); ->get();
$limitsResource = $limits->map(fn(VacationLimit $limit) => [
"id" => $limit->id,
"user" => new UserResource($limit->user),
"hasVacation" => $limit->hasVacation(),
"days" => $limit->days,
"remainingLastYear" => $previousYearPeriod
? $statsRetriever->getRemainingVacationDays($limit->user, $previousYearPeriod)
: 0,
]);
return inertia("VacationLimits", [ return inertia("VacationLimits", [
"limits" => VacationLimitResource::collection($limits), "limits" => $limitsResource,
]); ]);
} }

View File

@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class VacationLimitResource extends JsonResource
{
public static $wrap = null;
public function toArray($request): array
{
return [
"id" => $this->id,
"user" => new UserResource($this->user),
"hasVacation" => $this->hasVacation(),
"days" => $this->days,
];
}
}

View File

@ -34,6 +34,12 @@
> >
Posiada urlop? Posiada urlop?
</th> </th>
<th
scope="col"
class="px-6 py-3 text-left text-xs font-semibold text-gray-500 uppercase tracking-wider"
>
Pozostałe dni z poprzedniego roku
</th>
<th <th
scope="col" scope="col"
class="px-6 py-3 text-left text-xs font-semibold text-gray-500 uppercase tracking-wider" class="px-6 py-3 text-left text-xs font-semibold text-gray-500 uppercase tracking-wider"
@ -82,6 +88,11 @@
/> />
</Switch> </Switch>
</td> </td>
<td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
<div class="mt-1 sm:mt-0 sm:col-span-2 w-full max-w-lg bg-gray-50 border border-gray-300 rounded-md px-4 py-2 inline-flex items-center text-gray-500 sm:text-sm">
{{ item.remainingLastYear }}
</div>
</td>
<td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500"> <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
<div class="mt-1 sm:mt-0 sm:col-span-2"> <div class="mt-1 sm:mt-0 sm:col-span-2">
<input <input
@ -147,7 +158,7 @@ export default {
}, },
setup(props) { setup(props) {
const form = useForm({ const form = useForm({
items: props.limits.data, items: props.limits,
}) })
return { return {

View File

@ -336,8 +336,6 @@ export default {
] ]
const userNavigation = [ const userNavigation = [
{name: 'Your Profile', href: '#'},
{name: 'Settings', href: '#'},
{name: 'Wyloguj się', href: '/logout', method: 'post', as: 'button'}, {name: 'Wyloguj się', href: '/logout', method: 'post', as: 'button'},
] ]

View File

@ -3,30 +3,10 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Toby\Domain\UserVacationStatsRetriever; use Toby\Infrastructure\Http\Controllers\Api\CalculateUserVacationStatsController;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Controllers\Api\CalculateVacationDaysController; use Toby\Infrastructure\Http\Controllers\Api\CalculateVacationDaysController;
use Toby\Infrastructure\Http\Requests\Api\CalculateVacationStatsRequest;
Route::middleware("auth:sanctum")->group(function (): void { Route::middleware("auth:sanctum")->group(function (): void {
Route::post("calculate-vacation-days", CalculateVacationDaysController::class); Route::post("calculate-vacation-days", CalculateVacationDaysController::class);
Route::post("calculate-vacations-stats", function (CalculateVacationStatsRequest $request, UserVacationStatsRetriever $vacationStatsRetriever) { Route::post("calculate-vacations-stats", CalculateUserVacationStatsController::class);
/** @var User $user */
$user = User::query()->find($request->get("user"));
$yearPeriod = YearPeriod::current();
$limit = $vacationStatsRetriever->getVacationDaysLimit($user, $yearPeriod);
$used = $vacationStatsRetriever->getUsedVacationDays($user, $yearPeriod);
$pending = $vacationStatsRetriever->getPendingVacationDays($user, $yearPeriod);
$other = $vacationStatsRetriever->getOtherApprovedVacationDays($user, $yearPeriod);
return [
"limit" => $limit,
"remaining" => $limit - $used - $pending,
"used" => $used,
"pending" => $pending,
"other" => $other,
];
});
}); });

View File

@ -26,7 +26,7 @@ class VacationLimitTest extends FeatureTestCase
->assertInertia( ->assertInertia(
fn(Assert $page) => $page fn(Assert $page) => $page
->component("VacationLimits") ->component("VacationLimits")
->has("limits.data", 10), ->has("limits", 10),
); );
} }