diff --git a/app/Architecture/Providers/ObserverServiceProvider.php b/app/Architecture/Providers/ObserverServiceProvider.php index 9457a10..3409e18 100644 --- a/app/Architecture/Providers/ObserverServiceProvider.php +++ b/app/Architecture/Providers/ObserverServiceProvider.php @@ -6,10 +6,8 @@ namespace Toby\Architecture\Providers; use Illuminate\Support\ServiceProvider; use Toby\Eloquent\Models\User; -use Toby\Eloquent\Models\VacationRequest; use Toby\Eloquent\Models\YearPeriod; use Toby\Eloquent\Observers\UserObserver; -use Toby\Eloquent\Observers\VacationRequestObserver; use Toby\Eloquent\Observers\YearPeriodObserver; class ObserverServiceProvider extends ServiceProvider @@ -18,6 +16,5 @@ class ObserverServiceProvider extends ServiceProvider { User::observe(UserObserver::class); YearPeriod::observe(YearPeriodObserver::class); - VacationRequest::observe(VacationRequestObserver::class); } } diff --git a/app/Eloquent/Models/Vacation.php b/app/Eloquent/Models/Vacation.php index 232fcfe..c22cf76 100644 --- a/app/Eloquent/Models/Vacation.php +++ b/app/Eloquent/Models/Vacation.php @@ -12,7 +12,6 @@ use Illuminate\Support\Carbon; /** * @property int $id * @property Carbon $date - * @property string $event_id * @property User $user * @property VacationRequest $vacationRequest * @property YearPeriod $yearPeriod diff --git a/app/Eloquent/Models/VacationRequest.php b/app/Eloquent/Models/VacationRequest.php index 0b36764..e85708f 100644 --- a/app/Eloquent/Models/VacationRequest.php +++ b/app/Eloquent/Models/VacationRequest.php @@ -6,6 +6,7 @@ namespace Toby\Eloquent\Models; use Database\Factories\VacationRequestFactory; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -18,6 +19,7 @@ use Toby\Domain\States\VacationRequest\VacationRequestState; /** * @property int $id + * @property string $name * @property VacationType $type * @property VacationRequestState $state * @property Carbon $from @@ -29,6 +31,7 @@ use Toby\Domain\States\VacationRequest\VacationRequestState; * @property YearPeriod $yearPeriod * @property Collection $activities * @property Collection $vacations + * @property Collection $event_ids * @property Carbon $created_at * @property Carbon $updated_at */ @@ -44,6 +47,7 @@ class VacationRequest extends Model "state" => VacationRequestState::class, "from" => "date", "to" => "date", + "event_ids" => AsCollection::class, ]; public function user(): BelongsTo @@ -87,6 +91,11 @@ class VacationRequest extends Model ->where("to", ">=", $vacationRequest->from); } + public function getNameAttribute(): string + { + return "{$this->id}/{$this->yearPeriod->year}"; + } + public function hasFlowSkipped(): bool { return $this->flow_skipped; diff --git a/app/Eloquent/Observers/VacationRequestObserver.php b/app/Eloquent/Observers/VacationRequestObserver.php deleted file mode 100644 index 7c041fa..0000000 --- a/app/Eloquent/Observers/VacationRequestObserver.php +++ /dev/null @@ -1,28 +0,0 @@ -from->year; - - $vacationRequestNumber = $vacationRequest->user->vacationRequests() - ->whereYear("from", $year) - ->count() + 1; - - $vacationRequest->name = "{$vacationRequestNumber}/${year}"; - } -} diff --git a/app/Infrastructure/Jobs/ClearVacationRequestDaysInGoogleCalendar.php b/app/Infrastructure/Jobs/ClearVacationRequestDaysInGoogleCalendar.php index 4ae157d..ec5e6ff 100644 --- a/app/Infrastructure/Jobs/ClearVacationRequestDaysInGoogleCalendar.php +++ b/app/Infrastructure/Jobs/ClearVacationRequestDaysInGoogleCalendar.php @@ -8,7 +8,6 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Spatie\GoogleCalendar\Event; -use Toby\Eloquent\Models\Vacation; use Toby\Eloquent\Models\VacationRequest; class ClearVacationRequestDaysInGoogleCalendar implements ShouldQueue @@ -22,17 +21,16 @@ class ClearVacationRequestDaysInGoogleCalendar implements ShouldQueue public function handle(): void { - $vacations = $this->vacationRequest->vacations() - ->whereNotNull("event_id") - ->get(); + foreach ($this->vacationRequest->event_ids as $eventId) { + $calendarEvent = Event::find($eventId); - /** @var Vacation $vacation */ - foreach ($vacations as $vacation) { - Event::find($vacation->event_id)->delete(); - - $vacation->update([ - "event_id" => null, - ]); + if ($calendarEvent->googleEvent->getStatus() !== "cancelled") { + $calendarEvent->delete(); + } } + + $this->vacationRequest->update([ + "event_ids" => null, + ]); } } diff --git a/app/Infrastructure/Jobs/SendVacationRequestDaysToGoogleCalendar.php b/app/Infrastructure/Jobs/SendVacationRequestDaysToGoogleCalendar.php index d00201a..e912d87 100644 --- a/app/Infrastructure/Jobs/SendVacationRequestDaysToGoogleCalendar.php +++ b/app/Infrastructure/Jobs/SendVacationRequestDaysToGoogleCalendar.php @@ -7,8 +7,9 @@ namespace Toby\Infrastructure\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Spatie\GoogleCalendar\Event; -use Toby\Eloquent\Models\Vacation; use Toby\Eloquent\Models\VacationRequest; class SendVacationRequestDaysToGoogleCalendar implements ShouldQueue @@ -22,21 +23,57 @@ class SendVacationRequestDaysToGoogleCalendar implements ShouldQueue public function handle(): void { - $vacations = $this->vacationRequest->vacations() - ->whereNull("event_id") - ->get(); + $days = $this->vacationRequest + ->vacations() + ->orderBy("date") + ->pluck("date"); + + $this->vacationRequest->event_ids = new Collection(); + $ranges = $this->prepareRanges($days); + + foreach ($ranges as $range) { + $text = "{$this->vacationRequest->type->label()} - {$this->vacationRequest->user->fullName} [{$this->vacationRequest->name}]"; - /** @var Vacation $vacation */ - foreach ($vacations as $vacation) { $event = Event::create([ - "name" => "{$this->vacationRequest->type->label()} - {$this->vacationRequest->user->fullName}", - "startDate" => $vacation->date, - "endDate" => $vacation->date, + "name" => $text, + "startDate" => Carbon::create($range["from"]), + "endDate" => Carbon::create($range["to"])->addDay(), ]); - $vacation->update([ - "event_id" => $event->id, - ]); + $this->vacationRequest->event_ids->add($event->id); } + + $this->vacationRequest->save(); + } + + protected function prepareRanges(Collection $days): array + { + $ranges = []; + $index = 0; + $first = $days->shift(); + + $ranges[$index] = [ + "from" => $first, + "to" => $first, + ]; + + foreach ($days as $day) { + if ($day->diffInDays($ranges[$index]["to"]) > 1) { + $index++; + + $ranges[$index] = [ + "from" => $day, + "to" => $day, + ]; + + continue; + } + + if ($day->isAfter($ranges[$index]["to"])) { + $ranges[$index]["to"] = $day; + } + } + + return $ranges; } } diff --git a/database/factories/VacationRequestFactory.php b/database/factories/VacationRequestFactory.php index f9689b7..7d2e57a 100644 --- a/database/factories/VacationRequestFactory.php +++ b/database/factories/VacationRequestFactory.php @@ -15,7 +15,6 @@ use Toby\Eloquent\Models\YearPeriod; class VacationRequestFactory extends Factory { protected $model = VacationRequest::class; - private static int $number = 1; public function definition(): array { @@ -26,7 +25,6 @@ class VacationRequestFactory extends Factory "user_id" => User::factory(), "creator_id" => fn(array $attributes): int => $attributes["user_id"], "year_period_id" => YearPeriod::factory(), - "name" => fn(array $attributes): string => $this->generateName($attributes), "type" => $this->faker->randomElement(VacationType::cases()), "state" => $this->faker->randomElement(VacationRequestStatesRetriever::all()), "from" => $from, @@ -34,12 +32,4 @@ class VacationRequestFactory extends Factory "comment" => $this->faker->boolean ? $this->faker->paragraph() : null, ]; } - - protected function generateName(array $attributes): string - { - $year = YearPeriod::find($attributes["year_period_id"])->year; - $number = static::$number++; - - return "{$number}/{$year}"; - } } diff --git a/database/migrations/2022_03_17_105827_remove_event_id_from_vacations_table.php b/database/migrations/2022_03_17_105827_remove_event_id_from_vacations_table.php new file mode 100644 index 0000000..b5f8c8a --- /dev/null +++ b/database/migrations/2022_03_17_105827_remove_event_id_from_vacations_table.php @@ -0,0 +1,23 @@ +dropColumn("event_id"); + }); + } + + public function down(): void + { + Schema::table("vacations", function (Blueprint $table): void { + $table->string("event_id")->nullable(); + }); + } +}; diff --git a/database/migrations/2022_03_17_105927_add_event_ids_column_and_remove_name_column_in_vacation_requests_table.php b/database/migrations/2022_03_17_105927_add_event_ids_column_and_remove_name_column_in_vacation_requests_table.php new file mode 100644 index 0000000..226f79a --- /dev/null +++ b/database/migrations/2022_03_17_105927_add_event_ids_column_and_remove_name_column_in_vacation_requests_table.php @@ -0,0 +1,25 @@ +json("event_ids")->nullable(); + $table->dropColumn("name"); + }); + } + + public function down(): void + { + Schema::table("vacation_requests", function (Blueprint $table): void { + $table->dropColumn("event_ids"); + $table->string("name")->nullable(); + }); + } +}; diff --git a/resources/js/Pages/VacationLimits.vue b/resources/js/Pages/VacationLimits.vue index c30a845..f71d327 100644 --- a/resources/js/Pages/VacationLimits.vue +++ b/resources/js/Pages/VacationLimits.vue @@ -1,5 +1,5 @@