fixe duploads and attachments now with colors tab and enhancements

This commit is contained in:
Mark Gregory
2026-06-16 19:01:26 +01:00
parent c19497efc2
commit f8ae45d810
8 changed files with 289 additions and 108 deletions
+124 -44
View File
@@ -9,6 +9,8 @@ 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\DB;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Schema;
use Livewire\Attributes\Validate;
use Livewire\Component;
@@ -135,55 +137,124 @@ final class TicketShow extends Component
$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);
$this->validate(['status_id' => ['required', 'integer', 'exists:helpdesk_statuses,id']]);
$status = Status::query()->findOrFail($this->status_id);
$this->ticket->forceFill([
$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(),
])->save();
];
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);
}
$this->ticket = $this->freshTicket($this->ticket);
$workflow->dispatch($this->ticket, $status->is_closed ? 'ticket.closed' : 'ticket.updated', Auth::user());
$this->dispatch('helpdesk-status-updated');
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);
$this->validate(['priority_id' => ['required', 'integer', 'exists:helpdesk_priorities,id']]);
$this->ticket->forceFill([
'priority_id' => $this->priority_id,
$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(),
])->save();
];
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());
$this->dispatch('helpdesk-priority-updated');
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);
$this->validate(['agent_id' => ['nullable', 'integer', 'exists:users,id']]);
$this->ticket->forceFill([
'agent_id' => $this->agent_id,
$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(),
])->save();
];
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());
$this->dispatch('helpdesk-agent-updated');
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
@@ -196,6 +267,18 @@ final class TicketShow extends Component
])->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([
@@ -228,45 +311,42 @@ final class TicketShow extends Component
}
/**
* Return assignable users. Falls back to all tenant users when helpdesk flags/roles are absent.
* 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<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 {
$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(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);
}
})
->when($this->agent_id !== null, function ($query): void {
$query->orWhereKey($this->agent_id);
})
->get();
if (Schema::hasColumn('users', 'helpdesk_admin')) {
$query->orWhere('helpdesk_admin', true);
}
if ($tenantUsers->isNotEmpty()) {
return $tenantUsers;
}
}
if ($this->agent_id !== null) {
$query->orWhereKey($this->agent_id);
}
});
})
->orderBy('name')
->get();
return $query->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) {