From c8b5b3f1767357ca7930ff6301d1d7f24318f988 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 16 Apr 2025 09:51:44 -0700 Subject: [PATCH] adds a check for category checkin/out emails, also updates our test --- app/Listeners/CheckoutableListener.php | 18 ++++++- .../EmailNotificationsUponCheckinTest.php | 52 +++++++++++++++---- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index a13b0dd43..2bf93afc9 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -385,9 +385,25 @@ class CheckoutableListener private function shouldNotSendAnyNotifications($checkoutable): bool { - return in_array(get_class($checkoutable), $this->skipNotificationsFor); + if(in_array(get_class($checkoutable), $this->skipNotificationsFor)) { + return true; + } + //runs a check if the category wants to send checkin/checkout emails to users + $category = match (true) { + $checkoutable instanceof Asset => $checkoutable->model->category, + $checkoutable instanceof Accessory, + $checkoutable instanceof Consumable => $checkoutable->category, + $checkoutable instanceof LicenseSeat => $checkoutable->license->category, + default => null, + }; + + if (!$category->checkin_email) { + return true; + } + return false; } + private function shouldSendWebhookNotification(): bool { return Setting::getSettings() && Setting::getSettings()->webhook_endpoint; diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php index 93235789c..457ff4f86 100644 --- a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php @@ -3,6 +3,9 @@ namespace Tests\Feature\Notifications\Email; use App\Mail\CheckinAssetMail; +use App\Models\Accessory; +use App\Models\Consumable; +use App\Models\LicenseSeat; use Illuminate\Support\Facades\Mail; use PHPUnit\Framework\Attributes\Group; use App\Events\CheckoutableCheckedIn; @@ -45,20 +48,49 @@ class EmailNotificationsUponCheckinTest extends TestCase Mail::fake(); $user = User::factory()->create(); - $asset = Asset::factory()->assignedToUser($user)->create(); - - $asset->model->category->update([ - 'checkin_email' => false, - 'eula_text' => null, - 'require_acceptance' => false, + $checkoutables = collect([ + Asset::factory()->assignedToUser($user)->create(), + LicenseSeat::factory()->assignedToUser($user)->create(), + Accessory::factory()->checkedOutToUser($user)->create(), + Consumable::factory()->checkedOutToUser($user)->create(), ]); - $this->fireCheckInEvent($asset, $user); + foreach ($checkoutables as $checkoutable) { - Mail::assertNotSent(CheckinAssetMail::class, function($mail) use ($user) { - return $mail->hasTo($user->email); + if ($checkoutable instanceof Asset) { + $checkoutable->model->category->update([ + 'checkin_email' => false, + 'eula_text' => null, + 'require_acceptance' => false, + ]); + $checkoutable = $checkoutable->fresh(['model.category']); } - ); + + if ($checkoutable instanceof Accessory || $checkoutable instanceof \App\Models\Consumable) { + $checkoutable->category->update([ + 'checkin_email' => false, + 'eula_text' => null, + 'require_acceptance' => false, + ]); + $checkoutable = $checkoutable->fresh(['category']); + } + + if ($checkoutable instanceof LicenseSeat) { + $checkoutable->license->category->update([ + 'checkin_email' => false, + 'eula_text' => null, + 'require_acceptance' => false, + ]); + $checkoutable = $checkoutable->fresh(['license.category']); + } + + // Fire event manually + $this->fireCheckInEvent($checkoutable, $user); + } + + Mail::assertNotSent(CheckinAssetMail::class, function ($mail) use ($user) { + return $mail->hasTo($user->email); + }); } private function fireCheckInEvent($asset, $user): void