This commit is contained in:
Adrian Hopek
2022-02-14 14:56:30 +01:00
parent d3d6e3080c
commit 1fe6e72e9a
9 changed files with 69 additions and 34 deletions

View File

@@ -50,8 +50,11 @@ class CalendarGenerator
protected function generateCalendar(CarbonPeriod $period, Collection $holidays): array
{
$calendar = [];
$vacations = $this->getVacationsForPeriod($period);
foreach ($period as $day) {
$vacationsForDay = $vacations[$day->toDateString()] ?? new Collection();
$calendar[] = [
"date" => $day->toDateString(),
"dayOfMonth" => $day->translatedFormat("j"),
@@ -59,18 +62,19 @@ class CalendarGenerator
"isToday" => $day->isToday(),
"isWeekend" => $day->isWeekend(),
"isHoliday" => $holidays->contains($day),
"vacations" => $this->getVacationsForDay($day),
"vacations" => $vacationsForDay->pluck("user_id"),
];
}
return $calendar;
}
protected function getVacationsForDay(CarbonInterface $day): Collection
protected function getVacationsForPeriod(CarbonPeriod $period): Collection
{
return Vacation::query()
->whereDate("date", $day)
->whereBetween("date", [$period->start, $period->end])
->whereRelation("vacationRequest", "state", VacationRequestState::APPROVED->value)
->pluck("user_id");
->get()
->groupBy(fn(Vacation $vacation) => $vacation->date->toDateString());
}
}

View File

@@ -4,23 +4,64 @@ declare(strict_types=1);
namespace Toby\Domain\Validation\Rules;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Toby\Domain\Enums\VacationRequestState;
use Toby\Domain\Enums\VacationType;
use Toby\Domain\VacationDaysCalculator;
use Toby\Domain\VacationTypeConfigRetriever;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
use Toby\Eloquent\Models\YearPeriod;
class DoesNotExceedLimitRule implements VacationRequestRule
{
public function __construct(
protected VacationTypeConfigRetriever $configRetriever,
protected VacationDaysCalculator $vacationDaysCalculator,
) {
}
public function check(VacationRequest $vacationRequest): bool
{
return true;
if (!$this->configRetriever->hasLimit($vacationRequest->type)) {
return true;
}
$limit = $this->getUserVacationLimit($vacationRequest->user, $vacationRequest->yearPeriod);
$vacationDays = $this->getVacationDaysWithLimit($vacationRequest->user, $vacationRequest->yearPeriod);
$estimatedDays = $this->vacationDaysCalculator->calculateDays($vacationRequest->yearPeriod, $vacationRequest->from, $vacationRequest->to)->count();
return $limit >= ($vacationDays + $estimatedDays);
}
public function errorMessage(): string
{
return __("You have exceeded your vacation limit.");
}
protected function getUserVacationLimit(User $user, YearPeriod $yearPeriod): int
{
return $user->vacationLimits()->where("year_period_id", $yearPeriod->id)->first()->days ?? 0;
}
protected function getVacationDaysWithLimit(User $user, YearPeriod $yearPeriod): int
{
return $user->vacations()
->where("year_period_id", $yearPeriod->id)
->whereRelation(
"vacationRequest",
fn(Builder $query) => $query
->whereIn("type", $this->getLimitableVacationTypes())
->noStates(VacationRequestState::failedStates()),
)
->count();
}
protected function getLimitableVacationTypes(): Collection
{
$types = new Collection(VacationType::cases());
return $types->filter(fn(VacationType $type) => $this->configRetriever->hasLimit($type));
}
}