Skip to content

Commit

Permalink
#398 - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Mar 20, 2024
1 parent c338bc0 commit d689b60
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 245 deletions.
5 changes: 2 additions & 3 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
use Toby\Domain\States\VacationRequest\Approved;
use Toby\Eloquent\Models\Holiday;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\Vacation;
Expand Down Expand Up @@ -89,15 +88,15 @@ public function aggregateVacationRequests(User $user, YearPeriod $yearPeriod): J
{
if ($user->can("listAllRequests")) {
$vacationRequests = $yearPeriod->vacationRequests()
->with(["user.profile", "vacations"])
->with(["user", "vacations.user.profile", "user.permissions", "user.profile"])
->states(VacationRequestStatesRetriever::waitingForUserActionStates($user))
->latest("updated_at")
->limit(3)
->cache()
->get();
} else {
$vacationRequests = $user->vacationRequests()
->with(["user.profile", "vacations"])
->with(["user", "vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->latest("updated_at")
->limit(3)
Expand Down
7 changes: 4 additions & 3 deletions app/Eloquent/Helpers/YearPeriodRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

namespace Toby\Eloquent\Helpers;

use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Session\Session;
use Toby\Eloquent\Models\YearPeriod;
use Illuminate\Support\Facades\Cache;

class YearPeriodRetriever
{
public const string SESSION_KEY = "selected_year_period";

public function __construct(
protected Session $session,
protected CacheManager $cacheManager,
) {}

public function selected(): YearPeriod
{
return Cache::remember('selected_year_period', 60, function () {
return $this->cacheManager->remember("selected_year_period", 60, function () {
/** @var YearPeriod $yearPeriod */
$yearPeriod = YearPeriod::query()->find($this->session->get(static::SESSION_KEY));

Expand All @@ -36,7 +37,7 @@ public function links(): array
$selected = $this->selected();
$current = $this->current();

$years = YearPeriod::all();
$years = YearPeriod::query()->cache()->get();

$navigation = $years->map(fn(YearPeriod $yearPeriod): array => $this->toNavigation($yearPeriod));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SelectYearPeriodController extends Controller
public function __invoke(Request $request, YearPeriod $yearPeriod): RedirectResponse
{
$request->session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id);
Cache::forget('selected_year_period');
Cache::forget("selected_year_period");

return redirect()
->back()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function index(Request $request, YearPeriodRetriever $yearPeriodRetriever

$vacationRequests = $request->user()
->vacationRequests()
->with(["user", "vacations"])
->with(["user", "vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriodRetriever->selected())
->latest()
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
Expand Down Expand Up @@ -89,7 +89,9 @@ public function indexForApprovers(
Request $request,
YearPeriodRetriever $yearPeriodRetriever,
): RedirectResponse|Response {
if ($request->user()->cannot("listAllRequests")) {
$requestUser = $request->user();

if ($requestUser->cannot("listAllRequests")) {
return redirect()->route("vacation.requests.index");
}

Expand All @@ -99,11 +101,11 @@ public function indexForApprovers(
$type = $request->get("type");

$vacationRequests = VacationRequest::query()
->with(["user", "vacations"])
->with(["user", "vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->when($user !== null, fn(Builder $query): Builder => $query->where("user_id", $user))
->when($type !== null, fn(Builder $query): Builder => $query->where("type", $type))
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $requestUser))
->latest()
->paginate();

Expand Down Expand Up @@ -131,7 +133,7 @@ public function show(VacationRequest $vacationRequest, UserVacationStatsRetrieve
{
$this->authorize("show", $vacationRequest);

$vacationRequest->load(["user", "vacations", "activities", "activities.user.profile"]);
$vacationRequest->load(["vacations.user.profile", "user.permissions", "user.profile", "activities.user.profile"]);
$limit = $statsRetriever->getVacationDaysLimit($vacationRequest->user, $vacationRequest->yearPeriod);
$used = $statsRetriever->getUsedVacationDays($vacationRequest->user, $vacationRequest->yearPeriod);
$pending = $statsRetriever->getPendingVacationDays($vacationRequest->user, $vacationRequest->yearPeriod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function share(Request $request): array

protected function getAuthData(Request $request): Closure
{
$user = $request->user();
$user = $request->user()->load("profile");

return fn(): array => [
"user" => $user ? new UserResource($user) : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function __construct($resource)
public function toArray($request): array
{
$user = $request->user();
$user->load("profile", "roles", "permissions");

return [
"id" => $this->id,
Expand All @@ -39,7 +38,7 @@ public function toArray($request): array
"to" => $this->to->toDisplayString(),
"displayDate" => $this->getDate($this->from->toDisplayString(), $this->to->toDisplayString()),
"comment" => $this->comment,
"days" => VacationResource::collection($this->vacations->load("user")),
"days" => VacationResource::collection($this->vacations),
"can" => [
"acceptAsTechnical" => $this->resource->state->canTransitionTo(AcceptedByTechnical::class)
&& $user->can("acceptAsTechApprover", $this->resource),
Expand Down
Loading

0 comments on commit d689b60

Please sign in to comment.