diff --git a/Modules/Helpdesk/Livewire/TicketShow.php b/Modules/Helpdesk/Livewire/TicketShow.php index 6aa2a267..9df52792 100644 --- a/Modules/Helpdesk/Livewire/TicketShow.php +++ b/Modules/Helpdesk/Livewire/TicketShow.php @@ -41,11 +41,11 @@ final class TicketShow extends Component /** @var array */ public array $attachments = []; - public ?int $status_id = null; + public int|string|null $status_id = null; - public ?int $priority_id = null; + public int|string|null $priority_id = null; - public ?int $agent_id = null; + public int|string|null $agent_id = null; public ?int $reply_parent_id = null; @@ -225,10 +225,7 @@ final class TicketShow extends Component $agentId = $this->normaliseNullableInt($this->agent_id); - validator( - ['agent_id' => $agentId], - ['agent_id' => ['nullable', 'integer', Rule::exists('users', 'id')]], - )->validate(); + $this->validateAssignableAgent($agentId); abort_unless(Schema::hasColumn('helpdesk_tickets', 'agent_id'), 500, 'The helpdesk_tickets.agent_id column is missing.'); @@ -310,6 +307,39 @@ final class TicketShow extends Component return $ticket; } + /** + * Validate that the selected agent exists and belongs to this ticket's company context. + */ + private function validateAssignableAgent(?int $agentId): void + { + if ($agentId === null) { + return; + } + + $rule = Rule::exists('users', 'id'); + + if ($this->ticket->company_id !== null && Schema::hasColumn('users', 'company_id')) { + $ticketCompanyId = (int) $this->ticket->company_id; + $currentAgentId = $this->ticket->agent_id === null ? null : (int) $this->ticket->agent_id; + + $rule->where(function ($query) use ($ticketCompanyId, $currentAgentId): void { + $query->where(function ($tenantQuery) use ($ticketCompanyId): void { + $tenantQuery->where('company_id', $ticketCompanyId) + ->orWhereNull('company_id'); + }); + + if ($currentAgentId !== null) { + $query->orWhere('id', $currentAgentId); + } + }); + } + + validator( + ['agent_id' => $agentId], + ['agent_id' => ['nullable', 'integer', $rule]], + )->validate(); + } + /** * Return assignable users for this ticket. * diff --git a/Modules/Helpdesk/Resources/views/livewire/ticket-show.blade.php b/Modules/Helpdesk/Resources/views/livewire/ticket-show.blade.php index 61fc003f..b198ff27 100644 --- a/Modules/Helpdesk/Resources/views/livewire/ticket-show.blade.php +++ b/Modules/Helpdesk/Resources/views/livewire/ticket-show.blade.php @@ -202,10 +202,10 @@ @can('update', $ticket)
-
+

Ticket controls

-

Assignment, status and priority

-

Use the explicit update buttons below to save each change.

+

Assignment, status and priority

+

Choose a value, then save the specific change.

@if (session('ticket-action-updated')) @@ -214,9 +214,9 @@
@endif -
+
- @forelse ($agents as $agent) @@ -225,42 +225,42 @@ @endforelse @error('agent_id')

{{ $message }}

@enderror - - +
-
+
- @foreach ($statuses as $status) @endforeach @error('status_id')

{{ $message }}

@enderror - - +
-
+
- @foreach ($priorities as $priority) @endforeach @error('priority_id')

{{ $message }}

@enderror - - +
@endcan