Send webhook notifications for accessory checkins when enabled

This commit is contained in:
Marcus Moore 2023-04-05 12:36:24 -07:00
parent 2187310abb
commit 3cc3662992
No known key found for this signature in database
2 changed files with 53 additions and 6 deletions

View file

@ -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;
}
}

View file

@ -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);
}
}