handle some edge cases, null values clean up variable names

This commit is contained in:
Godfrey M 2024-10-15 14:01:28 -07:00
parent f8476f7133
commit 9f06a0e441
2 changed files with 29 additions and 19 deletions

View file

@ -4,6 +4,7 @@ namespace App\Listeners;
use App\Events\CheckoutableCheckedOut; use App\Events\CheckoutableCheckedOut;
use App\Mail\CheckoutAssetMail; use App\Mail\CheckoutAssetMail;
use App\Mail\CheckinAssetMail;
use App\Models\Accessory; use App\Models\Accessory;
use App\Models\Asset; use App\Models\Asset;
use App\Models\CheckoutAcceptance; use App\Models\CheckoutAcceptance;
@ -14,7 +15,6 @@ use App\Models\Recipients\AdminRecipient;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use App\Notifications\CheckinAccessoryNotification; use App\Notifications\CheckinAccessoryNotification;
use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckinLicenseSeatNotification; use App\Notifications\CheckinLicenseSeatNotification;
use App\Notifications\CheckoutAccessoryNotification; use App\Notifications\CheckoutAccessoryNotification;
use App\Notifications\CheckoutAssetNotification; use App\Notifications\CheckoutAssetNotification;
@ -46,7 +46,7 @@ class CheckoutableListener
* Make a checkout acceptance and attach it in the notification * Make a checkout acceptance and attach it in the notification
*/ */
$acceptance = $this->getCheckoutAcceptance($event); $acceptance = $this->getCheckoutAcceptance($event);
$notifiable = $this->getNotifiables($event); $notifiable = $this->getNotifiable($event);
$mailable = (new CheckoutAssetMail( $mailable = (new CheckoutAssetMail(
$event->checkoutable, $event->checkoutable,
$event->checkedOutTo, $event->checkedOutTo,
@ -61,7 +61,7 @@ class CheckoutableListener
$mailable->locale($event->checkedOutTo->locale); $mailable->locale($event->checkedOutTo->locale);
} }
Mail::to($notifiable)->send($mailable); Mail::to($notifiable)->send($mailable);
\Log::info('Sending email, Locale: ' .($event->checkedOutTo->locale ?? 'default'));
// Send Webhook notification // Send Webhook notification
if ($this->shouldSendWebhookNotification()) { if ($this->shouldSendWebhookNotification()) {
// Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint // Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint
@ -107,19 +107,22 @@ class CheckoutableListener
} }
} }
$notifiables = $this->getNotifiables($event); $notifiable = $this->getNotifiable($event);
$mailable = (new CheckInAssetMail(
$event->checkoutable,
$event->checkedOutTo,
$event->checkedOutBy,
$event->note,
null,
));
// Send email notifications // Send email notifications
try { try {
foreach ($notifiables as $notifiable) {
if ($notifiable instanceof User && $notifiable->email != '') {
if (!$event->checkedOutTo->locale){ if (!$event->checkedOutTo->locale){
Notification::locale(Setting::getSettings()->locale)->send($notifiable, $this->getCheckoutNotification($event, $acceptance)); $mailable->locale($event->checkedOutTo->locale);
}
else {
Notification::send($notifiable, $this->getCheckinNotification($event));
}
}
} }
Mail::to($notifiable)->send($mailable);
\Log::info('Sending email, Locale: ' .$event->checkedOutTo->locale);
// Send Webhook notification // Send Webhook notification
if ($this->shouldSendWebhookNotification()) { if ($this->shouldSendWebhookNotification()) {
// Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint // Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint
@ -168,25 +171,26 @@ class CheckoutableListener
* @param Event $event * @param Event $event
* @return Collection * @return Collection
*/ */
private function getNotifiables($event) private function getNotifiable($event)
{ {
$notifiables = collect(); $notifiable = collect();
/** /**
* Notify who checked out the item as long as the model can route notifications * Notify who checked out the item as long as the model can route notifications
*/ */
if (method_exists($event->checkedOutTo, 'routeNotificationFor')) { if (method_exists($event->checkedOutTo, 'routeNotificationFor')) {
$notifiables->push($event->checkedOutTo); $notifiable->push($event->checkedOutTo);
} }
/** /**
* Notify Admin users if the settings is activated * Notify Admin users if the settings is activated
*/ */
if ((Setting::getSettings()) && (Setting::getSettings()->admin_cc_email != '')) { if ((Setting::getSettings()) && (Setting::getSettings()->admin_cc_email != '')) {
$notifiables->push(new AdminRecipient()); $adminRecipient= new AdminRecipient;
$notifiable->push($adminRecipient->getEmail());
} }
return $notifiables; return new $notifiable;
} }
/** /**

View file

@ -6,9 +6,15 @@ use App\Models\Setting;
class AdminRecipient extends Recipient class AdminRecipient extends Recipient
{ {
protected $email;
public function __construct() public function __construct()
{ {
$settings = Setting::getSettings(); $settings = Setting::getSettings();
$this->email = trim($settings->admin_cc_email); $this->email = trim($settings->admin_cc_email);
} }
public function getEmail(){
return $this->email;
}
} }