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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user