79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Client\Doctor;
|
|
|
|
use App\Enums\ShiftDayEnum;
|
|
use App\Enums\UserEnum;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Client\ShiftResource;
|
|
use App\Models\Shift;
|
|
use App\Services\Shift\ShiftScheduleService;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Inertia\Inertia;
|
|
|
|
class ShiftController extends Controller
|
|
{
|
|
protected ShiftScheduleService $shiftScheduleService;
|
|
|
|
public function __construct(ShiftScheduleService $shiftScheduleService)
|
|
{
|
|
$this->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();
|
|
}
|
|
}
|