279 lines
9.5 KiB
PHP
279 lines
9.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Livewire;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
|
use Livewire\WithFileUploads;
|
|
use Modules\Helpdesk\Models\CannedResponse;
|
|
use Modules\Helpdesk\Models\ChecklistItem;
|
|
use Modules\Helpdesk\Models\Priority;
|
|
use Modules\Helpdesk\Models\Status;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
use Modules\Helpdesk\Services\CannedResponseRenderer;
|
|
use Modules\Helpdesk\Services\ChecklistService;
|
|
use Modules\Helpdesk\Services\SlaService;
|
|
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 $agent_id = null;
|
|
|
|
public ?int $reply_parent_id = null;
|
|
|
|
public string $replyBody = '';
|
|
|
|
public ?int $canned_response_id = null;
|
|
|
|
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;
|
|
$this->agent_id = $this->ticket->agent_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', 10240).'|mimes:'.implode(',', config('helpdesk.allowed_mimes', ['jpg', 'jpeg', 'png', 'pdf', 'txt', 'log', 'zip'])),
|
|
]);
|
|
|
|
$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-editor-reset', model: 'commentBody');
|
|
$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->reset(['reply_parent_id', 'replyBody']);
|
|
$this->ticket = $this->freshTicket($this->ticket);
|
|
$this->dispatch('helpdesk-editor-reset', model: 'replyBody');
|
|
$this->dispatch('helpdesk-reply-added');
|
|
}
|
|
|
|
public function applyCannedResponse(CannedResponseRenderer $renderer): void
|
|
{
|
|
$this->authorize('comment', $this->ticket);
|
|
|
|
if ($this->canned_response_id === null) {
|
|
return;
|
|
}
|
|
|
|
$response = CannedResponse::query()
|
|
->where('is_active', true)
|
|
->findOrFail($this->canned_response_id);
|
|
|
|
$rendered = $renderer->render($response->body, $this->ticket, Auth::user());
|
|
$this->commentBody = trim($this->commentBody."\n\n".$rendered);
|
|
$this->dispatch('helpdesk-canned-response-applied', body: $this->commentBody);
|
|
}
|
|
|
|
public function toggleChecklistItem(int $itemId, ChecklistService $checklists): void
|
|
{
|
|
$this->authorize('update', $this->ticket);
|
|
|
|
$item = ChecklistItem::query()
|
|
->where('ticket_id', $this->ticket->id)
|
|
->findOrFail($itemId);
|
|
|
|
$checklists->toggle($item, Auth::user());
|
|
$this->ticket = $this->freshTicket($this->ticket);
|
|
}
|
|
|
|
public function updateStatus(WorkflowNotificationService $workflow, SlaService $sla): 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();
|
|
|
|
if ($status->is_closed) {
|
|
$sla->recordClosure($this->ticket);
|
|
}
|
|
|
|
$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 updateAgent(WorkflowNotificationService $workflow): void
|
|
{
|
|
$this->authorize('update', $this->ticket);
|
|
$this->validate(['agent_id' => ['nullable', 'integer', 'exists:users,id']]);
|
|
|
|
$this->ticket->forceFill([
|
|
'agent_id' => $this->agent_id,
|
|
'last_activity_at' => now(),
|
|
])->save();
|
|
|
|
$this->ticket = $this->freshTicket($this->ticket);
|
|
$workflow->dispatch($this->ticket, 'ticket.updated', Auth::user());
|
|
$this->dispatch('helpdesk-agent-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(),
|
|
'agents' => $this->agentOptions(),
|
|
'cannedResponses' => CannedResponse::query()->where('is_active', true)->orderBy('sort_order')->orderBy('name')->get(),
|
|
])->layout('layouts.app');
|
|
}
|
|
|
|
private function freshTicket(Ticket $ticket): Ticket
|
|
{
|
|
$ticket = $ticket->fresh([
|
|
'owner',
|
|
'agent',
|
|
'status',
|
|
'priority',
|
|
'category',
|
|
'project',
|
|
'team',
|
|
'product',
|
|
'slaPolicy',
|
|
'slaEvents',
|
|
'ratings',
|
|
'tags',
|
|
'checklistItems',
|
|
'attachments.user',
|
|
'gitCommits.repository',
|
|
]) ?? $ticket;
|
|
|
|
$comments = \Modules\Helpdesk\Models\Comment::query()
|
|
->where('ticket_id', $ticket->id)
|
|
->with(['user', 'attachments.user'])
|
|
->oldest()
|
|
->get();
|
|
|
|
$ticket->setRelation('comments', $this->buildCommentTree($comments->groupBy('parent_id')));
|
|
|
|
return $ticket;
|
|
}
|
|
|
|
/**
|
|
* Return assignable users. Falls back to all tenant users when helpdesk flags/roles are absent.
|
|
*
|
|
* @return EloquentCollection<int, User>
|
|
*/
|
|
private function agentOptions(): EloquentCollection
|
|
{
|
|
return User::query()
|
|
->select(['id', 'name', 'email', 'company_id'])
|
|
->when($this->ticket->company_id !== null && Schema::hasColumn('users', 'company_id'), function ($query): void {
|
|
$query->where(function ($query): void {
|
|
$query->where('company_id', $this->ticket->company_id)
|
|
->orWhereNull('company_id');
|
|
});
|
|
})
|
|
->when(Schema::hasColumn('users', 'helpdesk_agent') || Schema::hasColumn('users', 'helpdesk_admin'), function ($query): void {
|
|
$query->where(function ($query): void {
|
|
if (Schema::hasColumn('users', 'helpdesk_agent')) {
|
|
$query->orWhere('helpdesk_agent', true);
|
|
}
|
|
|
|
if (Schema::hasColumn('users', 'helpdesk_admin')) {
|
|
$query->orWhere('helpdesk_admin', true);
|
|
}
|
|
|
|
if ($this->agent_id !== null) {
|
|
$query->orWhereKey($this->agent_id);
|
|
}
|
|
});
|
|
})
|
|
->orderBy('name')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Build an unlimited nested comment tree from a flat eager-loaded collection.
|
|
*
|
|
* @param Collection<int|string, Collection<int, mixed>> $grouped
|
|
* @return Collection<int, mixed>
|
|
*/
|
|
private function buildCommentTree(Collection $grouped, ?int $parentId = null): Collection
|
|
{
|
|
return $grouped->get($parentId, collect())->map(function ($comment) use ($grouped) {
|
|
$comment->setRelation('replies', $this->buildCommentTree($grouped, (int) $comment->id));
|
|
|
|
return $comment;
|
|
})->values();
|
|
}
|
|
}
|