phases one of time logged

This commit is contained in:
Mark Gregory
2026-06-22 12:05:18 +01:00
parent 6457e4b498
commit e1772367cf
14 changed files with 604 additions and 5 deletions
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Helpdesk\Models\Ticket;
final class TicketTimeLogController extends Controller
{
/**
* Store a time entry against the selected helpdesk ticket.
*/
public function store(Request $request, Ticket $ticket): RedirectResponse
{
$this->authorize('update', $ticket);
$validated = $request->validate([
'minutes' => ['required', 'integer', 'min:1', 'max:1440'],
'description' => ['nullable', 'string', 'max:1000'],
]);
$ticket->timeLogs()->create([
'user_id' => $request->user()?->id,
'minutes' => (int) $validated['minutes'],
'description' => $validated['description'] ?? null,
'logged_at' => now(),
]);
return back()->with('ticket-action-updated', 'Time logged successfully.');
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (Schema::hasTable('helpdesk_time_logs')) {
return;
}
Schema::create('helpdesk_time_logs', function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->unsignedInteger('minutes');
$table->text('description')->nullable();
$table->timestamp('logged_at')->useCurrent();
$table->timestamps();
$table->index(['ticket_id', 'logged_at']);
$table->index(['user_id', 'logged_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('helpdesk_time_logs');
}
};
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Helpdesk\Models\Ticket;
final class TicketTimeLogController extends Controller
{
/**
* Store a time entry against the selected helpdesk ticket.
*/
public function store(Request $request, Ticket $ticket): RedirectResponse
{
$this->authorize('update', $ticket);
$validated = $request->validate([
'minutes' => ['required', 'integer', 'min:1', 'max:1440'],
'description' => ['nullable', 'string', 'max:1000'],
]);
$ticket->timeLogs()->create([
'user_id' => $request->user()?->id,
'minutes' => (int) $validated['minutes'],
'description' => $validated['description'] ?? null,
'logged_at' => now(),
]);
return back()->with('ticket-action-updated', 'Time logged successfully.');
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
final class TimeLog extends Model
{
protected $table = 'helpdesk_time_logs';
/** @var list<string> */
protected $fillable = [
'ticket_id',
'user_id',
'minutes',
'description',
'logged_at',
];
/** @var array<string, string> */
protected $casts = [
'minutes' => 'integer',
'logged_at' => 'datetime',
];
/** @return BelongsTo<Ticket, TimeLog> */
public function ticket(): BelongsTo
{
return $this->belongsTo(Ticket::class, 'ticket_id');
}
/** @return BelongsTo<User, TimeLog> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\Helpdesk\Models\Ticket;
use Tests\TestCase;
final class TicketTimeLogTest extends TestCase
{
use RefreshDatabase;
/**
* It stores a time log for an authorized ticket user.
*/
public function test_authorized_user_can_log_ticket_time(): void
{
$user = User::factory()->create();
$ticket = Ticket::factory()->create();
$this->actingAs($user)
->post(route('helpdesk.tickets.time-logs.store', $ticket), [
'minutes' => 45,
'description' => 'Investigated the issue and updated the ticket.',
])
->assertRedirect();
$this->assertDatabaseHas('helpdesk_time_logs', [
'ticket_id' => $ticket->id,
'user_id' => $user->id,
'minutes' => 45,
'description' => 'Investigated the issue and updated the ticket.',
]);
}
/**
* It rejects invalid logged minutes.
*/
public function test_ticket_time_log_requires_valid_minutes(): void
{
$user = User::factory()->create();
$ticket = Ticket::factory()->create();
$this->actingAs($user)
->from(route('helpdesk.tickets.show', $ticket))
->post(route('helpdesk.tickets.time-logs.store', $ticket), [
'minutes' => 0,
])
->assertRedirect(route('helpdesk.tickets.show', $ticket))
->assertSessionHasErrors('minutes');
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (Schema::hasTable('helpdesk_time_logs')) {
return;
}
Schema::create('helpdesk_time_logs', function (Blueprint $table): void {
$table->id();
$table->foreignId('ticket_id')->constrained('helpdesk_tickets')->cascadeOnDelete();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->unsignedInteger('minutes');
$table->text('description')->nullable();
$table->timestamp('logged_at')->useCurrent();
$table->timestamps();
$table->index(['ticket_id', 'logged_at']);
$table->index(['user_id', 'logged_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('helpdesk_time_logs');
}
};
@@ -0,0 +1,42 @@
<form method="POST" action="{{ route('helpdesk.tickets.time-logs.store', $ticket) }}" class="mb-4 space-y-2">
@csrf
<label class="block text-xs font-bold text-zinc-700 dark:text-zinc-200" for="time_minutes">
Log time
</label>
<div class="grid grid-cols-[1fr_auto] gap-2">
<input
id="time_minutes"
name="minutes"
type="number"
min="1"
max="1440"
value="{{ old('minutes') }}"
placeholder="Minutes"
class="block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
data-dusk="ticket-time-minutes"
>
<button type="submit" dusk="log-ticket-time" data-dusk="log-ticket-time" class="inline-flex items-center justify-center rounded-lg bg-emerald-600 px-3 py-2 text-xs font-black text-white shadow-sm transition hover:bg-emerald-700 focus:outline-none focus:ring-2 focus:ring-emerald-400/70">
<i class="fa-solid fa-clock mr-2" aria-hidden="true"></i>
<span>Log</span>
</button>
</div>
<textarea
name="description"
rows="2"
placeholder="Optional work note"
class="block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
data-dusk="ticket-time-description"
>{{ old('description') }}</textarea>
@error('minutes')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
@error('description')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
</form>
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Route;
use Modules\Helpdesk\Http\Controllers\TicketTimeLogController;
Route::post('/tickets/{ticket}/time-logs', [TicketTimeLogController::class, 'store'])
->name('tickets.time-logs.store');
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Helpdesk\Models\Ticket;
final class TicketTimeLogController extends Controller
{
/**
* Store a time entry against the selected helpdesk ticket.
*/
public function store(Request $request, Ticket $ticket): RedirectResponse
{
$this->authorize('update', $ticket);
$validated = $request->validate([
'minutes' => ['required', 'integer', 'min:1', 'max:1440'],
'description' => ['nullable', 'string', 'max:1000'],
]);
$ticket->timeLogs()->create([
'user_id' => $request->user()?->id,
'minutes' => (int) $validated['minutes'],
'description' => $validated['description'] ?? null,
'logged_at' => now(),
]);
return back()->with('ticket-action-updated', 'Time logged successfully.');
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
final class TimeLog extends Model
{
protected $table = 'helpdesk_time_logs';
/** @var list<string> */
protected $fillable = [
'ticket_id',
'user_id',
'minutes',
'description',
'logged_at',
];
/** @var array<string, string> */
protected $casts = [
'minutes' => 'integer',
'logged_at' => 'datetime',
];
/** @return BelongsTo<Ticket, TimeLog> */
public function ticket(): BelongsTo
{
return $this->belongsTo(Ticket::class, 'ticket_id');
}
/** @return BelongsTo<User, TimeLog> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}
@@ -32,12 +32,136 @@
<i class="fa-solid fa-arrow-left mr-2" aria-hidden="true"></i>
Back
</a>
@can('update', $ticket)
<a href="{{ route('helpdesk.tickets.edit', $ticket) }}" wire:navigate class="inline-flex items-center rounded-lg bg-emerald-600 px-3 py-2 text-xs font-bold text-white shadow-sm hover:bg-emerald-700">
<i class="fa-solid fa-pen-to-square mr-2" aria-hidden="true"></i>
Edit
</a>
<div class="rounded-xl border border-zinc-200 bg-white p-4 shadow-sm dark:border-zinc-700 dark:bg-zinc-900">
<div class="-mx-4 -mt-4 mb-4 rounded-t-xl bg-indigo-700 px-4 py-4 text-white">
<div class="flex items-center gap-3">
<span class="inline-flex h-12 w-12 shrink-0 items-center justify-center rounded-xl bg-indigo-950 text-white ring-1 ring-indigo-300/60 shadow-lg">
<i class="fa-solid fa-sliders text-lg" aria-hidden="true"></i>
</span>
<div class="min-w-0">
<h2 class="text-lg font-black uppercase tracking-wide">Ticket controls</h2>
<p class="text-xs font-semibold text-indigo-100">Log time, assignment, status and priority</p>
</div>
</div>
</div>
@if (session('ticket-action-updated'))
<div class="mb-4 rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-xs font-bold text-emerald-800 dark:border-emerald-800 dark:bg-emerald-950 dark:text-emerald-100">
{{ session('ticket-action-updated') }}
</div>
@endif
<form method="POST" action="{{ route('helpdesk.tickets.time-logs.store', $ticket) }}" class="mb-4 space-y-2">
@csrf
<label class="block text-xs font-bold text-zinc-700 dark:text-zinc-200" for="ticket_time_minutes">
Log time
</label>
<div class="grid grid-cols-[1fr_auto] gap-2">
<input
id="ticket_time_minutes"
name="minutes"
type="number"
min="1"
max="1440"
placeholder="Minutes"
class="block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
data-dusk="ticket-time-minutes"
>
<button
type="submit"
dusk="log-ticket-time"
data-dusk="log-ticket-time"
class="inline-flex items-center justify-center rounded-lg bg-emerald-600 px-3 py-2 text-xs font-black text-white shadow-sm transition hover:bg-emerald-700 focus:outline-none focus:ring-2 focus:ring-emerald-400/70"
>
<i class="fa-solid fa-clock mr-2" aria-hidden="true"></i>
<span>Log</span>
</button>
</div>
<textarea
name="description"
rows="2"
placeholder="Optional work note"
class="block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
data-dusk="ticket-time-description"
></textarea>
@error('minutes')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
@error('description')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
</form>
<form method="POST" action="{{ route('helpdesk.tickets.agent.update', $ticket) }}" class="mb-4 space-y-2">
@csrf
<label class="block text-xs font-bold text-zinc-700 dark:text-zinc-200" for="show_agent_id">Assigned agent</label>
<select id="show_agent_id" name="agent_id" class="hd-select block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100" data-dusk="ticket-show-agent">
<option value="" @selected($agent_id === null)>Unassigned</option>
@forelse ($agents as $agent)
<option value="{{ $agent->id }}" @selected((int) $agent_id === (int) $agent->id)>{{ $agent->name }} · {{ $agent->email }}</option>
@empty
<option value="" disabled>No users available for assignment</option>
@endforelse
</select>
@error('agent_id')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<button type="submit" dusk="update-agent" data-dusk="update-agent" class="inline-flex items-center justify-center rounded-lg bg-emerald-600 px-3 py-2 text-xs font-black text-white shadow-sm transition hover:bg-emerald-700 focus:outline-none focus:ring-2 focus:ring-emerald-400/70">
<i class="fa-solid fa-user-check mr-2" aria-hidden="true"></i>
<span>Assign agent</span>
</button>
</form>
<form method="POST" action="{{ route('helpdesk.tickets.status.update', $ticket) }}" class="mb-4 space-y-2">
@csrf
<label class="block text-xs font-bold text-zinc-700 dark:text-zinc-200" for="show_status_id">Status</label>
<select id="show_status_id" name="status_id" class="hd-select block min-h-10 w-full rounded-lg border border-zinc-300 bg-white px-3 py-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100" data-dusk="ticket-show-status">
@foreach ($statuses as $status)
<option value="{{ $status->id }}" @selected((int) $status_id === (int) $status->id)>{{ $status->name }}</option>
@endforeach
</select>
@error('status_id')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<button type="submit" dusk="update-status" class="inline-flex items-center justify-center rounded-lg bg-blue-600 px-3 py-2 text-xs font-black text-white shadow-sm transition hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400/70">
<i class="fa-solid fa-circle-check mr-2" aria-hidden="true"></i>
<span>Update status</span>
</button>
</form>
<form method="POST" action="{{ route('helpdesk.tickets.priority.update', $ticket) }}" class="space-y-2">
@csrf
<label class="block text-xs font-bold text-zinc-700 dark:text-zinc-200" for="show_priority_id">Priority</label>
<select id="show_priority_id" name="priority_id" class="hd-select block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-amber-500 focus:ring-amber-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100" data-dusk="ticket-show-priority">
@foreach ($priorities as $priority)
<option value="{{ $priority->id }}" @selected((int) $priority_id === (int) $priority->id)>{{ $priority->name }}</option>
@endforeach
</select>
@error('priority_id')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<button type="submit" dusk="update-priority" class="inline-flex items-center justify-center rounded-lg bg-amber-500 px-3 py-2 text-xs font-black text-white shadow-sm transition hover:bg-amber-600 focus:outline-none focus:ring-2 focus:ring-amber-300/70">
<i class="fa-solid fa-flag mr-2" aria-hidden="true"></i>
<span>Update priority</span>
</button>
</form>
</div>
@endcan
</div>
</div>
@@ -0,0 +1,42 @@
<form method="POST" action="{{ route('helpdesk.tickets.time-logs.store', $ticket) }}" class="mb-4 space-y-2">
@csrf
<label class="block text-xs font-bold text-zinc-700 dark:text-zinc-200" for="time_minutes">
Log time
</label>
<div class="grid grid-cols-[1fr_auto] gap-2">
<input
id="time_minutes"
name="minutes"
type="number"
min="1"
max="1440"
value="{{ old('minutes') }}"
placeholder="Minutes"
class="block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
data-dusk="ticket-time-minutes"
>
<button type="submit" dusk="log-ticket-time" data-dusk="log-ticket-time" class="inline-flex items-center justify-center rounded-lg bg-emerald-600 px-3 py-2 text-xs font-black text-white shadow-sm transition hover:bg-emerald-700 focus:outline-none focus:ring-2 focus:ring-emerald-400/70">
<i class="fa-solid fa-clock mr-2" aria-hidden="true"></i>
<span>Log</span>
</button>
</div>
<textarea
name="description"
rows="2"
placeholder="Optional work note"
class="block w-full rounded-lg border border-zinc-300 bg-white p-2 text-xs font-semibold text-zinc-900 shadow-sm focus:border-emerald-500 focus:ring-emerald-500 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
data-dusk="ticket-time-description"
>{{ old('description') }}</textarea>
@error('minutes')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
@error('description')
<p class="text-xs font-bold text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
</form>
+3
View File
@@ -10,6 +10,7 @@ use Modules\Helpdesk\Http\Controllers\TicketCommentDeleteController;
use Modules\Helpdesk\Http\Controllers\TicketCommentStoreController;
use Modules\Helpdesk\Http\Controllers\TicketAttachmentStoreController;
use Modules\Helpdesk\Http\Controllers\TicketStoreController;
use Modules\Helpdesk\Http\Controllers\TicketTimeLogController;
use Modules\Helpdesk\Http\Controllers\WorkflowOpenController;
use Modules\Helpdesk\Livewire\KanbanBoard;
use Modules\Helpdesk\Livewire\SettingsManager;
@@ -39,3 +40,5 @@ Route::get('/tickets/{ticket}', TicketShow::class)->name('tickets.show');
Route::get('/attachments/{attachment}/download', AttachmentDownloadController::class)
->whereNumber('attachment')
->name('attachments.download');
Route::post('/tickets/{ticket}/time-logs', [TicketTimeLogController::class, 'store'])
->name('tickets.time-logs.store');
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\Helpdesk\Models\Ticket;
use Tests\TestCase;
final class TicketTimeLogTest extends TestCase
{
use RefreshDatabase;
/**
* It stores a time log for an authorized ticket user.
*/
public function test_authorized_user_can_log_ticket_time(): void
{
$user = User::factory()->create();
$ticket = Ticket::factory()->create();
$this->actingAs($user)
->post(route('helpdesk.tickets.time-logs.store', $ticket), [
'minutes' => 45,
'description' => 'Investigated the issue and updated the ticket.',
])
->assertRedirect();
$this->assertDatabaseHas('helpdesk_time_logs', [
'ticket_id' => $ticket->id,
'user_id' => $user->id,
'minutes' => 45,
'description' => 'Investigated the issue and updated the ticket.',
]);
}
/**
* It rejects invalid logged minutes.
*/
public function test_ticket_time_log_requires_valid_minutes(): void
{
$user = User::factory()->create();
$ticket = Ticket::factory()->create();
$this->actingAs($user)
->from(route('helpdesk.tickets.show', $ticket))
->post(route('helpdesk.tickets.time-logs.store', $ticket), [
'minutes' => 0,
])
->assertRedirect(route('helpdesk.tickets.show', $ticket))
->assertSessionHasErrors('minutes');
}
}