phases one of time logged now with log display
This commit is contained in:
@@ -5,8 +5,8 @@ namespace Modules\Helpdesk\Livewire;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Livewire\Component;
|
||||
use Modules\Helpdesk\Models\Ticket;
|
||||
@@ -25,7 +25,7 @@ class TicketTimer extends Component
|
||||
public int $elapsedSeconds = 0;
|
||||
|
||||
/**
|
||||
* Mount the timer for the current ticket.
|
||||
* Mount the ticket timer component.
|
||||
*/
|
||||
public function mount(Ticket $ticket): void
|
||||
{
|
||||
@@ -34,7 +34,7 @@ class TicketTimer extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a database-backed timer for the authenticated user.
|
||||
* Start a user-specific timer for this ticket.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
@@ -42,33 +42,32 @@ class TicketTimer extends Component
|
||||
{
|
||||
$user = $this->authenticatedUser();
|
||||
|
||||
if ($this->userHasAnyActiveTimer($user)) {
|
||||
Gate::authorize('update', $this->ticket);
|
||||
|
||||
if ($this->activeUserTimerQuery($user)->exists()) {
|
||||
throw ValidationException::withMessages([
|
||||
'timer' => 'You already have a running timer. End the current timer before starting another one.',
|
||||
'timer' => 'You already have a running timer. End it before starting another one.',
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->activeAnyTicketTimerQuery($user)->exists()) {
|
||||
throw ValidationException::withMessages([
|
||||
'timer' => 'You already have a running timer on another ticket. End it before starting another one.',
|
||||
]);
|
||||
}
|
||||
|
||||
$startedAt = Carbon::now();
|
||||
|
||||
try {
|
||||
TicketTimeLog::query()->create([
|
||||
'ticket_id' => $this->ticket->getKey(),
|
||||
'user_id' => $user->getAuthIdentifier(),
|
||||
'minutes' => 0,
|
||||
'description' => $this->normalisedDescription(),
|
||||
'started_at' => $startedAt,
|
||||
'ended_at' => null,
|
||||
'logged_at' => $startedAt,
|
||||
]);
|
||||
} catch (QueryException $exception) {
|
||||
if ($this->isUniqueTimerConstraintViolation($exception)) {
|
||||
throw ValidationException::withMessages([
|
||||
'timer' => 'You already have a running timer. End the current timer before starting another one.',
|
||||
]);
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
TicketTimeLog::query()->create([
|
||||
'ticket_id' => $this->ticket->getKey(),
|
||||
'user_id' => $user->getAuthIdentifier(),
|
||||
'minutes' => 0,
|
||||
'seconds' => 0,
|
||||
'description' => $this->normalisedDescription(),
|
||||
'started_at' => $startedAt,
|
||||
'ended_at' => null,
|
||||
'logged_at' => $startedAt,
|
||||
]);
|
||||
|
||||
$this->description = '';
|
||||
$this->message = 'Timer started.';
|
||||
@@ -78,14 +77,17 @@ class TicketTimer extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* End the active timer and add the elapsed minutes to the ticket total.
|
||||
* End the active user timer and add the elapsed time to the ticket.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function endTimer(): void
|
||||
{
|
||||
$user = $this->authenticatedUser();
|
||||
$timer = $this->activeTimerQuery($user)->first();
|
||||
|
||||
Gate::authorize('update', $this->ticket);
|
||||
|
||||
$timer = $this->activeUserTimerQuery($user)->first();
|
||||
|
||||
if (! $timer instanceof TicketTimeLog) {
|
||||
throw ValidationException::withMessages([
|
||||
@@ -94,21 +96,24 @@ class TicketTimer extends Component
|
||||
}
|
||||
|
||||
$endedAt = Carbon::now();
|
||||
$minutes = max(1, (int) ceil($timer->started_at->diffInSeconds($endedAt) / 60));
|
||||
$seconds = max(1, (int) $timer->started_at->diffInSeconds($endedAt));
|
||||
$minutes = (int) ceil($seconds / 60);
|
||||
$description = trim($this->description) !== '' ? trim($this->description) : $timer->description;
|
||||
|
||||
$timer->forceFill([
|
||||
'ended_at' => $endedAt,
|
||||
'seconds' => $seconds,
|
||||
'minutes' => $minutes,
|
||||
'description' => $description,
|
||||
'logged_at' => $endedAt,
|
||||
])->save();
|
||||
|
||||
$this->refreshTicketTimeSpentMinutes();
|
||||
$this->updateTicketTimeSpentFromSeconds();
|
||||
|
||||
$this->activeTimer = null;
|
||||
$this->elapsedSeconds = 0;
|
||||
$this->description = '';
|
||||
$this->message = 'Timer ended and time was added to the ticket.';
|
||||
$this->refreshActiveTimer();
|
||||
|
||||
$this->dispatch('timer-ended');
|
||||
}
|
||||
@@ -116,15 +121,15 @@ class TicketTimer extends Component
|
||||
/**
|
||||
* Render the ticket timer view.
|
||||
*/
|
||||
public function render()
|
||||
public function render(): mixed
|
||||
{
|
||||
return view('helpdesk::livewire.ticket-timer');
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the active timer and elapsed seconds from the database.
|
||||
* Refresh the active timer from the database.
|
||||
*/
|
||||
public function refreshActiveTimer(): void
|
||||
private function refreshActiveTimer(): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
@@ -135,16 +140,16 @@ class TicketTimer extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
$this->activeTimer = $this->activeTimerQuery($user)->first();
|
||||
$this->activeTimer = $this->activeUserTimerQuery($user)->first();
|
||||
$this->elapsedSeconds = $this->activeTimer instanceof TicketTimeLog
|
||||
? $this->activeTimer->started_at->diffInSeconds(Carbon::now())
|
||||
? (int) $this->activeTimer->started_at->diffInSeconds(Carbon::now())
|
||||
: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the active timer query for the authenticated user and current ticket.
|
||||
*/
|
||||
private function activeTimerQuery(Authenticatable $user): Builder
|
||||
private function activeUserTimerQuery(Authenticatable $user): Builder
|
||||
{
|
||||
return TicketTimeLog::query()
|
||||
->where('ticket_id', $this->ticket->getKey())
|
||||
@@ -153,28 +158,34 @@ class TicketTimer extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for any active timer for this user across all tickets.
|
||||
* Build the active timer query for the authenticated user across all tickets.
|
||||
*/
|
||||
private function userHasAnyActiveTimer(Authenticatable $user): bool
|
||||
private function activeAnyTicketTimerQuery(Authenticatable $user): Builder
|
||||
{
|
||||
return TicketTimeLog::query()
|
||||
->where('user_id', $user->getAuthIdentifier())
|
||||
->whereNull('ended_at')
|
||||
->exists();
|
||||
->whereNull('ended_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the ticket's denormalised total without requiring a Ticket::timeLogs() relationship.
|
||||
* Update the ticket's rounded minute total using stored seconds.
|
||||
*/
|
||||
private function refreshTicketTimeSpentMinutes(): void
|
||||
private function updateTicketTimeSpentFromSeconds(): void
|
||||
{
|
||||
$totalMinutes = (int) TicketTimeLog::query()
|
||||
$totalSeconds = (int) TicketTimeLog::query()
|
||||
->where('ticket_id', $this->ticket->getKey())
|
||||
->whereNotNull('ended_at')
|
||||
->sum('minutes');
|
||||
->sum('seconds');
|
||||
|
||||
if ($totalSeconds === 0) {
|
||||
$totalSeconds = (int) TicketTimeLog::query()
|
||||
->where('ticket_id', $this->ticket->getKey())
|
||||
->whereNotNull('ended_at')
|
||||
->sum('minutes') * 60;
|
||||
}
|
||||
|
||||
$this->ticket->forceFill([
|
||||
'time_spent_minutes' => $totalMinutes,
|
||||
'time_spent_minutes' => (int) ceil($totalSeconds / 60),
|
||||
])->save();
|
||||
}
|
||||
|
||||
@@ -189,7 +200,7 @@ class TicketTimer extends Component
|
||||
|
||||
if (! $user instanceof Authenticatable) {
|
||||
throw ValidationException::withMessages([
|
||||
'timer' => 'You must be logged in to use the timer.',
|
||||
'timer' => 'You must be signed in to use the ticket timer.',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -197,22 +208,12 @@ class TicketTimer extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the description to store against the timer.
|
||||
* Return a safe note for the timer.
|
||||
*/
|
||||
private function normalisedDescription(): string
|
||||
{
|
||||
$description = trim($this->description);
|
||||
|
||||
return $description !== '' ? $description : 'Timer work log';
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect PostgreSQL/MySQL unique constraint collisions for active timers.
|
||||
*/
|
||||
private function isUniqueTimerConstraintViolation(QueryException $exception): bool
|
||||
{
|
||||
$sqlState = (string) ($exception->errorInfo[0] ?? '');
|
||||
|
||||
return in_array($sqlState, ['23000', '23505'], true);
|
||||
return $description !== '' ? $description : 'Work timer';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user