Attempt to fix N+1 issues

This commit is contained in:
Mark Gregory
2026-06-16 20:09:21 +01:00
parent 85b96144c0
commit e81a509007
6 changed files with 67 additions and 22 deletions
@@ -24,6 +24,8 @@ final class TicketControlsController extends Controller
*/
public function updateAgent(Request $request, Ticket $ticket, WorkflowNotificationService $workflow): RedirectResponse
{
$ticket->loadMissing($this->ticketActionRelations());
Gate::authorize('update', $ticket);
$validated = $request->validate([
@@ -48,7 +50,7 @@ final class TicketControlsController extends Controller
'last_activity_at' => now(),
]);
$fresh = $ticket->fresh(['agent.company', 'owner.company', 'status', 'priority', 'category', 'project', 'team', 'product']) ?? $ticket;
$fresh = $ticket->fresh(['agent', 'owner', 'status', 'priority', 'category', 'project', 'team', 'product']) ?? $ticket;
$workflow->dispatch($fresh, 'ticket.updated', Auth::user());
return redirect()
@@ -63,6 +65,8 @@ final class TicketControlsController extends Controller
*/
public function updateStatus(Request $request, Ticket $ticket, WorkflowNotificationService $workflow, SlaService $sla): RedirectResponse
{
$ticket->loadMissing($this->ticketActionRelations());
Gate::authorize('update', $ticket);
$validated = $request->validate([
@@ -79,7 +83,7 @@ final class TicketControlsController extends Controller
$this->ensureBoardContainsStatus($ticket, (int) $status->id);
$fresh = $ticket->fresh(['agent.company', 'owner.company', 'status', 'priority', 'category', 'project', 'team', 'product']) ?? $ticket;
$fresh = $ticket->fresh(['agent', 'owner', 'status', 'priority', 'category', 'project', 'team', 'product']) ?? $ticket;
if ($status->is_closed) {
$sla->recordClosure($fresh);
@@ -97,6 +101,8 @@ final class TicketControlsController extends Controller
*/
public function updatePriority(Request $request, Ticket $ticket, WorkflowNotificationService $workflow): RedirectResponse
{
$ticket->loadMissing($this->ticketActionRelations());
Gate::authorize('update', $ticket);
$validated = $request->validate([
@@ -108,7 +114,7 @@ final class TicketControlsController extends Controller
'last_activity_at' => now(),
]);
$fresh = $ticket->fresh(['agent.company', 'owner.company', 'status', 'priority', 'category', 'project', 'team', 'product']) ?? $ticket;
$fresh = $ticket->fresh(['agent', 'owner', 'status', 'priority', 'category', 'project', 'team', 'product']) ?? $ticket;
$workflow->dispatch($fresh, 'ticket.updated', Auth::user());
return redirect()
@@ -116,6 +122,25 @@ final class TicketControlsController extends Controller
->with('ticket-action-updated', 'Priority updated to '.($fresh->priority?->name ?? 'selected priority').'.');
}
/**
* Relations needed by policies, redirect messages, and workflow templates.
*
* @return list<string>
*/
private function ticketActionRelations(): array
{
return [
'owner.company',
'agent.company',
'status',
'priority',
'category',
'project',
'team',
'product',
];
}
/**
* Apply a direct guarded ticket update without relying on Livewire hydration.
*