diff --git a/api/app/Http/Controllers/Api/V1/Portal/PortalShiftController.php b/api/app/Http/Controllers/Api/V1/Portal/PortalShiftController.php new file mode 100644 index 00000000..51d0c39c --- /dev/null +++ b/api/app/Http/Controllers/Api/V1/Portal/PortalShiftController.php @@ -0,0 +1,259 @@ +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; + } +} diff --git a/api/routes/api.php b/api/routes/api.php index 1d7b77d2..bdb6d912 100644 --- a/api/routes/api.php +++ b/api/routes/api.php @@ -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) diff --git a/api/tests/Feature/Api/V1/Portal/PortalShiftClaimingTest.php b/api/tests/Feature/Api/V1/Portal/PortalShiftClaimingTest.php new file mode 100644 index 00000000..ba01f987 --- /dev/null +++ b/api/tests/Feature/Api/V1/Portal/PortalShiftClaimingTest.php @@ -0,0 +1,627 @@ +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(); + } +} diff --git a/apps/portal/src/components/portal/StatusCard.vue b/apps/portal/src/components/portal/StatusCard.vue index 4d3463d1..e2df8242 100644 --- a/apps/portal/src/components/portal/StatusCard.vue +++ b/apps/portal/src/components/portal/StatusCard.vue @@ -94,12 +94,12 @@ const registeredLabel = computed(() => { sm="4" >
- Mijn Shifts + Mijn Diensten
Rooster bekijken @@ -111,14 +111,15 @@ const registeredLabel = computed(() => { sm="4" > -
- Shifts claimen +
+ Diensten claimen
- Binnenkort beschikbaar + Schrijf je in voor diensten
diff --git a/apps/portal/src/composables/api/usePortalShifts.ts b/apps/portal/src/composables/api/usePortalShifts.ts new file mode 100644 index 00000000..4aedb49b --- /dev/null +++ b/apps/portal/src/composables/api/usePortalShifts.ts @@ -0,0 +1,73 @@ +import { useQuery, useMutation, useQueryClient } from '@tanstack/vue-query' +import type { Ref } from 'vue' +import { apiClient } from '@/lib/axios' +import type { AvailableShiftsDay, MyShiftsResponse } from '@/types/portal-shift' + +interface ApiResponse { + data: T +} + +export function useAvailableShifts(eventId: Ref) { + return useQuery({ + queryKey: ['available-shifts', eventId], + queryFn: async () => { + const { data } = await apiClient.get>( + `/portal/events/${eventId.value}/available-shifts`, + ) + + return data.data + }, + enabled: () => !!eventId.value, + }) +} + +export function useMyShifts(eventId: Ref) { + return useQuery({ + queryKey: ['my-shifts', eventId], + queryFn: async () => { + const { data } = await apiClient.get>( + `/portal/events/${eventId.value}/my-shifts`, + ) + + return data.data + }, + enabled: () => !!eventId.value, + }) +} + +export function useClaimShift(eventId: Ref) { + const queryClient = useQueryClient() + + return useMutation({ + mutationFn: async (shiftId: string) => { + const { data } = await apiClient.post>( + `/portal/events/${eventId.value}/shifts/${shiftId}/claim`, + ) + + return data.data + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['available-shifts', eventId] }) + queryClient.invalidateQueries({ queryKey: ['my-shifts', eventId] }) + }, + }) +} + +export function useCancelAssignment(eventId: Ref) { + const queryClient = useQueryClient() + + return useMutation({ + mutationFn: async ({ assignmentId, reason }: { assignmentId: string; reason?: string }) => { + const { data } = await apiClient.post>( + `/portal/events/${eventId.value}/assignments/${assignmentId}/cancel`, + reason ? { reason } : {}, + ) + + return data.data + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['available-shifts', eventId] }) + queryClient.invalidateQueries({ queryKey: ['my-shifts', eventId] }) + }, + }) +} diff --git a/apps/portal/src/layouts/portal.vue b/apps/portal/src/layouts/portal.vue index 30743f20..d626d40b 100644 --- a/apps/portal/src/layouts/portal.vue +++ b/apps/portal/src/layouts/portal.vue @@ -1,24 +1,37 @@ + + diff --git a/apps/portal/src/pages/dashboard/my-shifts.vue b/apps/portal/src/pages/dashboard/my-shifts.vue new file mode 100644 index 00000000..e6b7fdfb --- /dev/null +++ b/apps/portal/src/pages/dashboard/my-shifts.vue @@ -0,0 +1,421 @@ + + + diff --git a/apps/portal/src/types/portal-shift.ts b/apps/portal/src/types/portal-shift.ts new file mode 100644 index 00000000..c7e7ba61 --- /dev/null +++ b/apps/portal/src/types/portal-shift.ts @@ -0,0 +1,50 @@ +export interface AvailableShiftsDay { + date: string + date_label: string + time_slots: AvailableShiftsTimeSlot[] +} + +export interface AvailableShiftsTimeSlot { + time_slot_id: string + name: string + start_time: string + end_time: string + shifts: AvailableShift[] +} + +export interface AvailableShift { + id: string + title: string + section_name: string + section_icon: string | null + location_name: string | null + slots_total: number + slots_open_for_claiming: number + slots_claimed: number + slots_available: number + has_conflict: boolean + conflict_reason: string | null + report_time: string | null + description: string | null +} + +export interface MyShiftsResponse { + upcoming: MyShiftAssignment[] + past: MyShiftAssignment[] + cancelled: MyShiftAssignment[] +} + +export interface MyShiftAssignment { + assignment_id: string + status: string + shift_title: string + section_name: string + section_icon: string | null + location_name: string | null + date: string + date_label: string + start_time: string + end_time: string + report_time: string | null + can_cancel: boolean +} diff --git a/apps/portal/typed-router.d.ts b/apps/portal/typed-router.d.ts index fbf5b241..d92b41b7 100644 --- a/apps/portal/typed-router.d.ts +++ b/apps/portal/typed-router.d.ts @@ -22,6 +22,8 @@ declare module 'vue-router/auto-routes' { 'not-found': RouteRecordInfo<'not-found', '/:path(.*)', { path: ParamValue }, { path: ParamValue }>, 'artist-advance': RouteRecordInfo<'artist-advance', '/advance/:token', { token: ParamValue }, { token: ParamValue }>, 'portal-dashboard': RouteRecordInfo<'portal-dashboard', '/dashboard', Record, Record>, + 'portal-claim-shifts': RouteRecordInfo<'portal-claim-shifts', '/dashboard/claim-shifts', Record, Record>, + 'portal-my-shifts': RouteRecordInfo<'portal-my-shifts', '/dashboard/my-shifts', Record, Record>, 'login': RouteRecordInfo<'login', '/login', Record, Record>, 'portal-profile': RouteRecordInfo<'portal-profile', '/profile', Record, Record>, 'volunteer-register': RouteRecordInfo<'volunteer-register', '/register/:eventSlug', { eventSlug: ParamValue }, { eventSlug: ParamValue }>,