feat: enrich assignable-persons with tags, preferences, availability and cascading filters

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 22:05:02 +02:00
parent 9e4e0c3d4b
commit 04ceecc51d
5 changed files with 540 additions and 141 deletions

View File

@@ -14,10 +14,12 @@ use App\Models\Event;
use App\Models\Person;
use App\Models\Shift;
use App\Models\ShiftAssignment;
use App\Models\VolunteerAvailability;
use App\Services\ShiftAssignmentService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
final class ShiftAssignmentController extends Controller
@@ -106,6 +108,8 @@ final class ShiftAssignmentController extends Controller
{
Gate::authorize('viewAny', [ShiftAssignment::class, $event]);
$shift->load(['festivalSection', 'timeSlot']);
$festivalEventId = $event->parent_event_id ?? $event->id;
$timeSlotId = $shift->time_slot_id;
$shiftId = $shift->id;
@@ -142,8 +146,37 @@ final class ShiftAssignmentController extends Controller
->where('status', PersonStatus::APPROVED)
->with('crowdType')
->orderBy('name')
->get()
->map(function (Person $person) use ($conflicts, $alreadyAssigned, $previousAssignments, $shiftId) {
->get();
// Batch: tags for all persons with user_id
$userIds = $persons->pluck('user_id')->filter()->unique();
$allTags = collect();
if ($userIds->isNotEmpty()) {
$allTags = DB::table('user_organisation_tags')
->join('person_tags', 'user_organisation_tags.person_tag_id', '=', 'person_tags.id')
->whereIn('user_organisation_tags.user_id', $userIds)
->where('user_organisation_tags.organisation_id', $event->organisation_id)
->where('person_tags.is_active', true)
->select(
'user_organisation_tags.user_id',
'person_tags.name',
'person_tags.icon',
'person_tags.color',
'user_organisation_tags.proficiency',
)
->get()
->groupBy('user_id');
}
// Batch: availability for this shift's time slot
$personIds = $persons->pluck('id');
$availablePersonIds = VolunteerAvailability::where('time_slot_id', $shift->time_slot_id)
->whereIn('person_id', $personIds)
->pluck('person_id')
->flip();
$mappedPersons = $persons
->map(function (Person $person) use ($conflicts, $alreadyAssigned, $previousAssignments, $shiftId, $allTags, $availablePersonIds) {
$isAlreadyAssigned = $alreadyAssigned->has($person->id);
$conflict = $conflicts->get($person->id);
$hasConflict = $conflict && $conflict->shift_id !== $shiftId;
@@ -174,6 +207,16 @@ final class ShiftAssignmentController extends Controller
'cancelled_at' => $previous->cancelled_at?->toIso8601String(),
'rejection_reason' => $previous->rejection_reason,
] : null,
'tags' => $person->user_id
? ($allTags->get($person->user_id) ?? collect())->map(fn ($t) => [
'name' => $t->name,
'icon' => $t->icon,
'color' => $t->color,
'proficiency' => $t->proficiency,
])->values()->toArray()
: [],
'section_preferences' => $person->custom_fields['section_preferences'] ?? [],
'has_availability' => $availablePersonIds->has($person->id),
];
})
->sortBy([
@@ -183,6 +226,15 @@ final class ShiftAssignmentController extends Controller
])
->values();
return response()->json(['data' => $persons]);
return response()->json([
'data' => $mappedPersons,
'meta' => [
'section_name' => $shift->festivalSection->name,
'time_slot_name' => $shift->timeSlot->name,
'slots_total' => $shift->slots_total,
'filled_slots' => $shift->filled_slots,
'is_overbooked' => $shift->filled_slots >= $shift->slots_total,
],
]);
}
}

View File

@@ -11,10 +11,13 @@ use App\Models\Event;
use App\Models\FestivalSection;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\PersonTag;
use App\Models\Shift;
use App\Models\ShiftAssignment;
use App\Models\TimeSlot;
use App\Models\User;
use App\Models\UserOrganisationTag;
use App\Models\VolunteerAvailability;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
@@ -492,6 +495,210 @@ class AssignablePersonsTest extends TestCase
->assertJsonPath('data.0.previous_assignment.rejection_reason', 'Geen ervaring');
}
// =========================================================================
// Enriched data: tags, section_preferences, has_availability, meta
// =========================================================================
public function test_assignable_persons_includes_tags_for_person_with_user_id(): void
{
$shift = $this->createOpenShift();
$user = User::factory()->create();
$person = $this->createPerson(['user_id' => $user->id]);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
'name' => 'Tapper',
'icon' => 'tabler-beer',
'color' => '#FF9800',
'is_active' => true,
]);
UserOrganisationTag::factory()->create([
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tag->id,
'proficiency' => 'experienced',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonCount(1, 'data.0.tags')
->assertJsonPath('data.0.tags.0.name', 'Tapper')
->assertJsonPath('data.0.tags.0.icon', 'tabler-beer')
->assertJsonPath('data.0.tags.0.color', '#FF9800')
->assertJsonPath('data.0.tags.0.proficiency', 'experienced');
}
public function test_assignable_persons_tags_empty_for_person_without_user_id(): void
{
$shift = $this->createOpenShift();
$this->createPerson(['user_id' => null]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.tags', []);
}
public function test_assignable_persons_excludes_inactive_tags(): void
{
$shift = $this->createOpenShift();
$user = User::factory()->create();
$this->createPerson(['user_id' => $user->id]);
$tag = PersonTag::factory()->inactive()->create([
'organisation_id' => $this->organisation->id,
]);
UserOrganisationTag::factory()->create([
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tag->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.tags', []);
}
public function test_assignable_persons_includes_section_preferences_from_custom_fields(): void
{
$shift = $this->createOpenShift();
$this->createPerson([
'custom_fields' => [
'section_preferences' => [
['section_name' => $this->section->name, 'priority' => 1],
['section_name' => 'Other Section', 'priority' => 2],
],
],
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonCount(2, 'data.0.section_preferences')
->assertJsonPath('data.0.section_preferences.0.section_name', $this->section->name)
->assertJsonPath('data.0.section_preferences.0.priority', 1);
}
public function test_assignable_persons_section_preferences_empty_when_not_set(): void
{
$shift = $this->createOpenShift();
$this->createPerson(['custom_fields' => null]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.section_preferences', []);
}
public function test_assignable_persons_has_availability_true_when_record_exists(): void
{
$shift = $this->createOpenShift();
$person = $this->createPerson();
VolunteerAvailability::factory()->create([
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.has_availability', true);
}
public function test_assignable_persons_has_availability_false_when_no_record(): 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.has_availability', false);
}
public function test_assignable_persons_meta_includes_shift_context(): void
{
$shift = $this->createOpenShift();
$this->createPerson();
// Create an approved assignment so filled_slots > 0
$otherPerson = $this->createPerson();
ShiftAssignment::factory()->create([
'shift_id' => $shift->id,
'person_id' => $otherPerson->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('meta.section_name', $this->section->name)
->assertJsonPath('meta.time_slot_name', $this->timeSlot->name)
->assertJsonPath('meta.slots_total', 4)
->assertJsonPath('meta.filled_slots', 1)
->assertJsonPath('meta.is_overbooked', false);
}
public function test_assignable_persons_meta_is_overbooked_when_full(): void
{
$shift = $this->createOpenShift(['slots_total' => 1]);
$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('meta.is_overbooked', true);
}
private function assertJsonPath($response, string $path, mixed $expected): void
{
$this->assertEquals($expected, $response->json($path));