155 lines
4.7 KiB
PHP
Executable File
155 lines
4.7 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Livewire;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Attributes\Url;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
use Modules\Helpdesk\Models\Priority;
|
|
use Modules\Helpdesk\Models\Status;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
|
|
final class TicketIndex extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
use WithPagination;
|
|
|
|
public bool $archiveModalOpen = false;
|
|
|
|
public ?int $pendingArchiveTicketId = null;
|
|
|
|
public ?string $pendingArchiveTicketReference = null;
|
|
|
|
public ?string $pendingArchiveTicketSubject = null;
|
|
|
|
#[Url]
|
|
public string $search = '';
|
|
|
|
#[Url]
|
|
public string $status = '';
|
|
|
|
#[Url]
|
|
public string $priority = '';
|
|
|
|
#[Url]
|
|
public string $scope = 'open';
|
|
|
|
public function updated(): void
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
/**
|
|
* Open the archive confirmation modal for a visible ticket.
|
|
*/
|
|
public function askToArchiveTicket(int $ticketId): void
|
|
{
|
|
$ticket = Ticket::query()
|
|
->visibleTo(Auth::user())
|
|
->findOrFail($ticketId);
|
|
|
|
$this->pendingArchiveTicketId = $ticket->id;
|
|
$this->pendingArchiveTicketReference = $ticket->reference;
|
|
$this->pendingArchiveTicketSubject = $ticket->subject;
|
|
$this->archiveModalOpen = true;
|
|
}
|
|
|
|
public function cancelTicketArchive(): void
|
|
{
|
|
$this->resetTicketArchiveState();
|
|
}
|
|
|
|
/**
|
|
* Archive the confirmed ticket using Laravel soft deletes.
|
|
*/
|
|
public function confirmTicketArchive(): void
|
|
{
|
|
if ($this->pendingArchiveTicketId === null) {
|
|
$this->resetTicketArchiveState();
|
|
|
|
return;
|
|
}
|
|
|
|
$ticket = Ticket::query()
|
|
->visibleTo(Auth::user())
|
|
->findOrFail($this->pendingArchiveTicketId);
|
|
|
|
$this->authorize('delete', $ticket);
|
|
|
|
$reference = $ticket->reference;
|
|
|
|
$ticket->forceFill(['archived_at' => now()])->save();
|
|
$ticket->delete();
|
|
|
|
$this->resetTicketArchiveState();
|
|
$this->resetPage();
|
|
|
|
$this->dispatch(
|
|
'thredary:toast',
|
|
type: 'success',
|
|
title: 'Ticket archived',
|
|
message: "Ticket {$reference} was moved to the archive.",
|
|
);
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('modules.helpdesk.livewire.ticket-index', [
|
|
'tickets' => $this->tickets(),
|
|
'statuses' => Status::query()->orderBy('sort_order')->get(),
|
|
'priorities' => Priority::query()->orderBy('sort_order')->get(),
|
|
])->layout('layouts.app');
|
|
}
|
|
|
|
private function resetTicketArchiveState(): void
|
|
{
|
|
$this->archiveModalOpen = false;
|
|
$this->pendingArchiveTicketId = null;
|
|
$this->pendingArchiveTicketReference = null;
|
|
$this->pendingArchiveTicketSubject = null;
|
|
}
|
|
|
|
/**
|
|
* @return LengthAwarePaginator<int, Ticket>
|
|
*/
|
|
private function tickets(): LengthAwarePaginator
|
|
{
|
|
$user = Auth::user();
|
|
$search = trim($this->search);
|
|
|
|
return Ticket::query()
|
|
->without('user')
|
|
->select('helpdesk_tickets.*')
|
|
->addSelect([
|
|
'agent_name' => User::query()
|
|
->select('name')
|
|
->whereColumn('users.id', 'helpdesk_tickets.agent_id')
|
|
->limit(1),
|
|
])
|
|
->with(['status', 'priority', 'category', 'project', 'team'])
|
|
->withCount('attachments')
|
|
->visibleTo($user)
|
|
->when($this->scope === 'open', static fn (Builder $query): Builder => $query->whereNull('completed_at'))
|
|
->when($this->scope === 'closed', static fn (Builder $query): Builder => $query->whereNotNull('completed_at'))
|
|
->when($this->status !== '', fn (Builder $query): Builder => $query->where('status_id', (int) $this->status))
|
|
->when($this->priority !== '', fn (Builder $query): Builder => $query->where('priority_id', (int) $this->priority))
|
|
->when($search !== '', static function (Builder $query) use ($search): Builder {
|
|
return $query->where(static function (Builder $subQuery) use ($search): void {
|
|
$subQuery->where('reference', 'like', '%'.$search.'%')
|
|
->orWhere('subject', 'like', '%'.$search.'%')
|
|
->orWhere('body', 'like', '%'.$search.'%');
|
|
});
|
|
})
|
|
->latest('last_activity_at')
|
|
->paginate(15);
|
|
}
|
|
}
|