started to add indexes and fixed N+1 issue
This commit is contained in:
@@ -4,12 +4,15 @@ 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\Comment;
|
||||
use Modules\Helpdesk\Models\Priority;
|
||||
use Modules\Helpdesk\Models\Status;
|
||||
use Modules\Helpdesk\Models\Ticket;
|
||||
@@ -38,7 +41,6 @@ final class TicketShow extends Component
|
||||
|
||||
public string $replyBody = '';
|
||||
|
||||
/** @return void */
|
||||
public function mount(Ticket $ticket): void
|
||||
{
|
||||
$this->authorize('view', $ticket);
|
||||
@@ -47,28 +49,25 @@ final class TicketShow extends Component
|
||||
$this->priority_id = $this->ticket->priority_id;
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function startReply(int $commentId): void
|
||||
{
|
||||
$this->reply_parent_id = $commentId;
|
||||
$this->replyBody = '';
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function cancelReply(): void
|
||||
{
|
||||
$this->reply_parent_id = null;
|
||||
$this->replyBody = '';
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
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')),
|
||||
'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);
|
||||
@@ -78,7 +77,6 @@ final class TicketShow extends Component
|
||||
$this->dispatch('helpdesk-comment-added');
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function addReply(TicketService $tickets): void
|
||||
{
|
||||
$this->authorize('comment', $this->ticket);
|
||||
@@ -95,7 +93,6 @@ final class TicketShow extends Component
|
||||
$this->dispatch('helpdesk-reply-added');
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function updateStatus(WorkflowNotificationService $workflow): void
|
||||
{
|
||||
$this->authorize('update', $this->ticket);
|
||||
@@ -113,7 +110,6 @@ final class TicketShow extends Component
|
||||
$this->dispatch('helpdesk-status-updated');
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function updatePriority(WorkflowNotificationService $workflow): void
|
||||
{
|
||||
$this->authorize('update', $this->ticket);
|
||||
@@ -129,7 +125,6 @@ final class TicketShow extends Component
|
||||
$this->dispatch('helpdesk-priority-updated');
|
||||
}
|
||||
|
||||
/** @return View */
|
||||
public function render(): View
|
||||
{
|
||||
return view('helpdesk::livewire.ticket-show', [
|
||||
@@ -138,13 +133,48 @@ final class TicketShow extends Component
|
||||
])->layout('layouts.app');
|
||||
}
|
||||
|
||||
/** @return Ticket */
|
||||
private function freshTicket(Ticket $ticket): Ticket
|
||||
{
|
||||
return $ticket->fresh([
|
||||
'owner', 'agent', 'status', 'priority', 'category', 'project', 'team',
|
||||
'comments.user', 'comments.attachments', 'comments.replies.user', 'comments.replies.attachments', 'comments.replies.replies.user',
|
||||
'attachments.user', 'gitCommits.repository',
|
||||
$ticket = $ticket->fresh([
|
||||
'owner',
|
||||
'agent',
|
||||
'status',
|
||||
'priority',
|
||||
'category',
|
||||
'project',
|
||||
'team',
|
||||
'attachments.user',
|
||||
'gitCommits.repository',
|
||||
]) ?? $ticket;
|
||||
|
||||
$comments = Comment::query()
|
||||
->where('ticket_id', $ticket->id)
|
||||
->with(['user', 'attachments'])
|
||||
->oldest()
|
||||
->get();
|
||||
|
||||
$grouped = $comments->groupBy('parent_id');
|
||||
|
||||
$comments->each(function ($comment) use ($grouped): void {
|
||||
$comment->setRelation('replies', $grouped->get($comment->id, collect()));
|
||||
});
|
||||
|
||||
$ticket->setRelation('comments', $this->buildCommentTree($grouped));
|
||||
|
||||
return $ticket;
|
||||
}
|
||||
|
||||
private function buildCommentTree($grouped, ?int $parentId = null)
|
||||
|
||||
{
|
||||
|
||||
return $grouped->get($parentId, collect())->map(function ($comment) use ($grouped) {
|
||||
|
||||
$comment->setRelation('replies', $this->buildCommentTree($grouped, $comment->id));
|
||||
|
||||
return $comment;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user