Files
Helpddesk/Modules/Helpdesk/Models/Comment.php
T
2026-09-02 17:08:57 +00:00

111 lines
2.8 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Schema;
final class Comment extends Model
{
protected $table = 'helpdesk_comments';
/** @var list<string> */
protected $fillable = [
'ticket_id',
'parent_id',
'user_id',
'subject',
'body',
'body_text',
'is_internal',
'guid',
'unid',
'replica_id',
'parent_unid',
'root_unid',
'originator_id',
'sync_sequence',
'sync_hash',
'source_system',
'source_record_id',
'replicated_at',
'synced_at',
'tombstoned_at',
'deleted_by',
'delete_reason',
];
/** @var array<string, string> */
protected $casts = [
'is_internal' => 'boolean',
'sync_sequence' => 'integer',
'replicated_at' => 'datetime',
'synced_at' => 'datetime',
'tombstoned_at' => 'datetime',
'deleted_by' => 'integer',
];
/** @return BelongsTo<Ticket, Comment> */
public function ticket(): BelongsTo
{
return $this->belongsTo(Ticket::class, 'ticket_id');
}
/** @return BelongsTo<Comment, Comment> */
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
/** @return HasMany<Comment> */
public function replies(): HasMany
{
return $this->hasMany(self::class, 'parent_id')
->active()
->oldest();
}
/** @return BelongsTo<User, Comment> */
public function deleter(): BelongsTo
{
return $this->belongsTo(User::class, 'deleted_by');
}
/** @return BelongsTo<User, Comment> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
/** @return HasMany<Attachment> */
public function attachments(): HasMany
{
return $this->hasMany(Attachment::class, 'comment_id');
}
/**
* Scope the query to non-tombstoned comments when the legacy column exists.
*
* Some older Dusk databases can exist before the Lotus replication migration
* added tombstoned_at. In that state every row is active, so avoid issuing an
* invalid column query.
*
* @param Builder<Comment> $query
* @return Builder<Comment>
*/
public function scopeActive(Builder $query): Builder
{
if (! Schema::hasColumn($this->getTable(), 'tombstoned_at')) {
return $query;
}
return $query->whereNull('tombstoned_at');
}
}