Files
crewli/api/tests/Feature/Api/V1/AssignablePersonsTest.php

707 lines
25 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1;
use App\Enums\CancellationSource;
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\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;
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);
}
// =========================================================================
// Cancellation source tracking
// =========================================================================
public function test_cancel_stores_cancellation_source_and_cancelled_by(): void
{
$shift = $this->createOpenShift();
$person = $this->createPerson();
$assignment = ShiftAssignment::factory()->approved()->create([
'shift_id' => $shift->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/events/{$this->event->id}/shift-assignments/{$assignment->id}/cancel",
);
$response->assertOk()
->assertJsonPath('data.cancellation_source', 'organiser')
->assertJsonPath('data.cancelled_by', $this->orgAdmin->id);
$this->assertNotNull($response->json('data.cancelled_at'));
}
// =========================================================================
// Re-assignment (reactivation)
// =========================================================================
public function test_assign_after_cancellation_reactivates_existing_record(): void
{
$shift = $this->createOpenShift();
$person = $this->createPerson();
$assignment = ShiftAssignment::factory()->create([
'shift_id' => $shift->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::CANCELLED,
'cancelled_by' => $this->orgAdmin->id,
'cancellation_source' => CancellationSource::ORGANISER,
'cancelled_at' => now(),
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/events/{$this->event->id}/sections/{$this->section->id}/shifts/{$shift->id}/assign",
['person_id' => $person->id],
);
$response->assertCreated()
->assertJsonPath('data.status', 'approved')
->assertJsonPath('data.cancelled_by', null)
->assertJsonPath('data.cancellation_source', null)
->assertJsonPath('data.cancelled_at', null);
// Same record reactivated, not a new one
$this->assertJsonPath($response, 'data.id', $assignment->id);
$this->assertDatabaseCount('shift_assignments', 1);
}
public function test_assign_after_rejection_reactivates_existing_record(): void
{
$shift = $this->createOpenShift();
$person = $this->createPerson();
$assignment = ShiftAssignment::factory()->create([
'shift_id' => $shift->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::REJECTED,
'rejection_reason' => 'Niet geschikt',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson(
"/api/v1/events/{$this->event->id}/sections/{$this->section->id}/shifts/{$shift->id}/assign",
['person_id' => $person->id],
);
$response->assertCreated()
->assertJsonPath('data.status', 'approved')
->assertJsonPath('data.rejection_reason', null);
$this->assertEquals($assignment->id, $response->json('data.id'));
$this->assertDatabaseCount('shift_assignments', 1);
}
public function test_conflict_check_excludes_cancelled_assignments(): void
{
$shift1 = $this->createOpenShift();
$shift2 = $this->createOpenShift(['festival_section_id' => $this->otherSection->id]);
$person = $this->createPerson();
// Cancelled assignment on shift1 should NOT block assignment on shift2
ShiftAssignment::factory()->create([
'shift_id' => $shift1->id,
'person_id' => $person->id,
'time_slot_id' => $this->timeSlot->id,
'status' => ShiftAssignmentStatus::CANCELLED,
]);
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->assertCreated();
}
// =========================================================================
// Assignable persons — previous assignment data
// =========================================================================
public function test_assignable_persons_cancelled_person_has_previous_assignment(): 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::CANCELLED,
'cancellation_source' => CancellationSource::ORGANISER,
'cancelled_at' => now(),
]);
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', false)
->assertJsonPath('data.0.is_available', true)
->assertJsonPath('data.0.previous_assignment.status', 'cancelled')
->assertJsonPath('data.0.previous_assignment.cancellation_source', 'organiser');
}
public function test_assignable_persons_volunteer_cancelled_has_volunteer_source(): 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::CANCELLED,
'cancellation_source' => CancellationSource::VOLUNTEER,
'cancelled_at' => now(),
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson(
"/api/v1/events/{$this->event->id}/shifts/{$shift->id}/assignable-persons",
);
$response->assertOk()
->assertJsonPath('data.0.previous_assignment.cancellation_source', 'volunteer');
}
public function test_assignable_persons_rejected_person_has_previous_assignment(): 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::REJECTED,
'rejection_reason' => 'Geen ervaring',
]);
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', false)
->assertJsonPath('data.0.previous_assignment.status', 'rejected')
->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));
}
}