Files
fewo-stommer/app/Actions/RequestReviews.php
T

45 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Actions;
use App\Enums\BookingStatus;
use App\Enums\Responsibility;
use App\Mail\ReviewRequest;
use App\Models\Booking;
use App\Process\OutwardSuspension;
use App\Process\ProcessPlan;
use Illuminate\Support\Facades\Mail;
/**
* Sends the review invitation once, a few days after departure. Old stays (more than 30 days ago)
* are skipped so that switching the feature on does not mail former guests.
*/
class RequestReviews
{
public function handle(): int
{
$latestDeparture = today()->subDays(config('fewo.review_request_after_days'));
$bookings = Booking::query()
->whereIn('status', BookingStatus::occupying())
->whereNull('review_requested_at')
->whereDate('departure', '<=', $latestDeparture)
->whereDate('departure', '>', $latestDeparture->copy()->subDays(30))
->whereDoesntHave('review')
->whereHas('guest', fn ($query) => $query->whereNull('anonymized_at')->whereNotNull('email')->where('email', '!=', ''))
->with('guest')
->get()
->filter(fn (Booking $booking): bool => $booking->process()->guestMessages() === Responsibility::Landlord
// The step can be switched off per channel (Kanal › Ablauf); an open portal report holds it back (O4).
&& ProcessPlan::of($booking)->enabled('review.request')
&& OutwardSuspension::reason($booking) === null);
foreach ($bookings as $booking) {
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new ReviewRequest($booking));
$booking->update(['review_requested_at' => now()]);
}
return $bookings->count();
}
}