diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 44987e20d..3e23259fb 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -57,8 +57,21 @@ class CheckoutableListener if (!$event->checkedOutTo->locale){ $mailable->locale($event->checkedOutTo->locale); } + + /** + * Send an email if any of the following conditions are met: + * 1. The asset requires acceptance + * 2. The item has a EULA + * 3. The item should send an email at check-in/check-out + */ + + if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() || + (method_exists($event->checkoutable, 'checkin_email') && $event->checkoutable->checkin_email())) { + Mail::to($notifiable)->send($mailable); Log::info('Sending email, Locale: ' .($event->checkedOutTo->locale ?? 'default')); + } + // Send Webhook notification if ($this->shouldSendWebhookNotification()) { Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint) diff --git a/app/Notifications/CheckoutAssetNotification.php b/app/Notifications/CheckoutAssetNotification.php index 1ca329ed8..97706cec9 100644 --- a/app/Notifications/CheckoutAssetNotification.php +++ b/app/Notifications/CheckoutAssetNotification.php @@ -9,7 +9,6 @@ use App\Models\User; use Exception; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Channels\SlackWebhookChannel; -use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; use NotificationChannels\GoogleChat\Card; @@ -154,12 +153,4 @@ public function toGoogleChat() ); } - - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage - */ - } diff --git a/app/Notifications/CheckoutConsumableNotification.php b/app/Notifications/CheckoutConsumableNotification.php index 0a2733689..3f2adc333 100644 --- a/app/Notifications/CheckoutConsumableNotification.php +++ b/app/Notifications/CheckoutConsumableNotification.php @@ -65,33 +65,33 @@ class CheckoutConsumableNotification extends Notification $notifyBy[] = SlackWebhookChannel::class; } -// /** -// * Only send notifications to users that have email addresses -// */ -// if ($this->target instanceof User && $this->target->email != '') { -// -// /** -// * Send an email if the asset requires acceptance, -// * so the user can accept or decline the asset -// */ -// if ($this->item->requireAcceptance()) { -// $notifyBy[1] = 'mail'; -// } -// -// /** -// * Send an email if the item has a EULA, since the user should always receive it -// */ -// if ($this->item->getEula()) { -// $notifyBy[1] = 'mail'; -// } -// -// /** -// * Send an email if an email should be sent at checkin/checkout -// */ -// if ((method_exists($this->item, 'checkin_email')) && ($this->item->checkin_email())) { -// $notifyBy[1] = 'mail'; -// } -// } + /** + * Only send notifications to users that have email addresses + */ + if ($this->target instanceof User && $this->target->email != '') { + + /** + * Send an email if the asset requires acceptance, + * so the user can accept or decline the asset + */ + if ($this->item->requireAcceptance()) { + $notifyBy[1] = 'mail'; + } + + /** + * Send an email if the item has a EULA, since the user should always receive it + */ + if ($this->item->getEula()) { + $notifyBy[1] = 'mail'; + } + + /** + * Send an email if an email should be sent at checkin/checkout + */ + if ((method_exists($this->item, 'checkin_email')) && ($this->item->checkin_email())) { + $notifyBy[1] = 'mail'; + } + } return $notifyBy; } diff --git a/resources/views/notifications/markdown/checkout-asset.blade.php b/resources/views/notifications/markdown/checkout-asset.blade.php deleted file mode 100644 index 5b4c811dd..000000000 --- a/resources/views/notifications/markdown/checkout-asset.blade.php +++ /dev/null @@ -1,76 +0,0 @@ -@component('mail::message') - # {{ trans('mail.hello') }} {{ $target->present()->fullName() }}, - - {{ trans('mail.new_item_checked') }} - - @if (($snipeSettings->show_images_in_email =='1') && $item->getImageUrl()) -
Asset
- @endif - - @component('mail::table') - | | | - | ------------- | ------------- | - @if ((isset($item->name)) && ($item->name!='')) - | **{{ trans('mail.asset_name') }}** | {{ $item->name }} | - @endif - @if (($item->name!=$item->asset_tag)) - | **{{ trans('mail.asset_tag') }}** | {{ $item->asset_tag }} | - @endif - @if (isset($item->manufacturer)) - | **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} | - @endif - @if (isset($item->model)) - | **{{ trans('general.asset_model') }}** | {{ $item->model->name }} | - @endif - @if ((isset($item->model->model_number)) && ($item->model->name!=$item->model->model_number)) - | **{{ trans('general.model_no') }}** | {{ $item->model->model_number }} | - @endif - @if (isset($item->serial)) - | **{{ trans('mail.serial') }}** | {{ $item->serial }} | - @endif - @if (isset($last_checkout)) - | **{{ trans('mail.checkout_date') }}** | {{ $last_checkout }} | - @endif - @if (isset($status)) - | **{{ trans('general.status') }}** | {{ $status }} | - @endif - @if ((isset($expected_checkin)) && ($expected_checkin!='')) - | **{{ trans('mail.expecting_checkin_date') }}** | {{ $expected_checkin }} | - @endif - @foreach($fields as $field) - @if (($item->{ $field->db_column_name() }!='') && ($field->show_in_email) && ($field->field_encrypted=='0')) - | **{{ $field->name }}** | {{ $item->{ $field->db_column_name() } }} | - @endif - @endforeach - @if ($admin) - | **{{ trans('general.administrator') }}** | {{ $admin->present()->fullName() }} | - @endif - @if ($note) - | **{{ trans('mail.additional_notes') }}** | {{ $note }} | - @endif - @endcomponent - - @if (($req_accept == 1) && ($eula!='')) - {{ trans('mail.read_the_terms_and_click') }} - @elseif (($req_accept == 1) && ($eula=='')) - {{ trans('mail.click_on_the_link_asset') }} - @elseif (($req_accept == 0) && ($eula!='')) - {{ trans('mail.read_the_terms') }} - @endif - - @if ($eula) - @component('mail::panel') - {!! $eula !!} - @endcomponent - @endif - - @if ($req_accept == 1) - **[✔ {{ trans('mail.i_have_read') }}]({{ $accept_url }})** - @endif - - - {{ trans('mail.best_regards') }} - - {{ $snipeSettings->site_name }} - -@endcomponent \ No newline at end of file diff --git a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php index 484b7e72c..46c34aedd 100644 --- a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php +++ b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php @@ -8,6 +8,7 @@ use App\Models\Component; use App\Models\Consumable; use App\Models\User; use App\Notifications\CheckoutConsumableNotification; +use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Notification; use Tests\TestCase; @@ -53,7 +54,7 @@ class ConsumableCheckoutTest extends TestCase public function testUserSentNotificationUponCheckout() { - Notification::fake(); + Mail::fake(); $consumable = Consumable::factory()->create(); $user = User::factory()->create(); @@ -63,7 +64,9 @@ class ConsumableCheckoutTest extends TestCase 'assigned_to' => $user->id, ]); - Notification::assertSentTo($user, CheckoutConsumableNotification::class); + Mail::assertSent(CheckoutConsumableNotification::class, function ($mail) use ($consumable, $user) { + return $mail->hasTo($user) && $mail->consumables->contains($consumable); + }); } public function testActionLogCreatedUponCheckout() diff --git a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php index 449f65c7a..c7c239b7a 100644 --- a/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php +++ b/tests/Feature/Notifications/Email/EmailNotificationsUponCheckinTest.php @@ -2,6 +2,8 @@ namespace Tests\Feature\Notifications\Email; +use App\Mail\CheckinAssetMail; +use Illuminate\Support\Facades\Mail; use PHPUnit\Framework\Attributes\Group; use App\Events\CheckoutableCheckedIn; use App\Models\Asset; @@ -22,6 +24,8 @@ class EmailNotificationsUponCheckinTest extends TestCase public function testCheckInEmailSentToUserIfSettingEnabled() { + Mail::fake(); + $user = User::factory()->create(); $asset = Asset::factory()->assignedToUser($user)->create(); @@ -29,16 +33,16 @@ class EmailNotificationsUponCheckinTest extends TestCase $this->fireCheckInEvent($asset, $user); - Notification::assertSentTo( - $user, - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); - }, - ); + Mail::assertSent(CheckinAssetMail::class, function($mail) use ($user, $asset) { + return $mail->hasTo($user->email) && $mail->event->checkoutable->id === $asset->id; + }); + } public function testCheckInEmailNotSentToUserIfSettingDisabled() { + Mail::fake(); + $user = User::factory()->create(); $asset = Asset::factory()->assignedToUser($user)->create(); @@ -46,10 +50,8 @@ class EmailNotificationsUponCheckinTest extends TestCase $this->fireCheckInEvent($asset, $user); - Notification::assertNotSentTo( - $user, - function (CheckinAssetNotification $notification, $channels) { - return in_array('mail', $channels); + Mail::assertNotSent(CheckinAssetMail::class, function($mail) use ($user, $asset) { + return $mail->hasTo($user->email) && $mail->event->checkoutable->id === $asset->id; } ); }