google calendar improvements
This commit is contained in:
parent
afb1a5e9ff
commit
ed3cab29b3
@ -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
|
||||
|
@ -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;
|
||||
@ -29,6 +30,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 +46,7 @@ class VacationRequest extends Model
|
||||
"state" => VacationRequestState::class,
|
||||
"from" => "date",
|
||||
"to" => "date",
|
||||
"event_ids" => AsCollection::class,
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
|
@ -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();
|
||||
if ($calendarEvent->googleEvent->getStatus() !== "cancelled") {
|
||||
$calendarEvent->delete();
|
||||
}
|
||||
}
|
||||
|
||||
$vacation->update([
|
||||
"event_id" => null,
|
||||
$this->vacationRequest->update([
|
||||
"event_ids" => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,55 @@ class SendVacationRequestDaysToGoogleCalendar implements ShouldQueue
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$vacations = $this->vacationRequest->vacations()
|
||||
->whereNull("event_id")
|
||||
->get();
|
||||
$days = $this->vacationRequest
|
||||
->vacations()
|
||||
->orderBy("date")
|
||||
->pluck("date");
|
||||
|
||||
/** @var Vacation $vacation */
|
||||
foreach ($vacations as $vacation) {
|
||||
$this->vacationRequest->event_ids = new Collection();
|
||||
$ranges = $this->prepareRanges($days);
|
||||
|
||||
foreach ($ranges as $range) {
|
||||
$event = Event::create([
|
||||
"name" => "{$this->vacationRequest->type->label()} - {$this->vacationRequest->user->fullName}",
|
||||
"startDate" => $vacation->date,
|
||||
"endDate" => $vacation->date,
|
||||
"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;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class() extends Migration {
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table("vacations", function (Blueprint $table): void {
|
||||
$table->dropColumn("event_id");
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table("vacations", function (Blueprint $table): void {
|
||||
$table->string("event_id")->nullable();
|
||||
});
|
||||
}
|
||||
};
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class() extends Migration {
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table("vacation_requests", function (Blueprint $table): void {
|
||||
$table->json("event_ids")->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table("vacation_requests", function (Blueprint $table): void {
|
||||
$table->dropColumn("event_ids");
|
||||
});
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user