registration_enabled && $event->is_published && $event->starts_at->isFuture() && ($event->registration_deadline === null || $event->registration_deadline->isFuture()); } /** * @throws RuntimeException wenn keine Anmeldung möglich ist */ public function register(Event $event, User $user, ?string $note = null): EventRegistration { if (! $this->isOpen($event)) { throw new RuntimeException('Für diesen Termin ist keine Anmeldung (mehr) möglich.'); } return DB::transaction(function () use ($event, $user, $note) { // Sperre auf den Termin, damit bei gleichzeitigen Anmeldungen niemand über die Grenze rutscht $event = Event::query()->lockForUpdate()->findOrFail($event->id); $existing = EventRegistration::query()->where('event_id', $event->id)->where('user_id', $user->id)->first(); if ($existing) { $existing->update(['note' => $note]); return $existing; } $status = $this->hasSpace($event) ? EventRegistration::CONFIRMED : ($event->waitlist ? EventRegistration::WAITLIST : null); if ($status === null) { throw new RuntimeException('Der Termin ist leider ausgebucht.'); } return EventRegistration::query()->create(['event_id' => $event->id, 'user_id' => $user->id, 'status' => $status, 'note' => $note]); }); } public function cancel(Event $event, User $user): void { $this->remove(EventRegistration::query()->where('event_id', $event->id)->where('user_id', $user->id)->first()); } public function remove(?EventRegistration $registration): void { if (! $registration) { return; } DB::transaction(function () use ($registration) { Event::query()->lockForUpdate()->find($registration->event_id); $registration->delete(); }); $this->fillFromWaitlist($registration->event); } /** * Freie Plätze an die Warteliste vergeben (nach Abmeldung oder mehr Plätzen). */ public function fillFromWaitlist(Event $event): void { $promoted = DB::transaction(function () use ($event) { $event = Event::query()->lockForUpdate()->findOrFail($event->id); $promoted = collect(); while ($this->hasSpace($event)) { $next = EventRegistration::query()->where('event_id', $event->id)->where('status', EventRegistration::WAITLIST)->oldest()->oldest('id')->first(); if (! $next) { break; } $next->update(['status' => EventRegistration::CONFIRMED]); $promoted->push($next); } return $promoted; }); if ($event->starts_at->isPast()) { return; } foreach ($promoted as $registration) { $registration->user?->notify(new PortalNotification( 'Sie sind nachgerückt: '.$event->title, 'Ein Platz ist frei geworden – Ihre Anmeldung ist jetzt bestätigt.', route('events.show', $event), 'calendar-check', )); } } public function confirmedCount(Event $event): int { return EventRegistration::query()->where('event_id', $event->id)->where('status', EventRegistration::CONFIRMED)->count(); } /** * Stand für die Anzeige: eigene Anmeldung, Plätze, Warteliste. * * @return array|null */ public function summary(Event $event, User $user): ?array { if (! $event->registration_enabled) { return null; } $mine = EventRegistration::query()->where('event_id', $event->id)->where('user_id', $user->id)->first(); $confirmed = $this->confirmedCount($event); $waiting = EventRegistration::query()->where('event_id', $event->id)->where('status', EventRegistration::WAITLIST); return [ 'enabled' => true, 'open' => $this->isOpen($event), 'deadline' => $event->registration_deadline?->toIso8601String(), 'capacity' => $event->capacity, 'confirmed' => $confirmed, 'free' => $event->capacity === null ? null : max(0, $event->capacity - $confirmed), 'waitlist' => $event->waitlist, 'waiting' => (clone $waiting)->count(), 'status' => $mine?->status, 'note' => $mine?->note, 'position' => $mine?->status === EventRegistration::WAITLIST ? (clone $waiting)->where(fn ($q) => $q->where('created_at', '<', $mine->created_at)->orWhere(fn ($q) => $q->where('created_at', $mine->created_at)->where('id', '<', $mine->id)))->count() + 1 : null, ]; } private function hasSpace(Event $event): bool { return $event->capacity === null || $this->confirmedCount($event) < $event->capacity; } }