shiftScheduleService = $shiftScheduleService; } public function index() { $this->ensureDoctor(); $myShifts = Shift::where('user_id', Auth::id())->get(); $openShifts = Shift::active() ->open() ->where(function ($query) { $query->whereNull('date') ->orWhereDate('date', '>=', now()->toDateString()); }) ->get(); return Inertia::client('Dashboard/Shifts/Index', [ 'myShifts' => ShiftResource::collection($this->sortSchedule($myShifts)), 'openShifts' => ShiftResource::collection($this->sortSchedule($openShifts)), ]); } public function update(Shift $shift) { $this->ensureDoctor(); $this->shiftScheduleService->claim($shift->id, Auth::user()); return response(null, 200); } public function destroy(Shift $shift) { $this->ensureDoctor(); $this->shiftScheduleService->release($shift, Auth::user()); return response(null, 200); } protected function ensureDoctor(): void { abort_unless(Auth::user()->level === UserEnum::Doctor->value, 403); } protected function sortSchedule($shifts) { $dayOrder = array_flip(ShiftDayEnum::values()); return $shifts ->sortBy([ fn ($a, $b) => strcmp($a->date?->toDateString() ?? '', $b->date?->toDateString() ?? ''), fn ($a, $b) => ($dayOrder[$a->day_of_week] ?? 0) <=> ($dayOrder[$b->day_of_week] ?? 0), fn ($a, $b) => strcmp($a->start_time, $b->start_time), ]) ->values(); } }