diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 44e731b4b..6acedd4d9 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -3,6 +3,8 @@ namespace App\Listeners; use App\Events\CheckoutableCheckedOut; +use App\Mail\CheckinAccessoryMail; +use App\Mail\CheckoutAccessoryMail; use App\Mail\CheckoutAssetMail; use App\Mail\CheckinAssetMail; use App\Models\Accessory; @@ -47,14 +49,7 @@ class CheckoutableListener */ $acceptance = $this->getCheckoutAcceptance($event); $notifiable = $this->getNotifiable($event); - $mailable = (new CheckoutAssetMail( - $event->checkoutable, - $event->checkedOutTo, - $event->checkedOutBy, - $event->note, - $acceptance, - )); - + $mailable = $this->getCheckoutMailType($event, $acceptance); // Send email notifications try { if (!$event->checkedOutTo->locale){ @@ -102,12 +97,7 @@ class CheckoutableListener } $notifiable = $this->getNotifiable($event); - $mailable = (new CheckInAssetMail( - $event->checkoutable, - $event->checkedOutTo, - $event->checkedInBy, - $event->note, - )); + $mailable = $this->getCheckinMailType($event); // Send email notifications try { @@ -233,9 +223,32 @@ class CheckoutableListener break; } - return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note); } + private function getCheckoutMailType($event, $acceptance){ + $lookup = [ + Accessory::class => CheckoutAccessoryMail::class, + Asset::class => CheckoutAssetMail::class, +// Consumable::class => +// LicenseSeat::class => + ]; + $mailable= $lookup[get_class($event->checkoutable)]; + + return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $event->note, $acceptance); + + } + private function getCheckinMailType($event){ + $lookup = [ + Accessory::class => CheckinAccessoryMail::class, + Asset::class => CheckinAssetMail::class, +// Consumable::class => +// LicenseSeat::class => + ]; + $mailable= $lookup[get_class($event->checkoutable)]; + + return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note); + + } /** * Register the listeners for the subscriber. diff --git a/app/Mail/CheckoutAccessoryMail.php b/app/Mail/CheckoutAccessoryMail.php new file mode 100644 index 000000000..f7f90e8fe --- /dev/null +++ b/app/Mail/CheckoutAccessoryMail.php @@ -0,0 +1,98 @@ +item = $accessory; + $this->admin = $checkedOutBy; + $this->note = $note; + $this->checkout_qty = $accessory->checkout_qty; + $this->target = $checkedOutTo; + $this->acceptance = $acceptance; + $this->settings = Setting::getSettings(); + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + $from = null; + $cc = []; + + if (!empty(Setting::getSettings()->alert_email)) { + $from = new Address(Setting::getSettings()->alert_email); + } + if (!empty(Setting::getSettings()->admin_cc_email)) { + $cc[] = new Address(Setting::getSettings()->admin_cc_email); + } + return new Envelope( + from: $from ?? new Address('default@example.com', 'Default Sender'), + cc: $cc, + subject: (trans('mail.Accessory_Checkout_Notification')), + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + Log::debug($this->item->getImageUrl()); + $eula = $this->item->getEula(); + $req_accept = $this->item->requireAcceptance(); + + $accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance); + + // Check if the item has custom fields associated with it + if (($this->item->model) && ($this->item->model->fieldset)) { + $fields = $this->item->model->fieldset->fields; + } + + $accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance); + + return new Content( + markdown: 'mail.markdown.checkout-accessory', + with: [ + 'item' => $this->item, + 'admin' => $this->admin, + 'note' => $this->note, + 'target' => $this->target, + 'eula' => $eula, + 'req_accept' => $req_accept, + 'accept_url' => $accept_url, + 'checkout_qty' => $this->checkout_qty, + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Notifications/CheckinAccessoryNotification.php b/app/Notifications/CheckinAccessoryNotification.php index 7e033f187..d36b77a85 100644 --- a/app/Notifications/CheckinAccessoryNotification.php +++ b/app/Notifications/CheckinAccessoryNotification.php @@ -58,18 +58,18 @@ class CheckinAccessoryNotification extends Notification $notifyBy[] = 'slack'; } - /** - * Only send notifications to users that have email addresses - */ - if ($this->target instanceof User && $this->target->email != '') { - Log::debug('The target is a user'); - - if ($this->item->checkin_email()) { - $notifyBy[] = 'mail'; - } - } - - Log::debug('checkin_email on this category is '.$this->item->checkin_email()); +// /** +// * Only send notifications to users that have email addresses +// */ +// if ($this->target instanceof User && $this->target->email != '') { +// Log::debug('The target is a user'); +// +// if ($this->item->checkin_email()) { +// $notifyBy[] = 'mail'; +// } +// } +// +// Log::debug('checkin_email on this category is '.$this->item->checkin_email()); return $notifyBy; } @@ -143,23 +143,23 @@ class CheckinAccessoryNotification extends Notification } - /** - * Get the mail representation of the notification. - * - * @param mixed $notifiable - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail() - { - Log::debug('to email called'); - - return (new MailMessage)->markdown('notifications.markdown.checkin-accessory', - [ - 'item' => $this->item, - 'admin' => $this->admin, - 'note' => $this->note, - 'target' => $this->target, - ]) - ->subject(trans('mail.Accessory_Checkin_Notification')); - } +// /** +// * Get the mail representation of the notification. +// * +// * @param mixed $notifiable +// * @return \Illuminate\Notifications\Messages\MailMessage +// */ +// public function toMail() +// { +// Log::debug('to email called'); +// +// return (new MailMessage)->markdown('notifications.markdown.checkin-accessory', +// [ +// 'item' => $this->item, +// 'admin' => $this->admin, +// 'note' => $this->note, +// 'target' => $this->target, +// ]) +// ->subject(trans('mail.Accessory_Checkin_Notification')); +// } } diff --git a/app/Notifications/CheckoutAccessoryNotification.php b/app/Notifications/CheckoutAccessoryNotification.php index 721ba7f6a..cbc946ba4 100644 --- a/app/Notifications/CheckoutAccessoryNotification.php +++ b/app/Notifications/CheckoutAccessoryNotification.php @@ -6,6 +6,7 @@ use App\Models\Accessory; use App\Models\Setting; use App\Models\User; use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Channels\SlackWebhookChannel; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; @@ -55,35 +56,7 @@ class CheckoutAccessoryNotification extends Notification } if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) { - $notifyBy[] = 'slack'; - } - - /** - * 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 ($this->item->checkin_email()) { - $notifyBy[1] = 'mail'; - } + $notifyBy[] = SlackWebhookChannel::class; } return $notifyBy; @@ -163,31 +136,4 @@ class CheckoutAccessoryNotification extends Notification } - - /** - * Get the mail representation of the notification. - * - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail() - { - Log::debug($this->item->getImageUrl()); - $eula = $this->item->getEula(); - $req_accept = $this->item->requireAcceptance(); - - $accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance); - - return (new MailMessage)->markdown('notifications.markdown.checkout-accessory', - [ - 'item' => $this->item, - 'admin' => $this->admin, - 'note' => $this->note, - 'target' => $this->target, - 'eula' => $eula, - 'req_accept' => $req_accept, - 'accept_url' => $accept_url, - 'checkout_qty' => $this->checkout_qty, - ]) - ->subject(trans('mail.Confirm_accessory_delivery')); - } } diff --git a/resources/views/notifications/markdown/checkin-accessory.blade.php b/resources/views/mail/markdown/checkin-accessory.blade.php similarity index 100% rename from resources/views/notifications/markdown/checkin-accessory.blade.php rename to resources/views/mail/markdown/checkin-accessory.blade.php diff --git a/resources/views/notifications/markdown/checkout-accessory.blade.php b/resources/views/mail/markdown/checkout-accessory.blade.php similarity index 100% rename from resources/views/notifications/markdown/checkout-accessory.blade.php rename to resources/views/mail/markdown/checkout-accessory.blade.php diff --git a/routes/web.php b/routes/web.php index 548758e38..756c6dd45 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,6 +24,7 @@ use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\Auth\ForgotPasswordController; use App\Http\Controllers\Auth\ResetPasswordController; use App\Livewire\Importer; +use App\Models\Accessory; use App\Models\Asset; use App\Models\User; use Illuminate\Support\Facades\Route; @@ -56,7 +57,7 @@ Route::group(['middleware' => 'auth'], function () { * Locations */ Route::get('/test-email', function() { - $item = Asset::find(1); // Load some test data + $item = Accessory::find(1); // Load some test data $admin = User::find(1); $target = User::find(2); $acceptance = null; // Simulate acceptance data @@ -67,11 +68,10 @@ Route::group(['middleware' => 'auth'], function () { $fields = $item->model->fieldset->fields; } - return new \App\Mail\CheckinAssetMail( + return new \App\Mail\CheckinAccessoryMail( $item, $admin, $target, - $acceptance, $note); });