Files
WMS/Modules/Helpdesk/Livewire/TicketShow.php
T
2026-06-12 15:11:51 +01:00

183 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Livewire;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Livewire\WithFileUploads;
use Modules\Helpdesk\Models\Priority;
use Modules\Helpdesk\Models\Status;
use Modules\Helpdesk\Models\Ticket;
use Modules\Helpdesk\Services\TicketService;
use Modules\Helpdesk\Services\WorkflowNotificationService;
final class TicketShow extends Component
{
use WithFileUploads;
public Ticket $ticket;
#[Validate('required|string|min:2')]
public string $commentBody = '';
public bool $internal = false;
/** @var array<int, TemporaryUploadedFile> */
public array $attachments = [];
public ?int $status_id = null;
public ?int $priority_id = null;
public ?int $reply_parent_id = null;
public string $replyBody = '';
public function mount(Ticket $ticket): void
{
$this->authorize('view', $ticket);
$this->ticket = $this->freshTicket($ticket);
$this->status_id = $this->ticket->status_id;
$this->priority_id = $this->ticket->priority_id;
}
public function startReply(int $commentId): void
{
$this->reply_parent_id = $commentId;
$this->replyBody = '';
}
public function cancelReply(): void
{
$this->reply_parent_id = null;
$this->replyBody = '';
}
public function addComment(TicketService $tickets): void
{
$this->authorize('comment', $this->ticket);
$this->validate([
'commentBody' => ['required', 'string', 'min:2'],
'attachments.*' => 'file|max:'.config('helpdesk.max_upload_kb').'|mimes:'.implode(',', config('helpdesk.allowed_mimes')),
]);
$tickets->comment($this->ticket, Auth::user(), $this->commentBody, $this->internal, $this->attachments);
$this->reset(['commentBody', 'internal', 'attachments']);
$this->ticket = $this->freshTicket($this->ticket);
$this->dispatch('helpdesk-comment-added');
}
public function addReply(TicketService $tickets): void
{
$this->authorize('comment', $this->ticket);
$this->validate([
'reply_parent_id' => ['required', 'integer', 'exists:helpdesk_comments,id'],
'replyBody' => ['required', 'string', 'min:2'],
]);
$tickets->comment($this->ticket, Auth::user(), $this->replyBody, $this->internal, [], $this->reply_parent_id);
$this->cancelReply();
$this->ticket = $this->freshTicket($this->ticket);
$this->dispatch('helpdesk-reply-added');
}
public function updateStatus(WorkflowNotificationService $workflow): void
{
$this->authorize('update', $this->ticket);
$this->validate(['status_id' => ['required', 'integer', 'exists:helpdesk_statuses,id']]);
$status = Status::query()->findOrFail($this->status_id);
$this->ticket->forceFill([
'status_id' => $status->id,
'completed_at' => $status->is_closed ? now() : null,
'last_activity_at' => now(),
])->save();
$this->ticket = $this->freshTicket($this->ticket);
$workflow->dispatch($this->ticket, $status->is_closed ? 'ticket.closed' : 'ticket.updated', Auth::user());
$this->dispatch('helpdesk-status-updated');
}
public function updatePriority(WorkflowNotificationService $workflow): void
{
$this->authorize('update', $this->ticket);
$this->validate(['priority_id' => ['required', 'integer', 'exists:helpdesk_priorities,id']]);
$this->ticket->forceFill([
'priority_id' => $this->priority_id,
'last_activity_at' => now(),
])->save();
$this->ticket = $this->freshTicket($this->ticket);
$workflow->dispatch($this->ticket, 'ticket.updated', Auth::user());
$this->dispatch('helpdesk-priority-updated');
}
public function render(): View
{
return view('helpdesk::livewire.ticket-show', [
'statuses' => Status::query()->orderBy('sort_order')->get(),
'priorities' => Priority::query()->orderBy('sort_order')->get(),
])->layout('layouts.app');
}
private function freshTicket(Ticket $ticket): Ticket
{
$ticket = $ticket->fresh([
'owner',
'agent',
'status',
'priority',
'category',
'project',
'team',
'attachments.user',
'gitCommits.repository',
'comments.attachments',
'comments.replies.attachments',
'comments.replies.replies.attachments',
]) ?? $ticket;
$comments = $ticket->comments
->merge($ticket->comments->flatMap->replies)
->merge($ticket->comments->flatMap->replies->flatMap->replies);
$users = User::query()
->whereIn('id', $comments->pluck('user_id')->filter()->unique())
->get()
->keyBy('id');
$this->attachCommentUsers($ticket->comments, $users);
return $ticket;
}
/**
* Attach loaded users to comment hierarchy without triggering lazy loading.
*
* @param Collection<int, mixed> $comments
* @param Collection<int, User> $users
*/
private function attachCommentUsers(Collection $comments, Collection $users): void
{
foreach ($comments as $comment) {
$comment->setRelation('user', $users->get($comment->user_id));
if ($comment->relationLoaded('replies')) {
$this->attachCommentUsers($comment->replies, $users);
}
}
}
}