Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#378 - clickable events in calendar #383

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Domain/Actions/Slack/UpdateDailySummaryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Toby\Domain\Actions\Slack;

use Toby\Domain\DailySummaryRetriever;
use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Eloquent\Models\DailySummary;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\VacationRequest;
Expand All @@ -22,12 +23,14 @@ public function execute(DailySummary $dailySummary): DailySummary
fn(VacationRequest $request) => [
"id" => $request->user->id,
"name" => $request->user->profile->fullName,
"pending" => $request->state->equals(...VacationRequestStatesRetriever::pendingStates()),
],
)->toArray(),
"remotes" => $this->dailySummaryRetriever->getRemoteDays($dailySummary->day)->map(
fn(VacationRequest $request) => [
"id" => $request->user->id,
"name" => $request->user->profile->fullName,
"pending" => $request->state->equals(...VacationRequestStatesRetriever::pendingStates()),
],
)->toArray(),
"birthdays" => $this->dailySummaryRetriever->getUpcomingBirthdays()->map(fn(User $user) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Toby\Domain\Actions\VacationRequest;

use Spatie\Permission\Models\Permission;
use Toby\Domain\Events\VacationRequestChanged;
use Toby\Domain\Notifications\VacationRequestWaitsForApprovalNotification;
use Toby\Domain\VacationRequestStateManager;
use Toby\Domain\VacationTypeConfigRetriever;
Expand All @@ -26,6 +27,8 @@ public function execute(VacationRequest $vacationRequest): void
if ($this->configRetriever->isVacation($vacationRequest->type)) {
$this->notifyAuthorizedUsers($vacationRequest);
}

event(new VacationRequestChanged($vacationRequest));
}

protected function notifyAuthorizedUsers(VacationRequest $vacationRequest): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Toby\Domain\Actions\VacationRequest;

use Spatie\Permission\Models\Permission;
use Toby\Domain\Events\VacationRequestChanged;
use Toby\Domain\Notifications\VacationRequestWaitsForApprovalNotification;
use Toby\Domain\VacationRequestStateManager;
use Toby\Domain\VacationTypeConfigRetriever;
Expand All @@ -26,6 +27,8 @@ public function execute(VacationRequest $vacationRequest): void
if ($this->configRetriever->isVacation($vacationRequest->type)) {
$this->notifyTechApprovers($vacationRequest);
}

event(new VacationRequestChanged($vacationRequest));
}

protected function notifyTechApprovers(VacationRequest $vacationRequest): void
Expand Down
12 changes: 6 additions & 6 deletions app/Domain/CalendarGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\Vacation;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Resources\SimpleVacationRequestResource;

class CalendarGenerator
{
Expand All @@ -31,11 +32,9 @@ protected function generateCalendar(CarbonPeriod $period, Collection $holidays):
{
$calendar = [];
$vacations = $this->getVacationsForPeriod($period);
$pendingVacations = $this->getPendingVacationsForPeriod($period);

foreach ($period as $day) {
$vacationsForDay = $vacations[$day->toDateString()] ?? new Collection();
$pendingVacationsForDay = $pendingVacations[$day->toDateString()] ?? new Collection();

$calendar[] = [
"date" => $day->toDateString(),
Expand All @@ -44,10 +43,8 @@ protected function generateCalendar(CarbonPeriod $period, Collection $holidays):
"isToday" => $day->isToday(),
"isWeekend" => $day->isWeekend(),
"isHoliday" => $holidays->contains($day),
"vacations" => $vacationsForDay->pluck("user_id"),
"pendingVacations" => $pendingVacationsForDay->pluck("user_id"),
"vacationTypes" => $vacationsForDay->pluck("vacationRequest.type", "user_id"),
"vacationPendingTypes" => $pendingVacationsForDay->pluck("vacationRequest.type", "user_id"),
"vacations" => $vacationsForDay
->mapWithKeys(fn(Vacation $vacation): array => [$vacation->user_id => new SimpleVacationRequestResource($vacation->vacationRequest)]),
];
}

Expand All @@ -59,6 +56,9 @@ protected function getVacationsForPeriod(CarbonPeriod $period): Collection
return Vacation::query()
->whereBetween("date", [$period->start, $period->end])
->approved()
->orWhere(function ($query): void {
$query->pending();
})
->with("vacationRequest")
->get()
->groupBy(fn(Vacation $vacation): string => $vacation->date->toDateString());
Expand Down
8 changes: 4 additions & 4 deletions app/Domain/DailySummaryRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getAbsences(Carbon $date): Collection
->with(["user"])
->whereDate("from", "<=", $date)
->whereDate("to", ">=", $date)
->states(VacationRequestStatesRetriever::successStates())
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
"type",
VacationType::all()->filter(fn(VacationType $type): bool => $this->configRetriever->isVacation($type)),
Expand All @@ -44,7 +44,7 @@ public function getRemoteDays(Carbon $date): Collection
->with(["user"])
->whereDate("from", "<=", $date)
->whereDate("to", ">=", $date)
->states(VacationRequestStatesRetriever::successStates())
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
"type",
VacationType::all()->filter(fn(VacationType $type): bool => !$this->configRetriever->isVacation($type)),
Expand All @@ -61,7 +61,7 @@ public function getUpcomingAbsences(Carbon $date): Collection
return VacationRequest::query()
->with(["user"])
->whereDate("from", ">", $date)
->states(VacationRequestStatesRetriever::successStates())
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
"type",
VacationType::all()->filter(fn(VacationType $type): bool => $this->configRetriever->isVacation($type)),
Expand All @@ -79,7 +79,7 @@ public function getUpcomingRemoteDays(Carbon $date): Collection
return VacationRequest::query()
->with(["user"])
->whereDate("from", ">", $date)
->states(VacationRequestStatesRetriever::successStates())
->states(VacationRequestStatesRetriever::notFailedStates())
->whereIn(
"type",
VacationType::all()->filter(fn(VacationType $type): bool => !$this->configRetriever->isVacation($type)),
Expand Down
14 changes: 7 additions & 7 deletions app/Domain/DashboardAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Toby\Eloquent\Models\Vacation;
use Toby\Eloquent\Models\YearPeriod;
use Toby\Infrastructure\Http\Resources\BirthdayResource;
use Toby\Infrastructure\Http\Resources\DashboardVacationRequestResource;
use Toby\Infrastructure\Http\Resources\HolidayResource;
use Toby\Infrastructure\Http\Resources\SimpleVacationRequestResource;
use Toby\Infrastructure\Http\Resources\UserBenefitsResource;
use Toby\Infrastructure\Http\Resources\VacationRequestResource;

Expand Down Expand Up @@ -55,7 +55,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
->get()
->mapWithKeys(
fn(Vacation $vacation): array => [
$vacation->date->toDateString() => new SimpleVacationRequestResource($vacation->vacationRequest),
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest),
],
);

Expand All @@ -67,7 +67,7 @@ public function aggregateCalendarData(User $user, YearPeriod $yearPeriod): array
->get()
->mapWithKeys(
fn(Vacation $vacation): array => [
$vacation->date->toDateString() => new SimpleVacationRequestResource($vacation->vacationRequest),
$vacation->date->toDateString() => new DashboardVacationRequestResource($vacation->vacationRequest),
],
);

Expand Down Expand Up @@ -109,8 +109,8 @@ public function aggregateCurrentData(): array
$remoteDays = $this->dailySummaryRetriever->getRemoteDays($today);

return [
"absences" => VacationRequestResource::collection($absences),
"remoteDays" => VacationRequestResource::collection($remoteDays),
"absences" => DashboardVacationRequestResource::collection($absences),
"remoteDays" => DashboardVacationRequestResource::collection($remoteDays),
];
}

Expand All @@ -129,8 +129,8 @@ public function aggregateUpcomingData(): array
->get();

return [
"absences" => VacationRequestResource::collection($upcomingAbsences),
"remoteDays" => VacationRequestResource::collection($upcomingRemoteDays),
"absences" => DashboardVacationRequestResource::collection($upcomingAbsences),
"remoteDays" => DashboardVacationRequestResource::collection($upcomingRemoteDays),
"birthdays" => BirthdayResource::collection($upcomingBirthdays),
"holidays" => HolidayResource::collection($upcomingHolidays),
];
Expand Down
5 changes: 5 additions & 0 deletions app/Domain/VacationRequestStatesRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static function failedStates(): array
];
}

public static function notFailedStates(): array
{
return array_merge(static::successStates(), static::pendingStates());
}

public static function waitingForUserActionStates(User $user): array
{
return match ($user->role) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Toby\Infrastructure\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Domain\VacationTypeConfigRetriever;

class DashboardVacationRequestResource extends JsonResource
{
public static $wrap = null;
protected VacationTypeConfigRetriever $configRetriever;

public function __construct($resource)
{
parent::__construct($resource);

$this->configRetriever = app(VacationTypeConfigRetriever::class);
}

public function toArray($request): array
{
$user = $request->user();

return [
"id" => $this->id,
"user" => new SimpleUserResource($this->user),
"type" => $this->type,
"state" => $this->state,
"from" => $this->from->toDisplayString(),
"to" => $this->to->toDisplayString(),
"displayDate" => $this->getDate($this->from->toDisplayString(), $this->to->toDisplayString()),
"days" => VacationResource::collection($this->vacations),
"pending" => $this->state->equals(...VacationRequestStatesRetriever::pendingStates()),
];
}

private function getDate(string $from, string $to): string
{
return ($from !== $to)
? "{$from} - {$to}"
: $from;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Toby\Infrastructure\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Toby\Domain\VacationRequestStatesRetriever;

class SimpleVacationRequestResource extends JsonResource
{
Expand All @@ -17,6 +18,7 @@ public function toArray($request): array
"name" => $this->name,
"type" => $this->type,
"state" => $this->state,
"pending" => $this->state->equals(...VacationRequestStatesRetriever::pendingStates()),
"from" => $this->from->toDisplayString(),
"to" => $this->to->toDisplayString(),
"days" => $this->vacations->count(),
Expand Down
3 changes: 2 additions & 1 deletion config/vacation_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@
],
VacationType::Sick->value => [
VacationTypeConfigRetriever::KEY_TECHNICAL_APPROVAL => false,
VacationTypeConfigRetriever::KEY_ADMINISTRATIVE_APPROVAL => false,
VacationTypeConfigRetriever::KEY_ADMINISTRATIVE_APPROVAL => true,
VacationTypeConfigRetriever::KEY_BILLABLE => true,
VacationTypeConfigRetriever::KEY_HAS_LIMIT => false,
VacationTypeConfigRetriever::KEY_AVAILABLE_FOR => [
EmploymentForm::EmploymentContract,
],
VacationTypeConfigRetriever::KEY_REQUEST_ALLOWED_FOR => [
Role::Employee,
Role::Administrator,
Role::AdministrativeApprover,
Role::TechnicalApprover,
Expand Down
10 changes: 5 additions & 5 deletions database/seeders/DemoSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,19 @@ public function run(): void

$users = User::all();

$year = 2021;
$year = Carbon::now()->year;

YearPeriod::factory()
->count(3)
->sequence(
[
"year" => Carbon::createFromDate($year)->year,
"year" => Carbon::createFromDate($year - 1)->year,
],
[
"year" => Carbon::createFromDate($year + 1)->year,
"year" => Carbon::createFromDate($year)->year,
],
[
"year" => Carbon::createFromDate($year + 2)->year,
"year" => Carbon::createFromDate($year + 1)->year,
],
)
->afterCreating(function (YearPeriod $yearPeriod) use ($users): void {
Expand All @@ -162,7 +162,7 @@ public function run(): void
})
->create();

$currentYearPeriod = YearPeriod::query()->where("year", 2022)->first();
$currentYearPeriod = YearPeriod::query()->where("year", $year)->first();

/** @var VacationRequest $vacationRequestApproved */
$vacationRequestApproved = VacationRequest::factory([
Expand Down
5 changes: 4 additions & 1 deletion resources/js/Pages/AnnualSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ function getVacationInfo(day) {
</button>
</div>
<template #content>
<VacationPopup :vacation="getVacationInfo(day)" />
<VacationPopup
:vacation="getVacationInfo(day)"
:see-vacation-details="true"
/>
</template>
</Popper>
<Popper
Expand Down
19 changes: 9 additions & 10 deletions resources/js/Pages/Calendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/vue/24/solid'
import { computed, ref } from 'vue'
import { useMonthInfo } from '@/Composables/monthInfo.js'
import VacationTypeCalendarIcon from '@/Shared/VacationTypeCalendarIcon.vue'
import CalendarDay from '@/Shared/CalendarDay.vue'

const props = defineProps({
users: Object,
Expand Down Expand Up @@ -40,6 +41,10 @@ function unsetActiveDay() {
function linkParameters(user, day) {
return props.auth.can.createRequestsOnBehalfOfEmployee ? { user: user.id, from_date: day.date } : { from_date: day.date }
}

function linkVacationRequest(user){
return props.auth.user.id === user.id || props.auth.can.manageRequestsAsTechnicalApprover || props.auth.can.manageRequestsAsAdministrativeApprover
}
</script>

<template>
Expand Down Expand Up @@ -149,20 +154,14 @@ function linkParameters(user, day) {
@mouseleave="unsetActiveDay"
>
<div
v-if="day.pendingVacations.includes(user.id) && (auth.user.id === user.id || auth.can.manageRequestsAsAdministrativeApprover || auth.can.manageRequestsAsTechnicalApprover)"
v-if="user.id in day.vacations"
class="flex justify-center items-center"
>
<VacationTypeCalendarIcon
:type="day.vacationPendingTypes[user.id]"
:opacity="true"
<CalendarDay
:vacation="day.vacations[user.id]"
:see-vacation-details="linkVacationRequest(user)"
/>
</div>
<div
v-else-if="day.vacations.includes(user.id)"
class="flex justify-center items-center"
>
<VacationTypeCalendarIcon :type="day.vacationTypes[user.id]" />
</div>
<template
v-else-if="isActiveDay(user.id + '+' + day.date) && !day.isWeekend && !day.isHoliday && (auth.user.id === user.id || auth.can.createRequestsOnBehalfOfEmployee)"
>
Expand Down
30 changes: 30 additions & 0 deletions resources/js/Shared/CalendarDay.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script setup>
import VacationPopup from '@/Shared/VacationPopup.vue'
import VacationTypeCalendarIcon from '@/Shared/VacationTypeCalendarIcon.vue'
import Popper from 'vue3-popper'

const props = defineProps({
vacation: Object,
seeVacationDetails: Boolean,
})

</script>

<template>
<Popper
open-delay="200"
hover
offset-distance="0"
>
<VacationTypeCalendarIcon
:type="props.vacation.type"
:opacity="props.vacation.pending"
/>
<template #content>
<VacationPopup
:vacation="props.vacation"
:see-vacation-details="props.seeVacationDetails"
/>
</template>
</Popper>
</template>
Loading
Loading