From 9710436d54fc23e79bf96b61430f3f0f3345581a Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 16 Oct 2024 13:12:07 -0700 Subject: [PATCH] adds Mailables for asset checkin and out --- app/Listeners/CheckoutableListener.php | 8 +- app/Mail/CheckinAssetMail.php | 102 ++++++++++++ app/Mail/CheckoutAssetMail.php | 150 ++++++++++++++++++ .../CheckinAssetNotification.php | 32 +--- .../markdown/checkin-asset.blade.php | 0 routes/web.php | 2 +- 6 files changed, 255 insertions(+), 39 deletions(-) create mode 100644 app/Mail/CheckinAssetMail.php create mode 100644 app/Mail/CheckoutAssetMail.php rename resources/views/{notifications => mail}/markdown/checkin-asset.blade.php (100%) diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 16032a54f5..44e731b4ba 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -64,15 +64,9 @@ class CheckoutableListener Log::info('Sending email, Locale: ' .($event->checkedOutTo->locale ?? 'default')); // Send Webhook notification if ($this->shouldSendWebhookNotification()) { - // Slack doesn't include the URL in its messaging format, so this is needed to hit the endpoint - if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general') { - Notification::route('slack', Setting::getSettings()->webhook_endpoint) - ->notify($this->getCheckoutNotification($event, $acceptance)); - } else { Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint) ->notify($this->getCheckoutNotification($event, $acceptance)); } - } } catch (ClientException $e) { Log::debug("Exception caught during checkout notification: " . $e->getMessage()); } catch (Exception $e) { @@ -111,7 +105,7 @@ class CheckoutableListener $mailable = (new CheckInAssetMail( $event->checkoutable, $event->checkedOutTo, - $event->checkedOutBy, + $event->checkedInBy, $event->note, )); diff --git a/app/Mail/CheckinAssetMail.php b/app/Mail/CheckinAssetMail.php new file mode 100644 index 0000000000..5795d795c7 --- /dev/null +++ b/app/Mail/CheckinAssetMail.php @@ -0,0 +1,102 @@ +target = $checkedOutTo; + $this->item = $asset; + $this->admin = $checkedInBy; + $this->note = $note; + + $this->settings = Setting::getSettings(); + $this->expected_checkin = ''; + + if ($this->item->expected_checkin) { + $this->expected_checkin = Helper::getFormattedDateObject($this->item->expected_checkin, 'date', + false); + } + } + + /** + * 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.Asset_Checkin_Notification'), + ); + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return Content + */ + public function content(): Content + { + $this->item->load('assetstatus'); + $fields = []; + + // Check if the item has custom fields associated with it + if (($this->item->model) && ($this->item->model->fieldset)) { + $fields = $this->item->model->fieldset->fields; + } + + return new Content( + markdown: 'mail.markdown.checkin-asset', + with: [ + 'item' => $this->item, + 'status' => $this->item->assetstatus?->name, + 'admin' => $this->admin, + 'note' => $this->note, + 'target' => $this->target, + 'fields' => $fields, + 'expected_checkin' => $this->expected_checkin, + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Mail/CheckoutAssetMail.php b/app/Mail/CheckoutAssetMail.php new file mode 100644 index 0000000000..214c48ed5c --- /dev/null +++ b/app/Mail/CheckoutAssetMail.php @@ -0,0 +1,150 @@ +item = $asset; + $this->admin = $checkedOutBy; + $this->note = $note; + $this->target = $checkedOutTo; + $this->acceptance = $acceptance; + + $this->settings = Setting::getSettings(); + + $this->last_checkout = ''; + $this->expected_checkin = ''; + + if ($this->item->last_checkout) { + $this->last_checkout = Helper::getFormattedDateObject($this->item->last_checkout, 'date', + false); + } + + if ($this->item->expected_checkin) { + $this->expected_checkin = Helper::getFormattedDateObject($this->item->expected_checkin, 'date', + false); + } + } + + /** + * 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.Asset_Checkout_Notification'), + ); + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return Content + */ + public function content(): Content + { + $this->item->load('assetstatus'); + $eula = method_exists($this->item, 'getEula') ? $this->item->getEula() : ''; + $req_accept = method_exists($this->item, 'requireAcceptance') ? $this->item->requireAcceptance() : 0; + $fields = []; + + // 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-asset', + with: [ + 'item' => $this->item, + 'admin' => $this->admin, + 'status' => $this->item->assetstatus?->name, + 'note' => $this->note, + 'target' => $this->target, + 'fields' => $fields, + 'eula' => $eula, + 'req_accept' => $req_accept, + 'accept_url' => $accept_url, + 'last_checkout' => $this->last_checkout, + 'expected_checkin' => $this->expected_checkin, + ], + ); + } +// public function build() +// { +// $this->item->load('assetstatus'); +// $eula = method_exists($this->item, 'getEula') ? $this->item->getEula() : ''; +// $req_accept = method_exists($this->item, 'requireAcceptance') ? $this->item->requireAcceptance() : 0; +// $fields = []; +// +// // 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 $this +// ->subject('Asset Checkout Notification') +// ->markdown('notifications.markdown.checkout-asset') +// ->with([ +// 'item' => $this->item, +// 'admin' => $this->admin, +// 'status' => $this->item->assetstatus?->name, +// 'note' => $this->note, +// 'target' => $this->target, +// 'fields' => $fields, +// 'eula' => $eula, +// 'req_accept' => $req_accept, +// 'accept_url' => $accept_url, +// 'last_checkout' => $this->last_checkout, +// 'expected_checkin' => $this->expected_checkin, +// ]); +// } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Notifications/CheckinAssetNotification.php b/app/Notifications/CheckinAssetNotification.php index 85b1c74413..5c38982ab6 100644 --- a/app/Notifications/CheckinAssetNotification.php +++ b/app/Notifications/CheckinAssetNotification.php @@ -63,7 +63,7 @@ class CheckinAssetNotification extends Notification Log::debug('use webhook'); $notifyBy[] = 'slack'; } -dd($notifyBy); + // /** // * Only send checkin notifications to users if the category // * has the corresponding checkbox checked. @@ -141,35 +141,5 @@ dd($notifyBy); ) ) ); - - } - - /** - * Get the mail representation of the notification. - * - * @return \Illuminate\Notifications\Messages\MailMessage - */ - public function toMail() - { - $fields = []; - - // Check if the item has custom fields associated with it - if (($this->item->model) && ($this->item->model->fieldset)) { - $fields = $this->item->model->fieldset->fields; - } - - $message = (new MailMessage)->markdown('notifications.markdown.checkin-asset', - [ - 'item' => $this->item, - 'status' => $this->item->assetstatus?->name, - 'admin' => $this->admin, - 'note' => $this->note, - 'target' => $this->target, - 'fields' => $fields, - 'expected_checkin' => $this->expected_checkin, - ]) - ->subject(trans('mail.Asset_Checkin_Notification')); - - return $message; } } diff --git a/resources/views/notifications/markdown/checkin-asset.blade.php b/resources/views/mail/markdown/checkin-asset.blade.php similarity index 100% rename from resources/views/notifications/markdown/checkin-asset.blade.php rename to resources/views/mail/markdown/checkin-asset.blade.php diff --git a/routes/web.php b/routes/web.php index 7bed89acf7..548758e382 100644 --- a/routes/web.php +++ b/routes/web.php @@ -67,7 +67,7 @@ Route::group(['middleware' => 'auth'], function () { $fields = $item->model->fieldset->fields; } - return new \App\Mail\CheckoutAssetMail( + return new \App\Mail\CheckinAssetMail( $item, $admin, $target,