*/ 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->ticket = $this->freshTicket($ticket); $this->authorize('view', $this->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); } /** * Persist the selected ticket status using a direct table update so Livewire UI wrappers cannot silently fail. */ public function updateStatus(WorkflowNotificationService $workflow, SlaService $sla): void { $this->authorize('update', $this->ticket); $statusId = $this->normaliseNullableInt($this->status_id); validator( ['status_id' => $statusId], ['status_id' => ['required', 'integer', Rule::exists('helpdesk_statuses', 'id')]], )->validate(); $status = Status::query()->findOrFail($statusId); $updates = [ 'status_id' => $status->id, 'completed_at' => $status->is_closed ? now() : null, 'last_activity_at' => now(), ]; if (Schema::hasColumn('helpdesk_tickets', 'updated_at')) { $updates['updated_at'] = now(); } DB::table('helpdesk_tickets') ->where('id', $this->ticket->id) ->update($updates); $this->ticket = $this->freshTicket($this->ticket); $this->status_id = (int) $this->ticket->status_id; if ($status->is_closed) { $sla->recordClosure($this->ticket); } $workflow->dispatch($this->ticket, $status->is_closed ? 'ticket.closed' : 'ticket.updated', Auth::user()); session()->flash('ticket-action-updated', 'Status updated to '.$this->ticket->status?->name.'.'); $this->dispatch('helpdesk-status-updated', statusId: $this->status_id); } /** * Persist the selected ticket priority using a direct table update. */ public function updatePriority(WorkflowNotificationService $workflow): void { $this->authorize('update', $this->ticket); $priorityId = $this->normaliseNullableInt($this->priority_id); validator( ['priority_id' => $priorityId], ['priority_id' => ['required', 'integer', Rule::exists('helpdesk_priorities', 'id')]], )->validate(); $updates = [ 'priority_id' => $priorityId, 'last_activity_at' => now(), ]; if (Schema::hasColumn('helpdesk_tickets', 'updated_at')) { $updates['updated_at'] = now(); } DB::table('helpdesk_tickets') ->where('id', $this->ticket->id) ->update($updates); $this->ticket = $this->freshTicket($this->ticket); $this->priority_id = (int) $this->ticket->priority_id; $workflow->dispatch($this->ticket, 'ticket.updated', Auth::user()); session()->flash('ticket-action-updated', 'Priority updated to '.$this->ticket->priority?->name.'.'); $this->dispatch('helpdesk-priority-updated', priorityId: $this->priority_id); } /** * Persist the selected ticket assignee. */ public function updateAgent(WorkflowNotificationService $workflow): void { $this->authorize('update', $this->ticket); $agentId = $this->normaliseNullableInt($this->agent_id); validator( ['agent_id' => $agentId], ['agent_id' => ['nullable', 'integer', Rule::exists('users', 'id')]], )->validate(); abort_unless(Schema::hasColumn('helpdesk_tickets', 'agent_id'), 500, 'The helpdesk_tickets.agent_id column is missing.'); $updates = [ 'agent_id' => $agentId, 'last_activity_at' => now(), ]; if (Schema::hasColumn('helpdesk_tickets', 'updated_at')) { $updates['updated_at'] = now(); } DB::table('helpdesk_tickets') ->where('id', $this->ticket->id) ->update($updates); $this->ticket = $this->freshTicket($this->ticket); $this->agent_id = $this->ticket->agent_id === null ? null : (int) $this->ticket->agent_id; $workflow->dispatch($this->ticket, 'ticket.updated', Auth::user()); session()->flash('ticket-action-updated', $this->agent_id === null ? 'Ticket is now unassigned.' : 'Ticket assigned to '.$this->ticket->agent?->name.'.'); $this->dispatch('helpdesk-agent-updated', agentId: $this->agent_id); } 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'); } /** * Convert Livewire select values into a nullable integer. */ private function normaliseNullableInt(mixed $value): ?int { if ($value === null || $value === '' || $value === '0' || $value === 0) { return null; } return (int) $value; } private function freshTicket(Ticket $ticket): Ticket { $ticket = $ticket->fresh([ 'status', 'priority', 'category', 'project', 'team', 'product', 'slaPolicy', 'slaEvents', 'ratings', 'tags', 'checklistItems', 'attachments', 'gitCommits.repository', ]) ?? $ticket; $this->hydrateTicketUsers($ticket); $comments = \Modules\Helpdesk\Models\Comment::query() ->where('ticket_id', $ticket->id) ->with(['user.company', 'attachments']) ->oldest() ->get(); $ticket->setRelation('comments', $this->buildCommentTree($comments->groupBy('parent_id'))); return $ticket; } /** * Load owner and agent users with one query and attach aliases used by the view/workflow. */ private function hydrateTicketUsers(Ticket $ticket): void { $userIds = collect([$ticket->user_id, $ticket->agent_id]) ->filter() ->map(static fn (mixed $id): int => (int) $id) ->unique() ->values(); if ($userIds->isEmpty()) { $ticket->setRelation('user', null); $ticket->setRelation('owner', null); $ticket->setRelation('agent', null); return; } $users = User::query() ->with('company') ->whereIn('id', $userIds) ->get() ->keyBy('id'); $owner = $users->get((int) $ticket->user_id); $agent = $ticket->agent_id === null ? null : $users->get((int) $ticket->agent_id); $ticket->setRelation('user', $owner); $ticket->setRelation('owner', $owner); $ticket->setRelation('agent', $agent); } /** * Return assignable users for this ticket. * * The dropdown must never be blank just because optional helpdesk_agent/helpdesk_admin * flags are missing or false. Prefer tenant/company users, then fall back to all users. * * @return EloquentCollection */ private function agentOptions(): EloquentCollection { $columns = ['id', 'name', 'email']; if (Schema::hasColumn('users', 'company_id')) { $columns[] = 'company_id'; } $query = User::query()->select($columns)->orderBy('name'); if ($this->ticket->company_id !== null && Schema::hasColumn('users', 'company_id')) { $tenantUsers = (clone $query) ->where(function ($query): void { $query->where('company_id', $this->ticket->company_id) ->orWhereNull('company_id'); }) ->when($this->agent_id !== null, function ($query): void { $query->orWhere('id', $this->agent_id); }) ->get(); if ($tenantUsers->isNotEmpty()) { return $tenantUsers; } } return $query->get(); } 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(); } }