feat: smart re-assignment with cancellation source tracking

Add cancelled_by, cancellation_source (organiser|volunteer|system), and
cancelled_at columns to shift_assignments. Cancel flow now records who
cancelled and why. Assign flow reactivates existing cancelled/rejected
records instead of creating duplicates, preventing UNIQUE constraint
violations. Assignable-persons endpoint returns previous_assignment data
for contextual UI indicators. Frontend shows cancellation source labels,
previous assignment history in assign dialog, and "Opnieuw toewijzen"
buttons with volunteer-cancelled confirmation dialogs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 21:50:24 +02:00
parent dfe7a63ad3
commit 3e292567c3
11 changed files with 554 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ 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;
@@ -293,4 +294,206 @@ class AssignablePersonsTest extends TestCase
$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');
}
private function assertJsonPath($response, string $path, mixed $expected): void
{
$this->assertEquals($expected, $response->json($path));
}
}