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:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user