added a coulple test adjustments and moved mail send logic to listener

This commit is contained in:
Godfrey M 2024-10-17 12:05:36 -07:00
parent 9a79483375
commit 285d6897cf
6 changed files with 57 additions and 124 deletions

View file

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

View file

@ -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
*/
}

View file

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

View file

@ -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())
<center><img src="{{ $item->getImageUrl() }}" alt="Asset" style="max-width: 570px;"></center>
@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

View file

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

View file

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