sends checkout notification via webhook

This commit is contained in:
Godfrey M 2024-10-16 12:51:19 -07:00
parent 3ab2521cb0
commit 9e1b86f586
3 changed files with 51 additions and 61 deletions

View file

@ -62,17 +62,17 @@ class CheckoutableListener
}
Mail::to($notifiable)->send($mailable);
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));
// }
// }
// 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) {
@ -231,17 +231,17 @@ class CheckoutableListener
{
$notificationClass = null;
switch (get_class($event->checkoutable)) {
case Accessory::class:
switch (true) {
case $event->checkoutable instanceof Accessory:
$notificationClass = CheckoutAccessoryNotification::class;
break;
case Asset::class:
case $event->checkoutable instanceof Asset:
$notificationClass = CheckoutAssetNotification::class;
break;
case Consumable::class:
case $event->checkoutable instanceof Consumable:
$notificationClass = CheckoutConsumableNotification::class;
break;
case LicenseSeat::class:
break;
case $event->checkoutable instanceof LicenseSeat:
$notificationClass = CheckoutLicenseSeatNotification::class;
break;
}

View file

@ -50,7 +50,6 @@ class CheckinAssetNotification extends Notification
*/
public function via()
{
$notifyBy = [];
if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {
$notifyBy[] = GoogleChatChannel::class;
@ -64,14 +63,14 @@ class CheckinAssetNotification extends Notification
Log::debug('use webhook');
$notifyBy[] = 'slack';
}
/**
* Only send checkin notifications to users if the category
* has the corresponding checkbox checked.
*/
if ($this->item->checkin_email() && $this->target instanceof User && $this->target->email != '') {
$notifyBy[] = 'mail';
}
dd($notifyBy);
// /**
// * Only send checkin notifications to users if the category
// * has the corresponding checkbox checked.
// */
// if ($this->item->checkin_email() && $this->target instanceof User && $this->target->email != '') {
// $notifyBy[] = 'mail';
// }
return $notifyBy;
}

View file

@ -8,6 +8,7 @@ use App\Models\Setting;
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;
@ -32,6 +33,23 @@ class CheckoutAssetNotification extends Notification
*/
public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
{
$this->settings = Setting::getSettings();
$this->item = $asset;
$this->admin = $checkedOutBy;
$this->note = $note;
$this->target = $checkedOutTo;
$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 notification's delivery channels.
@ -41,61 +59,34 @@ class CheckoutAssetNotification extends Notification
public function via()
{
$notifyBy = [];
if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {
if (Setting::getSettings()->webhook_selected === 'google' && Setting::getSettings()->webhook_endpoint) {
$notifyBy[] = GoogleChatChannel::class;
}
if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {
if (Setting::getSettings()->webhook_selected === 'microsoft' && Setting::getSettings()->webhook_endpoint) {
$notifyBy[] = MicrosoftTeamsChannel::class;
}
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general' ) {
Log::debug('use webhook');
$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;
}
public function toSlack()
public function toSlack() :SlackMessage
{
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;
$botname = ($this->settings->webhook_botname) ? $this->settings->webhook_botname : 'Snipe-Bot';
$botname = ($this->settings->webhook_botname) ?: 'Snipe-Bot';
$channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
$fields = [
@ -103,7 +94,7 @@ class CheckoutAssetNotification extends Notification
'By' => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
];
if (($this->expected_checkin) && ($this->expected_checkin != '')) {
if (($this->expected_checkin) && ($this->expected_checkin !== '')) {
$fields['Expected Checkin'] = $this->expected_checkin;
}