This commit is contained in:
Adrian Hopek
2022-02-14 13:42:49 +01:00
parent d026d41715
commit d3d6e3080c
7 changed files with 239 additions and 163 deletions

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Toby\Domain;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod;
use Illuminate\Support\Collection;
use Toby\Domain\Enums\VacationRequestState;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\Vacation;
use Toby\Eloquent\Models\YearPeriod;
class CalendarGenerator
{
public function __construct(
protected YearPeriodRetriever $yearPeriodRetriever,
) {
}
public function generate(YearPeriod $yearPeriod, string $month): array
{
$date = CarbonImmutable::create($yearPeriod->year, $this->monthNameToNumber($month));
$period = CarbonPeriod::create($date->startOfMonth(), $date->endOfMonth());
$holidays = $yearPeriod->holidays()->pluck("date");
return $this->generateCalendar($period, $holidays);
}
protected function monthNameToNumber($name): int
{
return match ($name) {
default => CarbonInterface::JANUARY,
"february" => CarbonInterface::FEBRUARY,
"march" => CarbonInterface::MARCH,
"april" => CarbonInterface::APRIL,
"may" => CarbonInterface::MAY,
"june" => CarbonInterface::JUNE,
"july" => CarbonInterface::JULY,
"august" => CarbonInterface::AUGUST,
"september" => CarbonInterface::SEPTEMBER,
"october" => CarbonInterface::OCTOBER,
"november" => CarbonInterface::NOVEMBER,
"december" => CarbonInterface::DECEMBER,
};
}
protected function generateCalendar(CarbonPeriod $period, Collection $holidays): array
{
$calendar = [];
foreach ($period as $day) {
$calendar[] = [
"date" => $day->toDateString(),
"dayOfMonth" => $day->translatedFormat("j"),
"dayOfWeek" => $day->translatedFormat("D"),
"isToday" => $day->isToday(),
"isWeekend" => $day->isWeekend(),
"isHoliday" => $holidays->contains($day),
"vacations" => $this->getVacationsForDay($day),
];
}
return $calendar;
}
protected function getVacationsForDay(CarbonInterface $day): Collection
{
return Vacation::query()
->whereDate("date", $day)
->whereRelation("vacationRequest", "state", VacationRequestState::APPROVED->value)
->pluck("user_id");
}
}

View File

@@ -4,63 +4,36 @@ declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Inertia\Response;
use Toby\Domain\Enums\VacationRequestState;
use Toby\Domain\CalendarGenerator;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\Vacation;
use Toby\Infrastructure\Http\Resources\UserResource;
class VacationCalendarController extends Controller
{
public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response
{
$month = $request->query("month", "february");
public function index(
Request $request,
YearPeriodRetriever $yearPeriodRetriever,
CalendarGenerator $calendarGenerator,
): Response {
$month = Str::lower($request->query("month", Carbon::now()->englishMonth));
$yearPeriod = $yearPeriodRetriever->selected();
$date = CarbonImmutable::create($yearPeriod->year, $this->monthNameToNumber($month));
$period = CarbonPeriod::create($date->startOfMonth(), $date->endOfMonth());
$holidays = $yearPeriod->holidays()->pluck("date");
$users = User::query()
->with([
"vacations" => fn($query) => $query
->whereBetween("date", [$period->start, $period->end])
->whereRelation("vacationRequest", "state", VacationRequestState::APPROVED->value),
])
->orderBy("last_name")
->orderBy("first_name")
->get();
$calendar = [];
foreach ($period as $day) {
$calendar[] = [
"date" => $day->toDateString(),
"dayOfMonth" => $day->translatedFormat("j"),
"dayOfWeek" => $day->translatedFormat("D"),
"isToday" => $day->isToday(),
"isWeekend" => $day->isWeekend(),
"isHoliday" => $holidays->contains($day),
];
}
$userVacations = [];
/** @var User $user */
foreach ($users as $user) {
$userVacations[] = [
"user" => new UserResource($user),
"vacations" => $user->vacations->map(fn(Vacation $vacation) => $vacation->date->toDateString()),
];
}
$calendar = $calendarGenerator->generate($yearPeriod, $month);
return inertia("Calendar", [
"calendar" => $calendar,
"currentMonth" => $month,
"userVacations" => $userVacations,
"users" => UserResource::collection($users),
]);
}