Tour-Guide bei An- und Abmeldungen benachrichtigen

This commit is contained in:
torsten 2026-11-12 19:22:10 +01:00
parent 397ff1c645
commit 7413aeffdc
2 changed files with 71 additions and 3 deletions

View file

@ -252,6 +252,40 @@ class TourController
}
}
/**
* Informiert den Tour Guide über eine An- oder Abmeldung in seiner Tour.
*
* Empfänger ist ausschließlich der eingetragene Guide (alle seine Geräte
* mit aktivierter App-Benachrichtigung). Löst der Guide die Änderung selbst
* aus, wird nichts versendet.
*/
private function notifyGuideBooking(Tour $tour, string $participantName, bool $joined): void
{
$guideId = (int) $tour->tour_guide_id;
if ($guideId <= 0 || $guideId === Auth::id() || $participantName === '') {
return;
}
try {
PushSubscription::sendToUsers([$guideId], [
'title' => ($joined ? 'Neue Anmeldung: ' : 'Abmeldung: ') . $tour->name,
'body' => $participantName . ($joined ? ' hat sich angemeldet.' : ' hat sich abgemeldet.'),
'url' => '/tour/' . (int) $tour->id,
// Bewusst ohne "tag": mehrere An-/Abmeldungen sollen sich in der
// Statusleiste nicht gegenseitig ersetzen.
]);
} catch (\Throwable $e) {
app_log('error', 'Push-Benachrichtigung zum Buchungsstatus fehlgeschlagen', [
'tour_id' => (int) $tour->id,
'guide_id' => $guideId,
'joined' => $joined,
'exception' => $e::class,
'error' => $e->getMessage(),
]);
}
}
public function show(string $id): void
{
$tour = Tour::find((int) $id);
@ -699,6 +733,7 @@ class TourController
Booking::joinRoom((int) $tour->id, (int) $user->id, $roomId);
$this->postChatSystem((int) $tour->id, $user->name . ' hat die Tour gebucht.');
$this->notifyGuideBooking($tour, $user->name, true);
redirect('/tour/' . $tour_id);
}
@ -718,11 +753,17 @@ class TourController
redirect('/tour/' . $tour_id . '/edit');
}
$wasBooked = Booking::forUser((int) $tour->id, $userId) !== null;
Booking::joinRoom((int) $tour->id, $userId, $roomId);
$participant = User::find($userId);
if ($participant !== null) {
if (!$wasBooked) {
// Nur eine echte Neuaufnahme, kein reiner Zimmerwechsel.
$this->notifyGuideBooking($tour, $participant->name, true);
}
if ($roomId > 0) {
$room = TourRoom::findForTour($roomId, (int) $tour->id);
$roomName = $room !== null ? (string) $room['name'] : '';
@ -747,25 +788,41 @@ class TourController
public function cancelBooking(string $tour_id): void
{
if (!$this->canManageTour(Tour::find((int) $tour_id))) {
$tour = Tour::find((int) $tour_id);
$user = Auth::user();
if (!$this->canManageTour($tour)) {
redirect('/tour');
}
Booking::deleteForUser((int) $tour_id, (int) Auth::id());
if ($tour !== null && $user !== null
&& Booking::forUser((int) $tour->id, (int) $user->id) !== null) {
Booking::deleteForUser((int) $tour->id, (int) $user->id);
$this->notifyGuideBooking($tour, $user->name, false);
}
redirect('/tour');
}
public function cancelBookingAdmin(string $tour_id, string $user_id): void
{
if (!$this->canManageTour(Tour::find((int) $tour_id))) {
$tour = Tour::find((int) $tour_id);
if (!$this->canManageTour($tour)) {
redirect('/tour/' . $tour_id);
}
$participant = User::find((int) $user_id);
$hadBooking = Booking::forUser((int) $tour_id, (int) $user_id) !== null;
Booking::deleteForUser((int) $tour_id, (int) $user_id);
if ($participant !== null) {
$this->postChatSystem((int) $tour_id, $participant->name . ' wurde aus der Tour entfernt.');
if ($hadBooking && $tour !== null) {
$this->notifyGuideBooking($tour, $participant->name, false);
}
}
redirect('/tour/' . $tour_id);