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>
102 lines
3.6 KiB
PHP
102 lines
3.6 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\BulkApproveShiftAssignmentRequest;
|
|
use App\Http\Requests\Api\V1\RejectShiftAssignmentRequest;
|
|
use App\Http\Resources\Api\V1\ShiftAssignmentResource;
|
|
use App\Models\Event;
|
|
use App\Models\ShiftAssignment;
|
|
use App\Services\ShiftAssignmentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
final class ShiftAssignmentController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ShiftAssignmentService $service,
|
|
) {}
|
|
|
|
public function index(Request $request, Event $event): AnonymousResourceCollection
|
|
{
|
|
Gate::authorize('viewAny', [ShiftAssignment::class, $event]);
|
|
|
|
$query = ShiftAssignment::query()
|
|
->whereHas('shift.festivalSection', fn ($q) => $q->where('event_id', $event->id))
|
|
->with(['person', 'shift.festivalSection', 'shift.timeSlot']);
|
|
|
|
if ($request->filled('status')) {
|
|
$status = ShiftAssignmentStatus::tryFrom($request->string('status')->toString());
|
|
if ($status !== null) {
|
|
$query->where('status', $status);
|
|
}
|
|
}
|
|
|
|
if ($request->filled('shift_id')) {
|
|
$query->where('shift_id', $request->string('shift_id')->toString());
|
|
}
|
|
|
|
if ($request->filled('person_id')) {
|
|
$query->where('person_id', $request->string('person_id')->toString());
|
|
}
|
|
|
|
if ($request->filled('section_id')) {
|
|
$query->whereHas('shift', fn ($q) => $q->where('festival_section_id', $request->string('section_id')->toString()));
|
|
}
|
|
|
|
$assignments = $query->orderByDesc('created_at')->paginate(50);
|
|
|
|
return ShiftAssignmentResource::collection($assignments);
|
|
}
|
|
|
|
public function approve(Event $event, ShiftAssignment $shiftAssignment): JsonResponse
|
|
{
|
|
Gate::authorize('approve', [$shiftAssignment, $event]);
|
|
|
|
$shiftAssignment = $this->service->approve($shiftAssignment, request()->user());
|
|
|
|
return $this->success(new ShiftAssignmentResource($shiftAssignment->load(['person', 'shift.festivalSection', 'shift.timeSlot'])));
|
|
}
|
|
|
|
public function reject(RejectShiftAssignmentRequest $request, Event $event, ShiftAssignment $shiftAssignment): JsonResponse
|
|
{
|
|
Gate::authorize('reject', [$shiftAssignment, $event]);
|
|
|
|
$shiftAssignment = $this->service->reject(
|
|
$shiftAssignment,
|
|
$request->user(),
|
|
$request->validated('reason'),
|
|
);
|
|
|
|
return $this->success(new ShiftAssignmentResource($shiftAssignment->load(['person', 'shift.festivalSection', 'shift.timeSlot'])));
|
|
}
|
|
|
|
public function cancel(Event $event, ShiftAssignment $shiftAssignment): JsonResponse
|
|
{
|
|
Gate::authorize('cancel', [$shiftAssignment, $event]);
|
|
|
|
$shiftAssignment = $this->service->cancel($shiftAssignment, request()->user());
|
|
|
|
return $this->success(new ShiftAssignmentResource($shiftAssignment->load(['person', 'shift.festivalSection', 'shift.timeSlot'])));
|
|
}
|
|
|
|
public function bulkApprove(BulkApproveShiftAssignmentRequest $request, Event $event): JsonResponse
|
|
{
|
|
Gate::authorize('bulkApprove', [ShiftAssignment::class, $event]);
|
|
|
|
$assignments = ShiftAssignment::whereIn('id', $request->validated('assignment_ids'))
|
|
->with('shift')
|
|
->get();
|
|
|
|
$results = $this->service->bulkApprove($assignments, $request->user());
|
|
|
|
return $this->success($results);
|
|
}
|
|
}
|