Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website. Co-authored-by: Cursor <cursoragent@cursor.com>
59 lines
1.7 KiB
PHP
59 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Actions\CreateChannelTasks;
|
|
use App\Enums\BookingStatus;
|
|
use App\Enums\ChannelTaskAction;
|
|
use App\Models\Booking;
|
|
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
|
|
|
|
/**
|
|
* Keeps manually maintained portals in step when a booking starts or stops occupying the calendar.
|
|
*/
|
|
class BookingObserver implements ShouldHandleEventsAfterCommit
|
|
{
|
|
public function __construct(private CreateChannelTasks $createChannelTasks) {}
|
|
|
|
public function created(Booking $booking): void
|
|
{
|
|
if ($this->occupies($booking->status)) {
|
|
$this->tasks($booking, ChannelTaskAction::Block);
|
|
}
|
|
}
|
|
|
|
public function updated(Booking $booking): void
|
|
{
|
|
if (! $booking->wasChanged('status')) {
|
|
return;
|
|
}
|
|
|
|
$before = $this->occupies($booking->getOriginal('status'));
|
|
$after = $this->occupies($booking->status);
|
|
|
|
if (! $before && $after) {
|
|
$this->tasks($booking, ChannelTaskAction::Block);
|
|
} elseif ($before && ! $after) {
|
|
$this->tasks($booking, ChannelTaskAction::Unblock);
|
|
}
|
|
}
|
|
|
|
private function occupies(BookingStatus|string|null $status): bool
|
|
{
|
|
$status = is_string($status) ? BookingStatus::tryFrom($status) : $status;
|
|
|
|
return in_array($status, BookingStatus::occupying(), true);
|
|
}
|
|
|
|
private function tasks(Booking $booking, ChannelTaskAction $action): void
|
|
{
|
|
$property = $booking->property()->withoutGlobalScope('tenant')->first();
|
|
|
|
if ($property === null) {
|
|
return;
|
|
}
|
|
|
|
$this->createChannelTasks->handle($property, $booking->arrival, $booking->departure, $action, "Buchung {$booking->reference}");
|
|
}
|
|
}
|