Skip notifications for component checkouts and checkins

This commit is contained in:
Marcus Moore 2023-04-18 13:07:55 -07:00
parent f6cff90829
commit 508660b1df
No known key found for this signature in database
2 changed files with 68 additions and 0 deletions

View file

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

View file

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