Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/Http/Controllers/VolunteerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public function checkOut(Request $request, TimeEntry $timeEntry): JsonResponse|R
*/
public function storeTimeEntry(TimeEntryStoreRequest $request, Event $event, User $user): JsonResponse {
// Don't allow an ongoing entry to be created if there already is one
$input = $request->safe();
if (!isset($input['stop'])) {
if (!$request->filled('stop')) {
if ($user->timeEntries()->ongoing()->forEvent($event)->exists()) {
return response()->json(['error' => 'User already has an ongoing time entry.'], 409);
}
Expand Down
9 changes: 7 additions & 2 deletions app/Http/Requests/TimeEntryStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ public function rules(): array {
*/
public function passedValidation(): void {
$this->merge([
'start' => $this->date('start')?->timezone(config('app.timezone')) ?? now(),
'stop' => $this->date('stop')?->timezone(config('app.timezone')),
'start' => ($this->date('start')?->timezone(config('app.timezone')) ?? now())->toISOString(true),
'stop' => $this->date('stop')?->timezone(config('app.timezone'))?->toISOString(true),
]);

$this->validator->setData(array_merge($this->validator->getData(), [
'start' => $this->input('start'),
'stop' => $this->input('stop'),
]));
}
}
59 changes: 59 additions & 0 deletions tests/Feature/VolunteerTimeEntryStoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use App\Models\Department;
use App\Models\Event;
use App\Models\Role;
use App\Models\TimeEntry;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('rejects creating a second ongoing time entry for the same user and event', function () {
$manager = User::factory()->create(['role' => Role::Admin]);
$volunteer = User::factory()->create();
$event = Event::factory()->create();
$department = Department::factory()->create(['event_id' => $event->id]);

$this->actingAs($manager);

// Mirrors the real clock-in UI, which always sends an explicit 'start' and omits 'stop'.
$this->putJson(route('volunteer.time.store', [$event, $volunteer]), [
'department_id' => $department->id,
'start' => now()->subSecond()->toISOString(),
])->assertOk();

expect(TimeEntry::where('user_id', $volunteer->id)->whereNull('stop')->count())->toBe(1);

$this->putJson(route('volunteer.time.store', [$event, $volunteer]), [
'department_id' => $department->id,
'start' => now()->subSecond()->toISOString(),
])
->assertStatus(409)
->assertJson(['error' => 'User already has an ongoing time entry.']);

expect(TimeEntry::where('user_id', $volunteer->id)->whereNull('stop')->count())->toBe(1);
});

it('allows creating a time entry with an explicit stop while another is ongoing', function () {
$manager = User::factory()->create(['role' => Role::Admin]);
$volunteer = User::factory()->create();
$event = Event::factory()->create();
$department = Department::factory()->create(['event_id' => $event->id]);

$this->actingAs($manager);

$this->putJson(route('volunteer.time.store', [$event, $volunteer]), [
'department_id' => $department->id,
'start' => now()->subSecond()->toISOString(),
])->assertOk();

$this->putJson(route('volunteer.time.store', [$event, $volunteer]), [
'department_id' => $department->id,
'start' => now()->subHour()->toISOString(),
'stop' => now()->toISOString(),
])->assertOk();

expect(TimeEntry::where('user_id', $volunteer->id)->count())->toBe(2);
expect(TimeEntry::where('user_id', $volunteer->id)->whereNull('stop')->count())->toBe(1);
});
Loading