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

@@ -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),
]);
}