feat(api): enrich TimeSlotResource with shift statistics

Add shifts_count, total_slots, filled_slots, and sections_count computed
fields to TimeSlotResource. Update TimeSlotController to eager-load shifts
with assignment counts for aggregate calculations. Includes test verifying
only approved assignments count towards filled_slots.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 15:47:25 +02:00
parent 4949fe4cf5
commit d9f99a4cf1
3 changed files with 87 additions and 2 deletions

View File

@@ -5,7 +5,11 @@ declare(strict_types=1);
namespace Tests\Feature\TimeSlot;
use App\Models\Event;
use App\Models\FestivalSection;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\Shift;
use App\Models\ShiftAssignment;
use App\Models\TimeSlot;
use App\Models\User;
use Database\Seeders\RoleSeeder;
@@ -52,6 +56,59 @@ class TimeSlotTest extends TestCase
$this->assertCount(3, $response->json('data'));
}
public function test_index_includes_shift_statistics(): void
{
$timeSlot = TimeSlot::factory()->create(['event_id' => $this->event->id]);
$sectionA = FestivalSection::factory()->create(['event_id' => $this->event->id]);
$sectionB = FestivalSection::factory()->create(['event_id' => $this->event->id]);
$person = Person::factory()->create(['event_id' => $this->event->id]);
$shift1 = Shift::factory()->create([
'festival_section_id' => $sectionA->id,
'time_slot_id' => $timeSlot->id,
'slots_total' => 5,
]);
$shift2 = Shift::factory()->create([
'festival_section_id' => $sectionB->id,
'time_slot_id' => $timeSlot->id,
'slots_total' => 3,
]);
// Create 2 approved assignments on shift1
ShiftAssignment::factory()->approved()->create([
'shift_id' => $shift1->id,
'person_id' => $person->id,
'time_slot_id' => $timeSlot->id,
]);
ShiftAssignment::factory()->approved()->create([
'shift_id' => $shift1->id,
'person_id' => Person::factory()->create(['event_id' => $this->event->id])->id,
'time_slot_id' => $timeSlot->id,
]);
// Create 1 pending assignment on shift2 (should NOT count towards filled_slots)
ShiftAssignment::factory()->create([
'shift_id' => $shift2->id,
'person_id' => $person->id,
'time_slot_id' => $timeSlot->id,
'status' => 'pending_approval',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/events/{$this->event->id}/time-slots");
$response->assertOk();
$data = $response->json('data.0');
$this->assertEquals(2, $data['shifts_count']);
$this->assertEquals(8, $data['total_slots']);
$this->assertEquals(2, $data['filled_slots']);
$this->assertEquals(2, $data['sections_count']);
}
public function test_store_creates_time_slot(): void
{
Sanctum::actingAs($this->orgAdmin);