Skip to content

A free open source IT asset/license management system

Send webhook notifications for accessory checkins when enabled

Marcus Moore3cc3662992e3
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index 2133876789..a46c47e0e0 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -36,7 +36,7 @@ class CheckoutableListener
// @todo: comment...we send this anonymously so that webhook notification still
// @todo: get sent for models that don't have email addresses associated...
- if (Setting::getSettings() && Setting::getSettings()->webhook_endpoint) {
+ if ($this->shouldSendWebhookNotification()) {
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckoutNotification($event));
}
@@ -71,11 +71,13 @@ class CheckoutableListener
{
\Log::debug('onCheckedIn in the Checkoutable listener fired');
- /**
- * When the item wasn't checked out to a user, we can't send notifications
- */
- if (! $event->checkedOutTo instanceof User) {
- return;
+ // @todo: update docblock
+
+ // @todo: comment...we send this anonymously so that webhook notification still
+ // @todo: get sent for models that don't have email addresses associated...
+ if ($this->shouldSendWebhookNotification()) {
+ Notification::route('slack', Setting::getSettings()->webhook_endpoint)
+ ->notify($this->getCheckinNotification($event));
}
/**
@@ -227,4 +229,9 @@ class CheckoutableListener
'App\Listeners\CheckoutableListener@onCheckedOut'
);
}
+
+ private function shouldSendWebhookNotification(): bool
+ {
+ return Setting::getSettings() && Setting::getSettings()->webhook_endpoint;
+ }
}
diff --git a/tests/Feature/Notifications/AccessoryWebhookTest.php b/tests/Feature/Notifications/AccessoryWebhookTest.php
index 967faf9c86..8272202d8c 100644
--- a/tests/Feature/Notifications/AccessoryWebhookTest.php
+++ b/tests/Feature/Notifications/AccessoryWebhookTest.php
@@ -2,10 +2,12 @@
namespace Tests\Feature\Notifications;
+use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Accessory;
use App\Models\Setting;
use App\Models\User;
+use App\Notifications\CheckinAccessoryNotification;
use App\Notifications\CheckoutAccessoryNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
@@ -50,4 +52,42 @@ class AccessoryWebhookTest extends TestCase
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAccessoryNotification::class);
}
+
+ public function testAccessoryCheckinSendsWebhookNotificationWhenSettingEnabled()
+ {
+ Notification::fake();
+
+ Setting::factory()->withWebhookEnabled()->create();
+
+ event(new CheckoutableCheckedIn(
+ Accessory::factory()->appleBtKeyboard()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ ''
+ ));
+
+ Notification::assertSentTo(
+ new AnonymousNotifiable,
+ CheckinAccessoryNotification::class,
+ function ($notification, $channels, $notifiable) {
+ return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
+ }
+ );
+ }
+
+ public function testAccessoryCheckinDoesNotSendWebhookNotificationWhenSettingDisabled()
+ {
+ Notification::fake();
+
+ Setting::factory()->withWebhookDisabled()->create();
+
+ event(new CheckoutableCheckedIn(
+ Accessory::factory()->appleBtKeyboard()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ ''
+ ));
+
+ Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAccessoryNotification::class);
+ }
}