feat(portal): shift claiming and my-shifts for volunteer portal
Backend: PortalShiftController with 4 endpoints (available-shifts, my-shifts, claim, cancel) delegating to ShiftAssignmentService. 24 PHPUnit tests covering happy paths, auth, conflicts, and edge cases. Frontend: claim-shifts and my-shifts pages with TanStack Query composable, conflict detection, confirmation dialogs, and cancel flow. Navigation and dashboard cards wired up for approved volunteers. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
259
api/app/Http/Controllers/Api/V1/Portal/PortalShiftController.php
Normal file
259
api/app/Http/Controllers/Api/V1/Portal/PortalShiftController.php
Normal file
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Portal;
|
||||
|
||||
use App\Enums\CancellationSource;
|
||||
use App\Enums\ShiftAssignmentStatus;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Event;
|
||||
use App\Models\Person;
|
||||
use App\Models\Shift;
|
||||
use App\Models\ShiftAssignment;
|
||||
use App\Services\ShiftAssignmentService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class PortalShiftController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ShiftAssignmentService $shiftAssignmentService,
|
||||
) {}
|
||||
|
||||
public function availableShifts(Request $request, Event $event): JsonResponse
|
||||
{
|
||||
$person = $this->resolvePerson($event);
|
||||
|
||||
if ($person->status !== 'approved') {
|
||||
return $this->forbidden('Je moet eerst goedgekeurd zijn om diensten te claimen.');
|
||||
}
|
||||
|
||||
$shifts = Shift::query()
|
||||
->where('status', 'open')
|
||||
->where('slots_open_for_claiming', '>', 0)
|
||||
->whereHas('timeSlot', fn ($q) => $q->where('event_id', $event->id)->where('person_type', 'VOLUNTEER'))
|
||||
->with(['festivalSection', 'timeSlot', 'location'])
|
||||
->withCount([
|
||||
'shiftAssignments as active_assignments_count' => fn ($q) => $q->whereNotIn('status', [
|
||||
ShiftAssignmentStatus::REJECTED,
|
||||
ShiftAssignmentStatus::CANCELLED,
|
||||
]),
|
||||
])
|
||||
->get()
|
||||
->filter(fn (Shift $s) => $s->active_assignments_count < $s->slots_open_for_claiming);
|
||||
|
||||
// Get person's active assignments for conflict detection
|
||||
$personActiveTimeSlots = ShiftAssignment::where('person_id', $person->id)
|
||||
->active()
|
||||
->pluck('time_slot_id')
|
||||
->toArray();
|
||||
|
||||
// Group by date, then time slot
|
||||
$grouped = $shifts
|
||||
->groupBy(fn (Shift $s) => $s->timeSlot->date->format('Y-m-d'))
|
||||
->sortKeys()
|
||||
->map(function ($dateShifts, string $date) use ($personActiveTimeSlots) {
|
||||
$carbonDate = Carbon::parse($date);
|
||||
|
||||
$timeSlots = $dateShifts
|
||||
->groupBy(fn (Shift $s) => $s->time_slot_id)
|
||||
->map(function ($slotShifts) use ($personActiveTimeSlots) {
|
||||
$timeSlot = $slotShifts->first()->timeSlot;
|
||||
$hasConflict = in_array($timeSlot->id, $personActiveTimeSlots);
|
||||
|
||||
return [
|
||||
'time_slot_id' => $timeSlot->id,
|
||||
'name' => $timeSlot->name,
|
||||
'start_time' => Carbon::parse($timeSlot->start_time)->format('H:i'),
|
||||
'end_time' => Carbon::parse($timeSlot->end_time)->format('H:i'),
|
||||
'shifts' => $slotShifts->map(function (Shift $shift) use ($hasConflict) {
|
||||
$slotsAvailable = $shift->slots_open_for_claiming - $shift->active_assignments_count;
|
||||
|
||||
return [
|
||||
'id' => $shift->id,
|
||||
'title' => $shift->title,
|
||||
'section_name' => $shift->festivalSection->name,
|
||||
'section_icon' => $shift->festivalSection->icon,
|
||||
'location_name' => $shift->location?->name,
|
||||
'slots_total' => $shift->slots_total,
|
||||
'slots_open_for_claiming' => $shift->slots_open_for_claiming,
|
||||
'slots_claimed' => $shift->active_assignments_count,
|
||||
'slots_available' => max(0, $slotsAvailable),
|
||||
'has_conflict' => $hasConflict && !$shift->allow_overlap,
|
||||
'conflict_reason' => ($hasConflict && !$shift->allow_overlap)
|
||||
? 'Je hebt al een dienst op dit tijdslot.'
|
||||
: null,
|
||||
'report_time' => $shift->report_time
|
||||
? Carbon::parse($shift->report_time)->format('H:i')
|
||||
: null,
|
||||
'description' => $shift->description,
|
||||
];
|
||||
})->values()->all(),
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return [
|
||||
'date' => $date,
|
||||
'date_label' => ucfirst($carbonDate->translatedFormat('l j F')),
|
||||
'time_slots' => $timeSlots,
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return $this->success($grouped);
|
||||
}
|
||||
|
||||
public function myShifts(Request $request, Event $event): JsonResponse
|
||||
{
|
||||
$person = $this->resolvePerson($event);
|
||||
|
||||
$assignments = ShiftAssignment::where('person_id', $person->id)
|
||||
->whereHas('shift.timeSlot', fn ($q) => $q->where('event_id', $event->id))
|
||||
->with(['shift.festivalSection', 'shift.timeSlot', 'shift.location'])
|
||||
->get();
|
||||
|
||||
$today = now()->format('Y-m-d');
|
||||
|
||||
$formatted = $assignments->map(function (ShiftAssignment $a) use ($today) {
|
||||
$shift = $a->shift;
|
||||
$timeSlot = $shift->timeSlot;
|
||||
$isFuture = $timeSlot->date->format('Y-m-d') >= $today;
|
||||
$canCancel = $isFuture && in_array($a->status, [
|
||||
ShiftAssignmentStatus::PENDING_APPROVAL,
|
||||
ShiftAssignmentStatus::APPROVED,
|
||||
]);
|
||||
|
||||
return [
|
||||
'assignment_id' => $a->id,
|
||||
'status' => $a->status->value,
|
||||
'shift_title' => $shift->title,
|
||||
'section_name' => $shift->festivalSection->name,
|
||||
'section_icon' => $shift->festivalSection->icon,
|
||||
'location_name' => $shift->location?->name,
|
||||
'date' => $timeSlot->date->format('Y-m-d'),
|
||||
'date_label' => ucfirst($timeSlot->date->translatedFormat('l j F')),
|
||||
'start_time' => Carbon::parse($timeSlot->start_time)->format('H:i'),
|
||||
'end_time' => Carbon::parse($timeSlot->end_time)->format('H:i'),
|
||||
'report_time' => $shift->report_time
|
||||
? Carbon::parse($shift->report_time)->format('H:i')
|
||||
: null,
|
||||
'can_cancel' => $canCancel,
|
||||
];
|
||||
});
|
||||
|
||||
$upcoming = $formatted->filter(
|
||||
fn ($a) => $a['date'] >= $today && in_array($a['status'], ['pending_approval', 'approved']),
|
||||
)->sortBy('date')->values()->all();
|
||||
|
||||
$past = $formatted->filter(
|
||||
fn ($a) => $a['date'] < $today && in_array($a['status'], ['approved', 'completed']),
|
||||
)->sortByDesc('date')->values()->all();
|
||||
|
||||
$cancelled = $formatted->filter(
|
||||
fn ($a) => in_array($a['status'], ['cancelled', 'rejected']),
|
||||
)->sortByDesc('date')->values()->all();
|
||||
|
||||
return $this->success([
|
||||
'upcoming' => $upcoming,
|
||||
'past' => $past,
|
||||
'cancelled' => $cancelled,
|
||||
]);
|
||||
}
|
||||
|
||||
public function claim(Request $request, Event $event, Shift $shift): JsonResponse
|
||||
{
|
||||
$person = $this->resolvePerson($event);
|
||||
|
||||
if ($person->status !== 'approved') {
|
||||
return $this->forbidden('Je moet eerst goedgekeurd zijn om diensten te claimen.');
|
||||
}
|
||||
|
||||
try {
|
||||
$assignment = $this->shiftAssignmentService->claim($shift, $person);
|
||||
} catch (ValidationException $e) {
|
||||
$messages = collect($e->errors())->flatten();
|
||||
$message = $messages->first() ?? 'Er is een fout opgetreden.';
|
||||
|
||||
// Map internal messages to portal-friendly messages
|
||||
$portalMessage = $this->mapClaimErrorMessage($message);
|
||||
|
||||
return $this->error($portalMessage, 422);
|
||||
}
|
||||
|
||||
$isApproved = $assignment->status === ShiftAssignmentStatus::APPROVED;
|
||||
|
||||
return $this->success([
|
||||
'assignment_id' => $assignment->id,
|
||||
'status' => $assignment->status->value,
|
||||
'message' => $isApproved
|
||||
? 'Je bent ingepland!'
|
||||
: 'Je claim is ingediend en wacht op goedkeuring.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function cancel(Request $request, Event $event, ShiftAssignment $shiftAssignment): JsonResponse
|
||||
{
|
||||
$person = $this->resolvePerson($event);
|
||||
|
||||
// Must be the person's own assignment
|
||||
if ($shiftAssignment->person_id !== $person->id) {
|
||||
return $this->forbidden('Je kunt alleen je eigen diensten annuleren.');
|
||||
}
|
||||
|
||||
// Must be cancellable status
|
||||
if (!in_array($shiftAssignment->status, [ShiftAssignmentStatus::PENDING_APPROVAL, ShiftAssignmentStatus::APPROVED])) {
|
||||
return $this->error('Deze dienst kan niet meer worden geannuleerd.', 422);
|
||||
}
|
||||
|
||||
// Must be a future shift
|
||||
$timeSlot = $shiftAssignment->shift->timeSlot;
|
||||
if ($timeSlot->date->format('Y-m-d') < now()->format('Y-m-d')) {
|
||||
return $this->error('Je kunt geen diensten in het verleden annuleren.', 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->shiftAssignmentService->cancel(
|
||||
$shiftAssignment,
|
||||
$request->user(),
|
||||
CancellationSource::VOLUNTEER,
|
||||
);
|
||||
} catch (ValidationException $e) {
|
||||
$messages = collect($e->errors())->flatten();
|
||||
|
||||
return $this->error($messages->first() ?? 'Er is een fout opgetreden.', 422);
|
||||
}
|
||||
|
||||
return $this->success(['message' => 'Je dienst is geannuleerd.']);
|
||||
}
|
||||
|
||||
private function resolvePerson(Event $event): Person
|
||||
{
|
||||
return Person::where('user_id', auth()->id())
|
||||
->where('event_id', $event->id)
|
||||
->firstOrFail();
|
||||
}
|
||||
|
||||
private function mapClaimErrorMessage(string $message): string
|
||||
{
|
||||
if (str_contains($message, 'niet open')) {
|
||||
return 'Deze dienst is niet beschikbaar voor inschrijving.';
|
||||
}
|
||||
if (str_contains($message, 'niet goedgekeurd') || str_contains($message, 'Persoon is nog niet')) {
|
||||
return 'Je moet eerst goedgekeurd zijn om diensten te claimen.';
|
||||
}
|
||||
if (str_contains($message, 'claimbare slots') || str_contains($message, 'Geen claimbare')) {
|
||||
return 'Deze dienst is helaas al vol.';
|
||||
}
|
||||
if (str_contains($message, 'al ingepland')) {
|
||||
return 'Je hebt al een dienst op dit tijdslot.';
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ use App\Http\Controllers\Api\V1\PublicRegistrationDataController;
|
||||
use App\Http\Controllers\Api\V1\PortalTokenController;
|
||||
use App\Http\Controllers\Api\V1\PasswordResetController;
|
||||
use App\Http\Controllers\Api\V1\PortalMeController;
|
||||
use App\Http\Controllers\Api\V1\Portal\PortalShiftController;
|
||||
use App\Http\Controllers\Api\V1\UserOrganisationTagController;
|
||||
use App\Models\FestivalSection;
|
||||
use App\Models\Organisation;
|
||||
@@ -79,6 +80,10 @@ Route::middleware('auth:sanctum')->group(function () {
|
||||
|
||||
// Portal (authenticated)
|
||||
Route::get('portal/me', [PortalMeController::class, 'index']);
|
||||
Route::get('portal/events/{event}/available-shifts', [PortalShiftController::class, 'availableShifts']);
|
||||
Route::get('portal/events/{event}/my-shifts', [PortalShiftController::class, 'myShifts']);
|
||||
Route::post('portal/events/{event}/shifts/{shift}/claim', [PortalShiftController::class, 'claim']);
|
||||
Route::post('portal/events/{event}/assignments/{shiftAssignment}/cancel', [PortalShiftController::class, 'cancel']);
|
||||
|
||||
// Organisations
|
||||
Route::apiResource('organisations', OrganisationController::class)
|
||||
|
||||
627
api/tests/Feature/Api/V1/Portal/PortalShiftClaimingTest.php
Normal file
627
api/tests/Feature/Api/V1/Portal/PortalShiftClaimingTest.php
Normal file
@@ -0,0 +1,627 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\V1\Portal;
|
||||
|
||||
use App\Enums\ShiftAssignmentStatus;
|
||||
use App\Models\CrowdType;
|
||||
use App\Models\Event;
|
||||
use App\Models\FestivalSection;
|
||||
use App\Models\Location;
|
||||
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 PortalShiftClaimingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $volunteer;
|
||||
private Organisation $organisation;
|
||||
private Event $event;
|
||||
private FestivalSection $section;
|
||||
private TimeSlot $timeSlot;
|
||||
private CrowdType $crowdType;
|
||||
private Person $person;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->seed(RoleSeeder::class);
|
||||
|
||||
$this->organisation = Organisation::factory()->create();
|
||||
$this->volunteer = User::factory()->create();
|
||||
$this->organisation->users()->attach($this->volunteer, ['role' => 'org_member']);
|
||||
|
||||
$this->event = Event::factory()->create(['organisation_id' => $this->organisation->id]);
|
||||
$this->section = FestivalSection::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crew_auto_accepts' => false,
|
||||
]);
|
||||
$this->timeSlot = TimeSlot::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'person_type' => 'VOLUNTEER',
|
||||
'date' => now()->addMonth(),
|
||||
]);
|
||||
$this->crowdType = CrowdType::factory()->systemType('VOLUNTEER')->create([
|
||||
'organisation_id' => $this->organisation->id,
|
||||
]);
|
||||
$this->person = Person::factory()->approved()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
'user_id' => $this->volunteer->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));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Available shifts
|
||||
// =========================================================================
|
||||
|
||||
public function test_available_shifts_returns_grouped_by_date_and_time_slot(): void
|
||||
{
|
||||
$shift = $this->createOpenShift(['title' => 'Tapper']);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonStructure([
|
||||
'data' => [
|
||||
'*' => [
|
||||
'date',
|
||||
'date_label',
|
||||
'time_slots' => [
|
||||
'*' => [
|
||||
'time_slot_id',
|
||||
'name',
|
||||
'start_time',
|
||||
'end_time',
|
||||
'shifts' => [
|
||||
'*' => [
|
||||
'id',
|
||||
'title',
|
||||
'section_name',
|
||||
'section_icon',
|
||||
'location_name',
|
||||
'slots_total',
|
||||
'slots_open_for_claiming',
|
||||
'slots_claimed',
|
||||
'slots_available',
|
||||
'has_conflict',
|
||||
'conflict_reason',
|
||||
'report_time',
|
||||
'description',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $response->json('data'));
|
||||
$this->assertEquals('Tapper', $response->json('data.0.time_slots.0.shifts.0.title'));
|
||||
}
|
||||
|
||||
public function test_available_shifts_only_shows_volunteer_time_slots(): void
|
||||
{
|
||||
// Create a CREW time slot with a shift — should not appear
|
||||
$crewSlot = TimeSlot::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'person_type' => 'CREW',
|
||||
'date' => now()->addMonth(),
|
||||
]);
|
||||
$this->createOpenShift(['time_slot_id' => $crewSlot->id]);
|
||||
|
||||
// Create a VOLUNTEER shift — should appear
|
||||
$this->createOpenShift(['title' => 'Volunteer Shift']);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
$allShifts = collect($response->json('data'))
|
||||
->flatMap(fn ($day) => collect($day['time_slots']))
|
||||
->flatMap(fn ($ts) => $ts['shifts']);
|
||||
|
||||
$this->assertCount(1, $allShifts);
|
||||
$this->assertEquals('Volunteer Shift', $allShifts->first()['title']);
|
||||
}
|
||||
|
||||
public function test_available_shifts_excludes_full_shifts(): void
|
||||
{
|
||||
$shift = $this->createOpenShift(['slots_open_for_claiming' => 1]);
|
||||
|
||||
// Fill the claimable slot
|
||||
ShiftAssignment::factory()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => Person::factory()->approved()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
])->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
'status' => ShiftAssignmentStatus::PENDING_APPROVAL,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$allShifts = collect($response->json('data'))
|
||||
->flatMap(fn ($day) => collect($day['time_slots']))
|
||||
->flatMap(fn ($ts) => $ts['shifts']);
|
||||
|
||||
$this->assertCount(0, $allShifts);
|
||||
}
|
||||
|
||||
public function test_available_shifts_marks_conflicting_shifts(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
|
||||
// Create existing assignment on same time slot
|
||||
ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => Shift::factory()->open()->create([
|
||||
'festival_section_id' => $this->section->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
])->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$shiftData = $response->json('data.0.time_slots.0.shifts.0');
|
||||
$this->assertTrue($shiftData['has_conflict']);
|
||||
$this->assertNotNull($shiftData['conflict_reason']);
|
||||
}
|
||||
|
||||
public function test_available_shifts_pending_person_gets_403(): void
|
||||
{
|
||||
$pendingUser = User::factory()->create();
|
||||
$this->organisation->users()->attach($pendingUser, ['role' => 'org_member']);
|
||||
Person::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
'user_id' => $pendingUser->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($pendingUser);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_available_shifts_unauthenticated_returns_401(): void
|
||||
{
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_available_shifts_only_shows_open_status_shifts(): void
|
||||
{
|
||||
// Draft shift — should not appear
|
||||
Shift::factory()->create([
|
||||
'festival_section_id' => $this->section->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
'slots_total' => 4,
|
||||
'slots_open_for_claiming' => 3,
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
// Open shift — should appear
|
||||
$this->createOpenShift(['title' => 'Open Shift']);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/available-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$allShifts = collect($response->json('data'))
|
||||
->flatMap(fn ($day) => collect($day['time_slots']))
|
||||
->flatMap(fn ($ts) => $ts['shifts']);
|
||||
|
||||
$this->assertCount(1, $allShifts);
|
||||
$this->assertEquals('Open Shift', $allShifts->first()['title']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// My shifts
|
||||
// =========================================================================
|
||||
|
||||
public function test_my_shifts_returns_sections(): void
|
||||
{
|
||||
$futureSlot = $this->timeSlot; // Already future
|
||||
$pastSlot = TimeSlot::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'person_type' => 'VOLUNTEER',
|
||||
'date' => now()->subMonth(),
|
||||
]);
|
||||
|
||||
$futureShift = $this->createOpenShift();
|
||||
$pastShift = $this->createOpenShift(['time_slot_id' => $pastSlot->id]);
|
||||
|
||||
// Upcoming approved
|
||||
ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $futureShift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $futureSlot->id,
|
||||
]);
|
||||
|
||||
// Past approved
|
||||
ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $pastShift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $pastSlot->id,
|
||||
]);
|
||||
|
||||
// Cancelled
|
||||
ShiftAssignment::factory()->create([
|
||||
'shift_id' => Shift::factory()->open()->create([
|
||||
'festival_section_id' => $this->section->id,
|
||||
'time_slot_id' => $futureSlot->id,
|
||||
'slots_total' => 4,
|
||||
'slots_open_for_claiming' => 3,
|
||||
])->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $futureSlot->id,
|
||||
'status' => ShiftAssignmentStatus::CANCELLED,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/my-shifts");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonStructure([
|
||||
'data' => [
|
||||
'upcoming',
|
||||
'past',
|
||||
'cancelled',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $response->json('data.upcoming'));
|
||||
$this->assertCount(1, $response->json('data.past'));
|
||||
$this->assertCount(1, $response->json('data.cancelled'));
|
||||
}
|
||||
|
||||
public function test_my_shifts_can_cancel_future_pending(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
ShiftAssignment::factory()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
'status' => ShiftAssignmentStatus::PENDING_APPROVAL,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/my-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertTrue($response->json('data.upcoming.0.can_cancel'));
|
||||
}
|
||||
|
||||
public function test_my_shifts_cannot_cancel_past_shifts(): void
|
||||
{
|
||||
$pastSlot = TimeSlot::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'person_type' => 'VOLUNTEER',
|
||||
'date' => now()->subMonth(),
|
||||
]);
|
||||
$pastShift = $this->createOpenShift(['time_slot_id' => $pastSlot->id]);
|
||||
|
||||
ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $pastShift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $pastSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/my-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertFalse($response->json('data.past.0.can_cancel'));
|
||||
}
|
||||
|
||||
public function test_my_shifts_only_returns_own_assignments(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
|
||||
// Other person's assignment
|
||||
$otherPerson = Person::factory()->approved()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
]);
|
||||
ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $otherPerson->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/my-shifts");
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertCount(0, $response->json('data.upcoming'));
|
||||
$this->assertCount(0, $response->json('data.past'));
|
||||
$this->assertCount(0, $response->json('data.cancelled'));
|
||||
}
|
||||
|
||||
public function test_my_shifts_unauthenticated_returns_401(): void
|
||||
{
|
||||
$response = $this->getJson("/api/v1/portal/events/{$this->event->id}/my-shifts");
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Claim
|
||||
// =========================================================================
|
||||
|
||||
public function test_successful_claim_returns_assignment(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift->id}/claim");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('data.status', 'pending_approval')
|
||||
->assertJsonPath('data.message', 'Je claim is ingediend en wacht op goedkeuring.');
|
||||
|
||||
$this->assertDatabaseHas('shift_assignments', [
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'status' => ShiftAssignmentStatus::PENDING_APPROVAL->value,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_auto_approve_claim_returns_approved_status(): void
|
||||
{
|
||||
$autoSection = FestivalSection::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crew_auto_accepts' => true,
|
||||
]);
|
||||
$shift = $this->createOpenShift(['festival_section_id' => $autoSection->id]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift->id}/claim");
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('data.status', 'approved')
|
||||
->assertJsonPath('data.message', 'Je bent ingepland!');
|
||||
}
|
||||
|
||||
public function test_claim_full_shift_returns_422(): void
|
||||
{
|
||||
$shift = $this->createOpenShift(['slots_open_for_claiming' => 1]);
|
||||
|
||||
// Fill the claimable slot
|
||||
ShiftAssignment::factory()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => Person::factory()->approved()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
])->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
'status' => ShiftAssignmentStatus::PENDING_APPROVAL,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift->id}/claim");
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertJsonFragment(['message' => 'Deze dienst is helaas al vol.']);
|
||||
}
|
||||
|
||||
public function test_claim_conflict_returns_422(): void
|
||||
{
|
||||
$shift1 = $this->createOpenShift();
|
||||
$shift2 = $this->createOpenShift();
|
||||
|
||||
ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $shift1->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift2->id}/claim");
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertJsonFragment(['message' => 'Je hebt al een dienst op dit tijdslot.']);
|
||||
}
|
||||
|
||||
public function test_claim_pending_person_returns_403(): void
|
||||
{
|
||||
$pendingUser = User::factory()->create();
|
||||
$this->organisation->users()->attach($pendingUser, ['role' => 'org_member']);
|
||||
Person::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
'user_id' => $pendingUser->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$shift = $this->createOpenShift();
|
||||
|
||||
Sanctum::actingAs($pendingUser);
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift->id}/claim");
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_claim_draft_shift_returns_422(): void
|
||||
{
|
||||
$shift = Shift::factory()->create([
|
||||
'festival_section_id' => $this->section->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
'slots_total' => 4,
|
||||
'slots_open_for_claiming' => 3,
|
||||
'status' => 'draft',
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift->id}/claim");
|
||||
|
||||
$response->assertUnprocessable();
|
||||
}
|
||||
|
||||
public function test_claim_unauthenticated_returns_401(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
|
||||
$response = $this->postJson("/api/v1/portal/events/{$this->event->id}/shifts/{$shift->id}/claim");
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Cancel
|
||||
// =========================================================================
|
||||
|
||||
public function test_successful_cancel(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
$assignment = ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson(
|
||||
"/api/v1/portal/events/{$this->event->id}/assignments/{$assignment->id}/cancel",
|
||||
);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('data.message', 'Je dienst is geannuleerd.');
|
||||
|
||||
$this->assertDatabaseHas('shift_assignments', [
|
||||
'id' => $assignment->id,
|
||||
'status' => ShiftAssignmentStatus::CANCELLED->value,
|
||||
'cancellation_source' => 'volunteer',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_cannot_cancel_someone_elses_assignment(): void
|
||||
{
|
||||
$otherPerson = Person::factory()->approved()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'crowd_type_id' => $this->crowdType->id,
|
||||
]);
|
||||
$shift = $this->createOpenShift();
|
||||
$assignment = ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $otherPerson->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson(
|
||||
"/api/v1/portal/events/{$this->event->id}/assignments/{$assignment->id}/cancel",
|
||||
);
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_cannot_cancel_completed_assignment(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
$assignment = ShiftAssignment::factory()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
'status' => ShiftAssignmentStatus::COMPLETED,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson(
|
||||
"/api/v1/portal/events/{$this->event->id}/assignments/{$assignment->id}/cancel",
|
||||
);
|
||||
|
||||
$response->assertUnprocessable();
|
||||
}
|
||||
|
||||
public function test_cannot_cancel_past_assignment(): void
|
||||
{
|
||||
$pastSlot = TimeSlot::factory()->create([
|
||||
'event_id' => $this->event->id,
|
||||
'person_type' => 'VOLUNTEER',
|
||||
'date' => now()->subMonth(),
|
||||
]);
|
||||
$shift = $this->createOpenShift(['time_slot_id' => $pastSlot->id]);
|
||||
$assignment = ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $pastSlot->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($this->volunteer);
|
||||
|
||||
$response = $this->postJson(
|
||||
"/api/v1/portal/events/{$this->event->id}/assignments/{$assignment->id}/cancel",
|
||||
);
|
||||
|
||||
$response->assertUnprocessable();
|
||||
}
|
||||
|
||||
public function test_cancel_unauthenticated_returns_401(): void
|
||||
{
|
||||
$shift = $this->createOpenShift();
|
||||
$assignment = ShiftAssignment::factory()->approved()->create([
|
||||
'shift_id' => $shift->id,
|
||||
'person_id' => $this->person->id,
|
||||
'time_slot_id' => $this->timeSlot->id,
|
||||
]);
|
||||
|
||||
$response = $this->postJson(
|
||||
"/api/v1/portal/events/{$this->event->id}/assignments/{$assignment->id}/cancel",
|
||||
);
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user