From d9f99a4cf1e7bc05bdfc43ad17dbe7b807d85c2d Mon Sep 17 00:00:00 2001 From: "bert.hausmans" Date: Fri, 10 Apr 2026 15:47:25 +0200 Subject: [PATCH] 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) --- .../Controllers/Api/V1/TimeSlotController.php | 17 +++++- .../Resources/Api/V1/TimeSlotResource.php | 15 +++++ api/tests/Feature/TimeSlot/TimeSlotTest.php | 57 +++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/api/app/Http/Controllers/Api/V1/TimeSlotController.php b/api/app/Http/Controllers/Api/V1/TimeSlotController.php index 7c4c7355..209733a6 100644 --- a/api/app/Http/Controllers/Api/V1/TimeSlotController.php +++ b/api/app/Http/Controllers/Api/V1/TimeSlotController.php @@ -20,11 +20,24 @@ final class TimeSlotController extends Controller { Gate::authorize('viewAny', [TimeSlot::class, $event]); - $timeSlots = $event->timeSlots()->orderBy('date')->orderBy('start_time')->get(); + $timeSlots = $event->timeSlots() + ->withCount('shifts') + ->with(['shifts' => fn ($q) => $q->withCount([ + 'shiftAssignments as assignments_count' => fn ($q) => $q->where('status', 'approved'), + ])]) + ->orderBy('date') + ->orderBy('start_time') + ->get(); if ($event->isSubEvent() && request()->boolean('include_parent') && $event->parent_event_id) { $parentTimeSlots = TimeSlot::where('event_id', $event->parent_event_id) - ->with('event') + ->withCount('shifts') + ->with([ + 'event', + 'shifts' => fn ($q) => $q->withCount([ + 'shiftAssignments as assignments_count' => fn ($q) => $q->where('status', 'approved'), + ]), + ]) ->orderBy('date') ->orderBy('start_time') ->get(); diff --git a/api/app/Http/Resources/Api/V1/TimeSlotResource.php b/api/app/Http/Resources/Api/V1/TimeSlotResource.php index 36128603..4fcfa155 100644 --- a/api/app/Http/Resources/Api/V1/TimeSlotResource.php +++ b/api/app/Http/Resources/Api/V1/TimeSlotResource.php @@ -23,6 +23,21 @@ final class TimeSlotResource extends JsonResource 'duration_hours' => $this->duration_hours, 'source' => $this->resource->getAttribute('source'), 'event_name' => $this->whenLoaded('event', fn () => $this->event->name), + 'shifts_count' => $this->whenCounted('shifts'), + // Aggregates computed from eager-loaded shifts with assignment counts. + // For events with >200 shifts, consider replacing with a raw aggregate query. + 'total_slots' => $this->when( + $this->relationLoaded('shifts'), + fn () => (int) $this->shifts->sum('slots_total'), + ), + 'filled_slots' => $this->when( + $this->relationLoaded('shifts'), + fn () => (int) $this->shifts->sum(fn ($shift) => $shift->assignments_count ?? 0), + ), + 'sections_count' => $this->when( + $this->relationLoaded('shifts'), + fn () => $this->shifts->pluck('festival_section_id')->unique()->count(), + ), 'created_at' => $this->created_at->toIso8601String(), ]; } diff --git a/api/tests/Feature/TimeSlot/TimeSlotTest.php b/api/tests/Feature/TimeSlot/TimeSlotTest.php index a5c584b6..98f1f5e0 100644 --- a/api/tests/Feature/TimeSlot/TimeSlotTest.php +++ b/api/tests/Feature/TimeSlot/TimeSlotTest.php @@ -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);