From c40209f5000aaf9c6ec3449cec6463d956dd19bb Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 18 Sep 2024 15:23:44 -0700 Subject: [PATCH 1/3] seperates emails and webhook notifications --- app/Listeners/CheckoutableListener.php | 75 ++++++++++++++------------ 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 5968632fdc..5c7e772976 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -43,30 +43,31 @@ class CheckoutableListener * Make a checkout acceptance and attach it in the notification */ $acceptance = $this->getCheckoutAcceptance($event); + $notifiables = $this->getNotifiables($event); + // Send email notifications try { - if (! $event->checkedOutTo->locale) { - Notification::locale(Setting::getSettings()->locale)->send( - $this->getNotifiables($event), - $this->getCheckoutNotification($event, $acceptance) - ); - } else { - Notification::send( - $this->getNotifiables($event), - $this->getCheckoutNotification($event, $acceptance) - ); + foreach ($notifiables as $notifiable) { + if ($notifiable instanceof User && $notifiable->email != '') { + if (! $event->checkedOutTo->locale){ + Notification::locale(Setting::getSettings()->locale)->send($notifiables, $this->getCheckoutNotification($event, $acceptance)); + } + else { + Notification::send($notifiable, $this->getCheckoutNotification($event, $acceptance)); + } + } } + // Send Webhook notification if ($this->shouldSendWebhookNotification()) { - - //slack doesn't include the url in its messaging format so this is needed to hit the endpoint - - if(Setting::getSettings()->webhook_selected =='slack' || Setting::getSettings()->webhook_selected =='general') { - - - Notification::route('slack', Setting::getSettings()->webhook_endpoint) - ->notify($this->getCheckoutNotification($event)); - } + // Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint + if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') { + Notification::route('slack', Setting::getSettings()->webhook_endpoint) + ->notify($this->getCheckoutNotification($event)); + } else { + Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint) + ->notify($this->getCheckoutNotification($event, $acceptance)); + } } } catch (ClientException $e) { Log::debug("Exception caught during checkout notification: " . $e->getMessage()); @@ -75,6 +76,7 @@ class CheckoutableListener } } + /** * Notify the user and post to webhook about the checked in checkoutable */ @@ -100,26 +102,29 @@ class CheckoutableListener } } } - + $acceptance = $this->getCheckoutAcceptance($event); + $notifiables = $this->getNotifiables($event); + // Send email notifications try { - // Use default locale - if (! $event->checkedOutTo->locale) { - Notification::locale(Setting::getSettings()->locale)->send( - $this->getNotifiables($event), - $this->getCheckinNotification($event) - ); - } else { - Notification::send( - $this->getNotifiables($event), - $this->getCheckinNotification($event) - ); + foreach ($notifiables as $notifiable) { + if ($notifiable instanceof User && $notifiable->email != '') { + if (! $event->checkedOutTo->locale){ + Notification::locale(Setting::getSettings()->locale)->send($notifiables, $this->getCheckoutNotification($event, $acceptance)); + } + else { + Notification::send($notifiable, $this->getCheckinNotification($event, $acceptance)); + } + } } - //slack doesn't include the url in its messaging format so this is needed to hit the endpoint - if(Setting::getSettings()->webhook_selected =='slack' || Setting::getSettings()->webhook_selected =='general') { - - if ($this->shouldSendWebhookNotification()) { + // Send Webhook notification + if ($this->shouldSendWebhookNotification()) { + // Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint + if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') { Notification::route('slack', Setting::getSettings()->webhook_endpoint) ->notify($this->getCheckinNotification($event)); + } else { + Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint) + ->notify($this->getCheckinNotification($event, $acceptance)); } } From 4ff5fc1ff9528e10218106db85902c644afedc4c Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 18 Sep 2024 16:19:35 -0700 Subject: [PATCH 2/3] clean up variables --- app/Listeners/CheckoutableListener.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 5c7e772976..6f9e867af1 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -50,7 +50,7 @@ class CheckoutableListener foreach ($notifiables as $notifiable) { if ($notifiable instanceof User && $notifiable->email != '') { if (! $event->checkedOutTo->locale){ - Notification::locale(Setting::getSettings()->locale)->send($notifiables, $this->getCheckoutNotification($event, $acceptance)); + Notification::locale(Setting::getSettings()->locale)->send($notifiable, $this->getCheckoutNotification($event, $acceptance)); } else { Notification::send($notifiable, $this->getCheckoutNotification($event, $acceptance)); @@ -63,7 +63,7 @@ class CheckoutableListener // Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') { Notification::route('slack', Setting::getSettings()->webhook_endpoint) - ->notify($this->getCheckoutNotification($event)); + ->notify($this->getCheckoutNotification($event, $acceptance)); } else { Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint) ->notify($this->getCheckoutNotification($event, $acceptance)); @@ -102,17 +102,17 @@ class CheckoutableListener } } } - $acceptance = $this->getCheckoutAcceptance($event); + $notifiables = $this->getNotifiables($event); // Send email notifications try { foreach ($notifiables as $notifiable) { if ($notifiable instanceof User && $notifiable->email != '') { if (! $event->checkedOutTo->locale){ - Notification::locale(Setting::getSettings()->locale)->send($notifiables, $this->getCheckoutNotification($event, $acceptance)); + Notification::locale(Setting::getSettings()->locale)->send($notifiable, $this->getCheckoutNotification($event, $acceptance)); } else { - Notification::send($notifiable, $this->getCheckinNotification($event, $acceptance)); + Notification::send($notifiable, $this->getCheckinNotification($event)); } } } @@ -124,7 +124,7 @@ class CheckoutableListener ->notify($this->getCheckinNotification($event)); } else { Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint) - ->notify($this->getCheckinNotification($event, $acceptance)); + ->notify($this->getCheckinNotification($event)); } } From b32ab6a06ce226562add8a8654dd9307efe6abea Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 18 Sep 2024 16:36:24 -0700 Subject: [PATCH 3/3] import User model --- app/Listeners/CheckoutableListener.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 6f9e867af1..eb6b738094 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -11,6 +11,7 @@ use App\Models\Consumable; use App\Models\LicenseSeat; use App\Models\Recipients\AdminRecipient; use App\Models\Setting; +use App\Models\User; use App\Notifications\CheckinAccessoryNotification; use App\Notifications\CheckinAssetNotification; use App\Notifications\CheckinLicenseSeatNotification;