From a37be18da6a0da092c5460b117b184813d59a2a0 Mon Sep 17 00:00:00 2001 From: Adrian Hopek Date: Tue, 1 Mar 2022 12:25:07 +0100 Subject: [PATCH] #43 - wip --- .../Providers/AppServiceProvider.php | 8 ---- app/Domain/UserVacationStatsRetriever.php | 5 +++ .../CalculateUserVacationStatsController.php | 38 +++++++++++++++++++ .../Http/Controllers/HolidayController.php | 8 +++- .../Controllers/VacationLimitController.php | 25 ++++++++++-- .../Http/Resources/VacationLimitResource.php | 22 ----------- resources/js/Pages/VacationLimits.vue | 13 ++++++- resources/js/Shared/MainMenu.vue | 2 - routes/api.php | 24 +----------- tests/Feature/VacationLimitTest.php | 2 +- 10 files changed, 85 insertions(+), 62 deletions(-) create mode 100644 app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php delete mode 100644 app/Infrastructure/Http/Resources/VacationLimitResource.php diff --git a/app/Architecture/Providers/AppServiceProvider.php b/app/Architecture/Providers/AppServiceProvider.php index b1dc896..23059cb 100644 --- a/app/Architecture/Providers/AppServiceProvider.php +++ b/app/Architecture/Providers/AppServiceProvider.php @@ -6,9 +6,6 @@ namespace Toby\Architecture\Providers; use Illuminate\Support\Carbon; use Illuminate\Support\ServiceProvider; -use Toby\Eloquent\Models\Holiday; -use Toby\Eloquent\Models\VacationLimit; -use Toby\Eloquent\Scopes\SelectedYearPeriodScope; class AppServiceProvider extends ServiceProvider { @@ -16,10 +13,5 @@ class AppServiceProvider extends ServiceProvider { Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y")); Carbon::macro("toDisplayDate", fn() => $this->translatedFormat("d.m.Y")); - - $selectedYearPeriodScope = $this->app->make(SelectedYearPeriodScope::class); - - VacationLimit::addGlobalScope($selectedYearPeriodScope); - Holiday::addGlobalScope($selectedYearPeriodScope); } } diff --git a/app/Domain/UserVacationStatsRetriever.php b/app/Domain/UserVacationStatsRetriever.php index 02ce455..830a3cd 100644 --- a/app/Domain/UserVacationStatsRetriever.php +++ b/app/Domain/UserVacationStatsRetriever.php @@ -59,6 +59,11 @@ class UserVacationStatsRetriever ->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 { $limit = $user->vacationLimits() diff --git a/app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php b/app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php new file mode 100644 index 0000000..554027d --- /dev/null +++ b/app/Infrastructure/Http/Controllers/Api/CalculateUserVacationStatsController.php @@ -0,0 +1,38 @@ +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, + ]); + } +} diff --git a/app/Infrastructure/Http/Controllers/HolidayController.php b/app/Infrastructure/Http/Controllers/HolidayController.php index fe256dd..ed28f6b 100644 --- a/app/Infrastructure/Http/Controllers/HolidayController.php +++ b/app/Infrastructure/Http/Controllers/HolidayController.php @@ -6,6 +6,7 @@ namespace Toby\Infrastructure\Http\Controllers; use Illuminate\Http\RedirectResponse; use Inertia\Response; +use Toby\Eloquent\Helpers\YearPeriodRetriever; use Toby\Eloquent\Models\Holiday; use Toby\Infrastructure\Http\Requests\HolidayRequest; use Toby\Infrastructure\Http\Resources\HolidayFormDataResource; @@ -13,9 +14,12 @@ use Toby\Infrastructure\Http\Resources\HolidayResource; 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") ->get(); diff --git a/app/Infrastructure/Http/Controllers/VacationLimitController.php b/app/Infrastructure/Http/Controllers/VacationLimitController.php index 2762b7d..9cac7a9 100644 --- a/app/Infrastructure/Http/Controllers/VacationLimitController.php +++ b/app/Infrastructure/Http/Controllers/VacationLimitController.php @@ -6,22 +6,39 @@ namespace Toby\Infrastructure\Http\Controllers; use Illuminate\Http\RedirectResponse; use Inertia\Response; +use Toby\Domain\UserVacationStatsRetriever; +use Toby\Eloquent\Helpers\YearPeriodRetriever; use Toby\Eloquent\Models\VacationLimit; +use Toby\Eloquent\Models\YearPeriod; use Toby\Infrastructure\Http\Requests\VacationLimitRequest; -use Toby\Infrastructure\Http\Resources\VacationLimitResource; +use Toby\Infrastructure\Http\Resources\UserResource; 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") ->orderByUserField("last_name") ->orderByUserField("first_name") ->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", [ - "limits" => VacationLimitResource::collection($limits), + "limits" => $limitsResource, ]); } diff --git a/app/Infrastructure/Http/Resources/VacationLimitResource.php b/app/Infrastructure/Http/Resources/VacationLimitResource.php deleted file mode 100644 index 4226a14..0000000 --- a/app/Infrastructure/Http/Resources/VacationLimitResource.php +++ /dev/null @@ -1,22 +0,0 @@ - $this->id, - "user" => new UserResource($this->user), - "hasVacation" => $this->hasVacation(), - "days" => $this->days, - ]; - } -} diff --git a/resources/js/Pages/VacationLimits.vue b/resources/js/Pages/VacationLimits.vue index 5a23728..c30a845 100644 --- a/resources/js/Pages/VacationLimits.vue +++ b/resources/js/Pages/VacationLimits.vue @@ -34,6 +34,12 @@ > Posiada urlop? + + Pozostałe dni z poprzedniego roku + + +
+ {{ item.remainingLastYear }} +
+
group(function (): void { Route::post("calculate-vacation-days", CalculateVacationDaysController::class); - Route::post("calculate-vacations-stats", function (CalculateVacationStatsRequest $request, UserVacationStatsRetriever $vacationStatsRetriever) { - /** @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, - ]; - }); + Route::post("calculate-vacations-stats", CalculateUserVacationStatsController::class); }); diff --git a/tests/Feature/VacationLimitTest.php b/tests/Feature/VacationLimitTest.php index e57e9e3..de37060 100644 --- a/tests/Feature/VacationLimitTest.php +++ b/tests/Feature/VacationLimitTest.php @@ -26,7 +26,7 @@ class VacationLimitTest extends FeatureTestCase ->assertInertia( fn(Assert $page) => $page ->component("VacationLimits") - ->has("limits.data", 10), + ->has("limits", 10), ); }