feat: smart assign person dialog with conflict details and assignable-persons endpoint

Add GET /events/{event}/shifts/{shift}/assignable-persons endpoint that
returns approved persons with availability status, conflict details, and
already-assigned flags. Improve ShiftAssignmentService conflict errors to
include section name, time slot, and time range. Replace both assign
dialogs with a new AssignPersonDialog featuring search, crowd type
filtering, availability toggle, and inline conflict warnings.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 20:32:31 +02:00
parent c220446920
commit 968e17c6d6
10 changed files with 1872 additions and 13 deletions

View File

@@ -4,12 +4,15 @@ declare(strict_types=1);
namespace App\Http\Controllers\Api\V1;
use App\Enums\PersonStatus;
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\Person;
use App\Models\Shift;
use App\Models\ShiftAssignment;
use App\Services\ShiftAssignmentService;
use Illuminate\Http\JsonResponse;
@@ -98,4 +101,72 @@ final class ShiftAssignmentController extends Controller
return $this->success($results);
}
public function assignablePersons(Event $event, Shift $shift): JsonResponse
{
Gate::authorize('viewAny', [ShiftAssignment::class, $event]);
$festivalEventId = $event->parent_event_id ?? $event->id;
$timeSlotId = $shift->time_slot_id;
$shiftId = $shift->id;
// Get all conflict assignments for this time slot in one query
$conflicts = ShiftAssignment::where('time_slot_id', $timeSlotId)
->whereIn('status', [
ShiftAssignmentStatus::PENDING_APPROVAL,
ShiftAssignmentStatus::APPROVED,
])
->with(['shift.festivalSection', 'shift.timeSlot'])
->get()
->keyBy('person_id');
// Get all assignments for THIS shift in one query
$alreadyAssigned = ShiftAssignment::where('shift_id', $shiftId)
->whereNotIn('status', [
ShiftAssignmentStatus::REJECTED,
ShiftAssignmentStatus::CANCELLED,
])
->pluck('person_id')
->flip();
$persons = Person::where('event_id', $festivalEventId)
->where('status', PersonStatus::APPROVED)
->with('crowdType')
->orderBy('name')
->get()
->map(function (Person $person) use ($conflicts, $alreadyAssigned, $shiftId) {
$isAlreadyAssigned = $alreadyAssigned->has($person->id);
$conflict = $conflicts->get($person->id);
$hasConflict = $conflict && $conflict->shift_id !== $shiftId;
return [
'id' => $person->id,
'name' => $person->name,
'email' => $person->email,
'status' => $person->status,
'crowd_type' => $person->crowdType ? [
'id' => $person->crowdType->id,
'name' => $person->crowdType->name,
'system_type' => $person->crowdType->system_type,
] : null,
'is_available' => ! $hasConflict && ! $isAlreadyAssigned,
'already_assigned' => $isAlreadyAssigned,
'conflict' => $hasConflict ? [
'section_name' => $conflict->shift->festivalSection->name,
'shift_title' => $conflict->shift->title ?? $conflict->shift->festivalSection->name,
'time_slot_name' => $conflict->shift->timeSlot->name,
'time' => $conflict->shift->timeSlot->start_time
. '' . $conflict->shift->timeSlot->end_time,
] : null,
];
})
->sortBy([
['already_assigned', 'asc'],
['is_available', 'desc'],
['name', 'asc'],
])
->values();
return response()->json(['data' => $persons]);
}
}

View File

@@ -24,7 +24,7 @@ final class ShiftAssignmentService
$this->validateShiftIsOpen($shift);
$this->validatePersonApproved($person);
$this->validateClaimCapacity($shift);
$this->validateNoConflict($shift, $person);
$this->validateNoConflict($shift, $person, isClaim: true);
$autoApprove = $shift->festivalSection->crew_auto_accepts;
$status = $autoApprove
@@ -323,20 +323,30 @@ final class ShiftAssignmentService
/**
* @throws ValidationException
*/
private function validateNoConflict(Shift $shift, Person $person): void
private function validateNoConflict(Shift $shift, Person $person, bool $isClaim = false): void
{
if ($shift->allow_overlap) {
return;
}
$conflict = ShiftAssignment::where('person_id', $person->id)
$existingAssignment = ShiftAssignment::where('person_id', $person->id)
->where('time_slot_id', $shift->time_slot_id)
->active()
->exists();
->with(['shift.festivalSection', 'shift.timeSlot'])
->first();
if ($existingAssignment) {
$section = $existingAssignment->shift->festivalSection->name;
$timeSlot = $existingAssignment->shift->timeSlot->name;
$time = $existingAssignment->shift->timeSlot->start_time
. '' . $existingAssignment->shift->timeSlot->end_time;
$message = $isClaim
? "Je bent al ingepland bij \"{$section}\" voor {$timeSlot} ({$time})."
: "Deze persoon is al ingepland bij \"{$section}\" voor {$timeSlot} ({$time}).";
if ($conflict) {
throw ValidationException::withMessages([
'person_id' => ['Deze persoon is al ingepland voor dit tijdslot.'],
'person_id' => [$message],
]);
}
}

View File

@@ -147,6 +147,7 @@ Route::middleware('auth:sanctum')->group(function () {
// Shift assignments (event-level)
Route::get('shift-assignments', [ShiftAssignmentController::class, 'index']);
Route::get('shifts/{shift}/assignable-persons', [ShiftAssignmentController::class, 'assignablePersons']);
Route::post('shift-assignments/{shiftAssignment}/approve', [ShiftAssignmentController::class, 'approve']);
Route::post('shift-assignments/{shiftAssignment}/reject', [ShiftAssignmentController::class, 'reject']);
Route::post('shift-assignments/{shiftAssignment}/cancel', [ShiftAssignmentController::class, 'cancel']);

View File

@@ -0,0 +1,296 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Enums\ShiftAssignmentStatus;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\FestivalSection;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\Shift;
use App\Models\ShiftAssignment;
use App\Models\TimeSlot;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AssignablePersonsTest extends TestCase
{
use RefreshDatabase;
private User $orgAdmin;
private User $outsider;
private Organisation $organisation;
private Organisation $otherOrganisation;
private Event $event;
private FestivalSection $section;
private FestivalSection $otherSection;
private TimeSlot $timeSlot;
private CrowdType $crowdType;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->otherOrganisation = Organisation::factory()->create();
$this->orgAdmin = User::factory()->create();
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->outsider = User::factory()->create();
$this->otherOrganisation->users()->attach($this->outsider, ['role' => 'org_admin']);
$this->event = Event::factory()->create(['organisation_id' => $this->organisation->id]);
$this->section = FestivalSection::factory()->create(['event_id' => $this->event->id]);
$this->otherSection = FestivalSection::factory()->create(['event_id' => $this->event->id]);
$this->timeSlot = TimeSlot::factory()->create(['event_id' => $this->event->id]);
$this->crowdType = CrowdType::factory()->systemType('VOLUNTEER')->create([
'organisation_id' => $this->organisation->id,
]);
}
private function createOpenShift(array $overrides = []): Shift
{
return Shift::factory()->open()->create(array_merge([
'festival_section_id' => $this->section->id,
'time_slot_id' => $this->timeSlot->id,
'slots_total' => 4,
'slots_open_for_claiming' => 3,
], $overrides));
}
private function createPerson(array $overrides = []): Person
{
return Person::factory()->approved()->create(array_merge([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
], $overrides));
}
// =========================================================================
// Assignable persons endpoint
// =========================================================================
public function test_assignable_persons_returns_available_persons(): void
{
$shift = $this->createOpenShift();
$person = $this->createPerson();
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $person->id)
->assertJsonPath('data.0.is_available', true)
->assertJsonPath('data.0.already_assigned', false)
->assertJsonPath('data.0.conflict', null);
}
public function test_assignable_persons_shows_conflict_details(): void
{
$shift1 = $this->createOpenShift();
$shift2 = $this->createOpenShift(['festival_section_id' => $this->otherSection->id]);
$person = $this->createPerson();
// Assign person to shift1 (same time slot)
ShiftAssignment::factory()->create([
'shift_id' => $shift1->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::APPROVED,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift2->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.is_available', false)
->assertJsonPath('data.0.already_assigned', false)
->assertJsonPath('data.0.conflict.section_name', $this->section->name)
->assertJsonPath('data.0.conflict.time_slot_name', $this->timeSlot->name);
}
public function test_assignable_persons_shows_already_assigned(): void
{
$shift = $this->createOpenShift();
$person = $this->createPerson();
ShiftAssignment::factory()->create([
'shift_id' => $shift->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::APPROVED,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.already_assigned', true)
->assertJsonPath('data.0.is_available', false);
}
public function test_assignable_persons_excludes_non_approved_persons(): void
{
$shift = $this->createOpenShift();
Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'status' => 'pending',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonCount(0, 'data');
}
public function test_assignable_persons_unauthenticated_returns_401(): void
{
$shift = $this->createOpenShift();
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertUnauthorized();
}
public function test_assignable_persons_wrong_org_returns_403(): void
{
$shift = $this->createOpenShift();
Sanctum::actingAs($this->outsider);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertForbidden();
}
public function test_assignable_persons_sorts_available_first(): void
{
$shift1 = $this->createOpenShift();
$shift2 = $this->createOpenShift(['festival_section_id' => $this->otherSection->id]);
$available = $this->createPerson(['name' => 'Anna Bakker']);
$conflicted = $this->createPerson(['name' => 'Bob Jansen']);
ShiftAssignment::factory()->create([
'shift_id' => $shift1->id,
'person_id' => $conflicted->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::APPROVED,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift2->id}/assignable-persons",
);
$response->assertOk()
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', $available->id)
->assertJsonPath('data.0.is_available', true)
->assertJsonPath('data.1.id', $conflicted->id)
->assertJsonPath('data.1.is_available', false);
}
public function test_assignable_persons_includes_crowd_type(): void
{
$shift = $this->createOpenShift();
$this->createPerson();
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.crowd_type.system_type', 'VOLUNTEER')
->assertJsonPath('data.0.crowd_type.name', $this->crowdType->name);
}
// =========================================================================
// Improved conflict error messages
// =========================================================================
public function test_assign_conflict_error_includes_section_and_timeslot(): void
{
$shift1 = $this->createOpenShift();
$shift2 = $this->createOpenShift(['festival_section_id' => $this->otherSection->id]);
$person = $this->createPerson();
ShiftAssignment::factory()->create([
'shift_id' => $shift1->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::APPROVED,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/events/{$this->event->id}/sections/{$this->otherSection->id}/shifts/{$shift2->id}/assign",
['person_id' => $person->id],
);
$response->assertUnprocessable()
->assertJsonValidationErrors(['person_id']);
$message = $response->json('errors.person_id.0');
$this->assertStringContainsString($this->section->name, $message);
$this->assertStringContainsString($this->timeSlot->name, $message);
$this->assertStringContainsString('Deze persoon is al ingepland bij', $message);
}
public function test_claim_conflict_error_uses_volunteer_language(): void
{
$shift1 = $this->createOpenShift();
$shift2 = $this->createOpenShift(['festival_section_id' => $this->otherSection->id]);
$person = $this->createPerson(['user_id' => $this->orgAdmin->id]);
ShiftAssignment::factory()->create([
'shift_id' => $shift1->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::APPROVED,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/events/{$this->event->id}/sections/{$this->otherSection->id}/shifts/{$shift2->id}/claim",
['person_id' => $person->id],
);
$response->assertUnprocessable();
$message = $response->json('errors.person_id.0');
$this->assertStringContainsString('Je bent al ingepland bij', $message);
}
}