Skip to content

A free open source IT asset/license management system

adds Checkout Consumable mailable and slackwebhook channel to notifs

Godfrey M02bda3cd9598
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index fe9f7b6d38..4f51ed705f 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -8,6 +8,7 @@ use App\Mail\CheckinLicenseMail;
use App\Mail\CheckoutAccessoryMail;
use App\Mail\CheckoutAssetMail;
use App\Mail\CheckinAssetMail;
+use App\Mail\CheckoutConsumableMail;
use App\Mail\CheckoutLicenseMail;
use App\Models\Accessory;
use App\Models\Asset;
@@ -210,21 +211,22 @@ class CheckoutableListener
{
$notificationClass = null;
- switch (true) {
- case $event->checkoutable instanceof Accessory:
+ switch (get_class($event->checkoutable)) {
+ case Accessory::class:
$notificationClass = CheckoutAccessoryNotification::class;
break;
- case $event->checkoutable instanceof Asset:
+ case Asset::class:
$notificationClass = CheckoutAssetNotification::class;
break;
- case $event->checkoutable instanceof Consumable:
+ case Consumable::class:
$notificationClass = CheckoutConsumableNotification::class;
break;
- case $event->checkoutable instanceof LicenseSeat:
+ case LicenseSeat::class:
$notificationClass = CheckoutLicenseSeatNotification::class;
break;
}
+
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
}
private function getCheckoutMailType($event, $acceptance){
@@ -232,7 +234,7 @@ class CheckoutableListener
Accessory::class => CheckoutAccessoryMail::class,
Asset::class => CheckoutAssetMail::class,
LicenseSeat::class => CheckoutLicenseMail::class,
-// Consumable::class =>
+ Consumable::class => CheckoutConsumableMail::class,
];
$mailable= $lookup[get_class($event->checkoutable)];
@@ -244,8 +246,8 @@ class CheckoutableListener
Accessory::class => CheckinAccessoryMail::class,
Asset::class => CheckinAssetMail::class,
LicenseSeat::class => CheckinLicenseMail::class,
-// Consumable::class =>
];
+
$mailable= $lookup[get_class($event->checkoutable)];
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
diff --git a/app/Mail/CheckoutConsumableMail.php b/app/Mail/CheckoutConsumableMail.php
new file mode 100644
index 0000000000..382f789eb9
--- /dev/null
+++ b/app/Mail/CheckoutConsumableMail.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace App\Mail;
+
+use App\Models\Consumable;
+use App\Models\Setting;
+use App\Models\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Mail\Mailable;
+use Illuminate\Mail\Mailables\Address;
+use Illuminate\Mail\Mailables\Content;
+use Illuminate\Mail\Mailables\Envelope;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\Log;
+
+class CheckoutConsumableMail extends Mailable
+{
+ use Queueable, SerializesModels;
+
+ /**
+ * Create a new message instance.
+ */
+ public function __construct(Consumable $consumable, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
+ {
+ $this->item = $consumable;
+ $this->admin = $checkedOutBy;
+ $this->note = $note;
+ $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('[email protected]', 'Default Sender'),
+ cc: $cc,
+ subject: trans('mail.Confirm_consumable_delivery'),
+ );
+ }
+
+ /**
+ * 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);
+
+ return new Content(
+ markdown: 'mail.markdown.checkout-consumable',
+ with: [
+ 'item' => $this->item,
+ 'admin' => $this->admin,
+ 'note' => $this->note,
+ 'target' => $this->target,
+ 'eula' => $eula,
+ 'req_accept' => $req_accept,
+ 'accept_url' => $accept_url,
+ ]
+ );
+ }
+
+ /**
+ * Get the attachments for the message.
+ *
+ * @return array<int, \Illuminate\Mail\Mailables\Attachment>
+ */
+ public function attachments(): array
+ {
+ return [];
+ }
+}
diff --git a/app/Notifications/CheckinAccessoryNotification.php b/app/Notifications/CheckinAccessoryNotification.php
index 777834aae7..d295515121 100644
--- a/app/Notifications/CheckinAccessoryNotification.php
+++ b/app/Notifications/CheckinAccessoryNotification.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,7 +56,7 @@ class CheckinAccessoryNotification extends Notification
}
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
- $notifyBy[] = 'slack';
+ $notifyBy[] = SlackWebhookChannel::class;
}
return $notifyBy;
diff --git a/app/Notifications/CheckoutConsumableNotification.php b/app/Notifications/CheckoutConsumableNotification.php
index 6746795f2c..0a2733689b 100644
--- a/app/Notifications/CheckoutConsumableNotification.php
+++ b/app/Notifications/CheckoutConsumableNotification.php
@@ -6,6 +6,7 @@ use App\Models\Consumable;
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;
@@ -61,36 +62,36 @@ class CheckoutConsumableNotification extends Notification
}
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
- $notifyBy[] = 'slack';
+ $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;
}
@@ -165,30 +166,4 @@ class CheckoutConsumableNotification 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-consumable',
- [
- 'item' => $this->item,
- 'admin' => $this->admin,
- 'note' => $this->note,
- 'target' => $this->target,
- 'eula' => $eula,
- 'req_accept' => $req_accept,
- 'accept_url' => $accept_url,
- ])
- ->subject(trans('mail.Confirm_consumable_delivery'));
- }
}
diff --git a/app/Notifications/CheckoutLicenseSeatNotification.php b/app/Notifications/CheckoutLicenseSeatNotification.php
index 85d02abc92..1c26138a6e 100644
--- a/app/Notifications/CheckoutLicenseSeatNotification.php
+++ b/app/Notifications/CheckoutLicenseSeatNotification.php
@@ -6,6 +6,7 @@ use App\Models\LicenseSeat;
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;
@@ -60,7 +61,7 @@ class CheckoutLicenseSeatNotification extends Notification
}
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
- $notifyBy[] = 'slack';
+ $notifyBy[] = SlackWebhookChannel::class;
}
return $notifyBy;
diff --git a/resources/views/notifications/markdown/checkout-consumable.blade.php b/resources/views/mail/markdown/checkout-consumable.blade.php
similarity index 100%
rename from resources/views/notifications/markdown/checkout-consumable.blade.php
rename to resources/views/mail/markdown/checkout-consumable.blade.php