Files
fewo-stommer/app/Models/Review.php
T
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
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>
2026-10-23 12:00:11 +02:00

112 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\ReviewStatus;
use App\Models\Concerns\BelongsToTenant;
use Database\Factories\ReviewFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* A guest review. Reviews from the website always belong to a booking (verified stay);
* reviews taken over from portals carry their source instead.
*/
#[Fillable([
'property_id',
'booking_id',
'status',
'source',
'rating',
'rating_cleanliness',
'rating_equipment',
'rating_location',
'rating_host',
'title',
'body',
'private_feedback',
'display_name',
'publish_consent',
'stayed_on',
'response',
'responded_at',
'approved_at',
])]
class Review extends Model
{
/** @use HasFactory<ReviewFactory> */
use BelongsToTenant, HasFactory;
/** Rating categories besides the overall rating, with their German labels. */
public const CATEGORIES = [
'rating_cleanliness' => 'Sauberkeit',
'rating_equipment' => 'Ausstattung',
'rating_location' => 'Lage',
'rating_host' => 'Gastgeber',
];
protected static function booted(): void
{
static::saving(function (Review $review): void {
if ($review->status === ReviewStatus::Approved && $review->approved_at === null) {
$review->approved_at = now();
}
if ($review->isDirty('response')) {
$review->responded_at = filled($review->response) ? now() : null;
}
});
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => ReviewStatus::class,
'publish_consent' => 'boolean',
'stayed_on' => 'immutable_date',
'responded_at' => 'immutable_datetime',
'approved_at' => 'immutable_datetime',
];
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @return BelongsTo<Booking, $this>
*/
public function booking(): BelongsTo
{
return $this->belongsTo(Booking::class);
}
/**
* Reviews that may be shown on the website.
*
* @param Builder<Review> $query
* @return Builder<Review>
*/
#[Scope]
protected function published(Builder $query): Builder
{
return $query
->where('status', ReviewStatus::Approved)
->where('publish_consent', true)
->latest('stayed_on');
}
}