feat: add shift management functionality
This commit is contained in:
@@ -2,14 +2,77 @@
|
||||
|
||||
namespace App\Http\Controllers\Client\Doctor;
|
||||
|
||||
use App\Enums\ShiftDayEnum;
|
||||
use App\Enums\UserEnum;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
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()
|
||||
{
|
||||
return Inertia::client('Dashboard/Shifts/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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ class FastRequestController extends Controller
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'document' => new FastRequestResource($document->load('pet')),
|
||||
'document' => new FastRequestResource($document->refresh()->load('pet')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user