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:
@@ -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();
|
||||
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user