feat: shift assignment workflow with claim, approve, reject, cancel, and bulk approve
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>
This commit is contained in:
101
api/app/Http/Controllers/Api/V1/ShiftAssignmentController.php
Normal file
101
api/app/Http/Controllers/Api/V1/ShiftAssignmentController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -12,21 +13,26 @@ 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\Models\ShiftAssignment;
|
||||
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', 'approved')])
|
||||
->withCount(['shiftAssignments as filled_slots' => fn ($q) => $q->where('status', ShiftAssignmentStatus::APPROVED)])
|
||||
->get();
|
||||
|
||||
return ShiftResource::collection($shifts);
|
||||
@@ -65,41 +71,8 @@ final class ShiftController extends Controller
|
||||
{
|
||||
Gate::authorize('assign', [$shift, $event, $section]);
|
||||
|
||||
$personId = $request->validated('person_id');
|
||||
|
||||
// Check if shift is full
|
||||
$approvedCount = $shift->shiftAssignments()->where('status', 'approved')->count();
|
||||
if ($approvedCount >= $shift->slots_total) {
|
||||
return $this->error('Shift is vol — alle slots zijn bezet.', 422);
|
||||
}
|
||||
|
||||
// Check overlap conflict if allow_overlap is false
|
||||
if (! $shift->allow_overlap) {
|
||||
$conflict = ShiftAssignment::where('person_id', $personId)
|
||||
->where('time_slot_id', $shift->time_slot_id)
|
||||
->whereNotIn('status', ['rejected', 'cancelled'])
|
||||
->exists();
|
||||
|
||||
if ($conflict) {
|
||||
return $this->error('Deze persoon is al ingepland voor dit tijdslot.', 422);
|
||||
}
|
||||
}
|
||||
|
||||
$assignment = $shift->shiftAssignments()->create([
|
||||
'person_id' => $personId,
|
||||
'time_slot_id' => $shift->time_slot_id,
|
||||
'status' => 'approved',
|
||||
'auto_approved' => false,
|
||||
'assigned_by' => $request->user()->id,
|
||||
'assigned_at' => now(),
|
||||
'approved_at' => now(),
|
||||
]);
|
||||
|
||||
// Update shift status if full
|
||||
$newApprovedCount = $shift->shiftAssignments()->where('status', 'approved')->count();
|
||||
if ($newApprovedCount >= $shift->slots_total) {
|
||||
$shift->update(['status' => 'full']);
|
||||
}
|
||||
$person = Person::findOrFail($request->validated('person_id'));
|
||||
$assignment = $this->shiftAssignmentService->assign($shift, $person, $request->user());
|
||||
|
||||
return $this->created(new ShiftAssignmentResource($assignment));
|
||||
}
|
||||
@@ -108,39 +81,8 @@ final class ShiftController extends Controller
|
||||
{
|
||||
Gate::authorize('claim', [$shift, $event, $section]);
|
||||
|
||||
$personId = $request->validated('person_id');
|
||||
|
||||
// Check claiming slots available
|
||||
$claimedCount = $shift->shiftAssignments()
|
||||
->whereNotIn('status', ['rejected', 'cancelled'])
|
||||
->count();
|
||||
|
||||
if ($shift->slots_open_for_claiming <= 0 || $claimedCount >= $shift->slots_open_for_claiming) {
|
||||
return $this->error('Geen claimbare slots beschikbaar voor deze shift.', 422);
|
||||
}
|
||||
|
||||
// Check overlap conflict if allow_overlap is false
|
||||
if (! $shift->allow_overlap) {
|
||||
$conflict = ShiftAssignment::where('person_id', $personId)
|
||||
->where('time_slot_id', $shift->time_slot_id)
|
||||
->whereNotIn('status', ['rejected', 'cancelled'])
|
||||
->exists();
|
||||
|
||||
if ($conflict) {
|
||||
return $this->error('Deze persoon is al ingepland voor dit tijdslot.', 422);
|
||||
}
|
||||
}
|
||||
|
||||
$autoApprove = $section->crew_auto_accepts;
|
||||
|
||||
$assignment = $shift->shiftAssignments()->create([
|
||||
'person_id' => $personId,
|
||||
'time_slot_id' => $shift->time_slot_id,
|
||||
'status' => $autoApprove ? 'approved' : 'pending_approval',
|
||||
'auto_approved' => $autoApprove,
|
||||
'assigned_at' => now(),
|
||||
'approved_at' => $autoApprove ? now() : null,
|
||||
]);
|
||||
$person = Person::findOrFail($request->validated('person_id'));
|
||||
$assignment = $this->shiftAssignmentService->claim($shift, $person);
|
||||
|
||||
return $this->created(new ShiftAssignmentResource($assignment));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\SyncVolunteerAvailabilityRequest;
|
||||
use App\Models\Event;
|
||||
use App\Models\Person;
|
||||
use App\Models\TimeSlot;
|
||||
use App\Models\VolunteerAvailability;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class VolunteerAvailabilityController extends Controller
|
||||
{
|
||||
public function index(Event $event, Person $person): JsonResponse
|
||||
{
|
||||
Gate::authorize('view', [$person, $event]);
|
||||
|
||||
$availabilities = VolunteerAvailability::where('person_id', $person->id)
|
||||
->with('timeSlot')
|
||||
->get()
|
||||
->map(fn (VolunteerAvailability $a) => [
|
||||
'id' => $a->id,
|
||||
'time_slot_id' => $a->time_slot_id,
|
||||
'preference_level' => $a->preference_level,
|
||||
'submitted_at' => $a->submitted_at?->toIso8601String(),
|
||||
'time_slot' => $a->timeSlot ? [
|
||||
'id' => $a->timeSlot->id,
|
||||
'name' => $a->timeSlot->name,
|
||||
'date' => $a->timeSlot->date?->toDateString(),
|
||||
'start_time' => $a->timeSlot->start_time,
|
||||
'end_time' => $a->timeSlot->end_time,
|
||||
] : null,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $availabilities]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function sync(SyncVolunteerAvailabilityRequest $request, Event $event, Person $person): JsonResponse
|
||||
{
|
||||
Gate::authorize('update', [$person, $event]);
|
||||
|
||||
$availabilities = $request->validated('availabilities');
|
||||
|
||||
// Validate all time_slot_ids belong to the event (or parent festival)
|
||||
$validTimeSlotIds = $event->getAllRelevantTimeSlots()->pluck('id')->toArray();
|
||||
|
||||
$invalidSlots = collect($availabilities)
|
||||
->pluck('time_slot_id')
|
||||
->diff($validTimeSlotIds);
|
||||
|
||||
if ($invalidSlots->isNotEmpty()) {
|
||||
throw ValidationException::withMessages([
|
||||
'availabilities' => ['Een of meer tijdsloten behoren niet tot dit evenement.'],
|
||||
]);
|
||||
}
|
||||
|
||||
// Validate time slots have person_type matching the person's crowd_type system_type
|
||||
$personSystemType = $person->crowdType?->system_type;
|
||||
if ($personSystemType !== null) {
|
||||
$requestedSlotIds = collect($availabilities)->pluck('time_slot_id')->toArray();
|
||||
$mismatchedSlots = TimeSlot::whereIn('id', $requestedSlotIds)
|
||||
->where('person_type', '!=', $personSystemType)
|
||||
->exists();
|
||||
|
||||
if ($mismatchedSlots) {
|
||||
throw ValidationException::withMessages([
|
||||
'availabilities' => ['Een of meer tijdsloten komen niet overeen met het type van deze persoon.'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete existing availabilities for this person
|
||||
VolunteerAvailability::where('person_id', $person->id)->delete();
|
||||
|
||||
// Create new availabilities
|
||||
$now = now();
|
||||
foreach ($availabilities as $item) {
|
||||
VolunteerAvailability::create([
|
||||
'person_id' => $person->id,
|
||||
'time_slot_id' => $item['time_slot_id'],
|
||||
'preference_level' => $item['preference_level'] ?? 3,
|
||||
'submitted_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
activity('volunteer_availability')
|
||||
->causedBy($request->user())
|
||||
->performedOn($person)
|
||||
->withProperties([
|
||||
'count' => count($availabilities),
|
||||
'time_slot_ids' => collect($availabilities)->pluck('time_slot_id')->toArray(),
|
||||
])
|
||||
->log('volunteer_availability.synced');
|
||||
|
||||
return $this->success(null, 'Beschikbaarheid opgeslagen.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user