Skip to content

A free open source IT asset/license management system

Skip notifications for component checkouts and checkins

Marcus Moore508660b1df1b
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index c990dc211e..01d74df85d 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -5,6 +5,7 @@ namespace App\Listeners;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
+use App\Models\Component;
use App\Models\Consumable;
use App\Models\LicenseSeat;
use App\Models\Recipients\AdminRecipient;
@@ -22,12 +23,20 @@ use Log;
class CheckoutableListener
{
+ private array $skipNotificationsFor = [
+ Component::class,
+ ];
+
/**
* Notify the user and post to webhook about the checked out checkoutable and add a record to the
* checkout_requests table.
*/
public function onCheckedOut($event)
{
+ if ($this->shouldNotSendAnyNotifications($event->checkoutable)){
+ return;
+ }
+
// Send an anonymous webhook notification if setting enabled
if ($this->shouldSendWebhookNotification()) {
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
@@ -64,6 +73,10 @@ class CheckoutableListener
{
\Log::debug('onCheckedIn in the Checkoutable listener fired');
+ if ($this->shouldNotSendAnyNotifications($event->checkoutable)) {
+ return;
+ }
+
// Send an anonymous webhook notification if setting enabled
if ($this->shouldSendWebhookNotification()) {
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
@@ -220,6 +233,11 @@ class CheckoutableListener
);
}
+ private function shouldNotSendAnyNotifications($checkoutable): bool
+ {
+ return in_array(get_class($checkoutable), $this->skipNotificationsFor);
+ }
+
private function shouldSendWebhookNotification(): bool
{
return Setting::getSettings() && Setting::getSettings()->webhook_endpoint;
diff --git a/tests/Feature/Notifications/ComponentWebhookTest.php b/tests/Feature/Notifications/ComponentWebhookTest.php
new file mode 100644
index 0000000000..8f3a51b15d
--- /dev/null
+++ b/tests/Feature/Notifications/ComponentWebhookTest.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Tests\Feature\Notifications;
+
+use App\Events\CheckoutableCheckedIn;
+use App\Events\CheckoutableCheckedOut;
+use App\Models\Asset;
+use App\Models\Component;
+use App\Models\User;
+use Illuminate\Support\Facades\Notification;
+use Tests\Support\InteractsWithSettings;
+use Tests\TestCase;
+
+class ComponentWebhookTest extends TestCase
+{
+
+ use InteractsWithSettings;
+
+ public function testComponentCheckoutDoesNotSendWebhookNotification()
+ {
+ Notification::fake();
+
+ $this->settings->enableWebhook();
+
+ event(new CheckoutableCheckedOut(
+ Component::factory()->ramCrucial8()->create(),
+ Asset::factory()->laptopMbp()->create(),
+ User::factory()->superuser()->create(),
+ ''
+ ));
+
+ Notification::assertNothingSent();
+ }
+
+ public function testComponentCheckinDoesNotSendWebhookNotification()
+ {
+ Notification::fake();
+
+ $this->settings->enableWebhook();
+
+ event(new CheckoutableCheckedIn(
+ Component::factory()->ramCrucial8()->create(),
+ Asset::factory()->laptopMbp()->create(),
+ User::factory()->superuser()->create(),
+ ''
+ ));
+
+ Notification::assertNothingSent();
+ }
+}