Implements the complete ShiftAssignment lifecycle: - ShiftAssignmentStatus enum with allowed transitions - ShiftAssignmentService with claim/assign/approve/reject/cancel/bulkApprove - ShiftAssignmentController with event-scoped endpoints - ShiftAssignmentPolicy (organizer + volunteer self-cancel) - VolunteerAvailability model, controller, and sync endpoint - Refactored ShiftController to delegate to service layer - 31 workflow tests covering all paths and multi-tenancy Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
90 lines
3.1 KiB
PHP
90 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Enums\ShiftAssignmentStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V1\AssignShiftRequest;
|
|
use App\Http\Requests\Api\V1\StoreShiftRequest;
|
|
use App\Http\Requests\Api\V1\UpdateShiftRequest;
|
|
use App\Http\Resources\Api\V1\ShiftAssignmentResource;
|
|
use App\Http\Resources\Api\V1\ShiftResource;
|
|
use App\Models\Event;
|
|
use App\Models\FestivalSection;
|
|
use App\Models\Person;
|
|
use App\Models\Shift;
|
|
use App\Services\ShiftAssignmentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
final class ShiftController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ShiftAssignmentService $shiftAssignmentService,
|
|
) {}
|
|
|
|
public function index(Event $event, FestivalSection $section): AnonymousResourceCollection
|
|
{
|
|
Gate::authorize('viewAny', [Shift::class, $event]);
|
|
|
|
$shifts = $section->shifts()
|
|
->with(['timeSlot', 'location'])
|
|
->withCount(['shiftAssignments as filled_slots' => fn ($q) => $q->where('status', ShiftAssignmentStatus::APPROVED)])
|
|
->get();
|
|
|
|
return ShiftResource::collection($shifts);
|
|
}
|
|
|
|
public function store(StoreShiftRequest $request, Event $event, FestivalSection $section): JsonResponse
|
|
{
|
|
Gate::authorize('create', [Shift::class, $event]);
|
|
|
|
$shift = $section->shifts()->create($request->validated());
|
|
$shift->load(['timeSlot', 'location']);
|
|
|
|
return $this->created(new ShiftResource($shift));
|
|
}
|
|
|
|
public function update(UpdateShiftRequest $request, Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('update', [$shift, $event, $section]);
|
|
|
|
$shift->update($request->validated());
|
|
$shift->load(['timeSlot', 'location']);
|
|
|
|
return $this->success(new ShiftResource($shift->fresh()->load(['timeSlot', 'location'])));
|
|
}
|
|
|
|
public function destroy(Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('delete', [$shift, $event, $section]);
|
|
|
|
$shift->delete();
|
|
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
public function assign(AssignShiftRequest $request, Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('assign', [$shift, $event, $section]);
|
|
|
|
$person = Person::findOrFail($request->validated('person_id'));
|
|
$assignment = $this->shiftAssignmentService->assign($shift, $person, $request->user());
|
|
|
|
return $this->created(new ShiftAssignmentResource($assignment));
|
|
}
|
|
|
|
public function claim(AssignShiftRequest $request, Event $event, FestivalSection $section, Shift $shift): JsonResponse
|
|
{
|
|
Gate::authorize('claim', [$shift, $event, $section]);
|
|
|
|
$person = Person::findOrFail($request->validated('person_id'));
|
|
$assignment = $this->shiftAssignmentService->claim($shift, $person);
|
|
|
|
return $this->created(new ShiftAssignmentResource($assignment));
|
|
}
|
|
}
|