Fixed checkin/checkout notification crashing

This commit is contained in:
snipe 2020-09-11 16:10:18 -07:00
parent a91e81fc42
commit 52848ca8d8
No known key found for this signature in database
GPG key ID: 10BFFDA3ED34B5AC
3 changed files with 98 additions and 43 deletions

View file

@ -57,22 +57,32 @@ class CheckoutableListener
* Notify the user about the checked in checkoutable
*/
public function onCheckedIn($event) {
\Log::debug('checkin fired');
/**
* When the item wasn't checked out to a user, we can't send notifications
*/
if(!$event->checkedOutTo instanceof User) {
\Log::debug('checked out to not a user');
return;
}
/**
* Send the appropriate notification
*/
\Log::debug('checked out to a user');
if(!$event->checkedOutTo->locale){
\Log::debug('Use default settings locale');
Notification::locale(Setting::getSettings()->locale)->send(
$this->getNotifiables($event),
$this->getCheckinNotification($event)
);
} else {
\Log::debug('Use user locale? I do not think this works as expected yet');
// \Log::debug(print_r($this->getNotifiables($event), true));
Notification::send(
$this->getNotifiables($event),
$this->getCheckinNotification($event)
@ -130,7 +140,9 @@ class CheckoutableListener
*/
private function getCheckinNotification($event) {
$model = get_class($event->checkoutable);
// $model = get_class($event->checkoutable);
$notificationClass = null;
@ -146,6 +158,7 @@ class CheckoutableListener
break;
}
\Log::debug('Notification class: '.$notificationClass);
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
}

View file

@ -122,38 +122,38 @@ trait Loggable
$log->logaction('checkin from');
$params = [
'target' => $target,
'item' => $log->item,
'admin' => $log->user,
'note' => $note,
'target_type' => $log->target_type,
'settings' => $settings,
];
$checkinClass = null;
if (method_exists($target, 'notify')) {
try {
$target->notify(new static::$checkinClass($params));
} catch (\Exception $e) {
\Log::debug($e);
}
}
// Send to the admin, if settings dictate
$recipient = new \App\Models\Recipients\AdminRecipient();
if (($settings->admin_cc_email!='') && (static::$checkinClass!='')) {
try {
$recipient->notify(new static::$checkinClass($params));
} catch (\Exception $e) {
\Log::debug($e);
}
}
// $params = [
// 'target' => $target,
// 'item' => $log->item,
// 'admin' => $log->user,
// 'note' => $note,
// 'target_type' => $log->target_type,
// 'settings' => $settings,
// ];
//
//
// $checkinClass = null;
//
// if (method_exists($target, 'notify')) {
// try {
// $target->notify(new static::$checkinClass($params));
// } catch (\Exception $e) {
// \Log::debug($e);
// }
//
// }
//
// // Send to the admin, if settings dictate
// $recipient = new \App\Models\Recipients\AdminRecipient();
//
// if (($settings->admin_cc_email!='') && (static::$checkinClass!='')) {
// try {
// $recipient->notify(new static::$checkinClass($params));
// } catch (\Exception $e) {
// \Log::debug($e);
// }
//
// }
return $log;
}

View file

@ -26,6 +26,7 @@ class CheckinAccessoryNotification extends Notification
$this->admin = $checkedInby;
$this->note = $note;
$this->settings = Setting::getSettings();
\Log::debug('Constructor for notification fired');
}
/**
@ -35,23 +36,63 @@ class CheckinAccessoryNotification extends Notification
*/
public function via()
{
\Log::debug('via called');
$notifyBy = [];
if (Setting::getSettings()->slack_endpoint) {
$notifyBy[] = 'slack';
}
if (Setting::getSettings()->slack_endpoint!='') {
$notifyBy[] = 'slack';
}
/**
* Only send checkin notifications to users if the category
* has the corresponding checkbox checked.
* Only send notifications to users that have email addresses
*/
if ($this->item->checkin_email() && $this->target instanceof User && $this->target->email != '')
{
\Log::debug('use email');
if ($this->target instanceof User && $this->target->email != '') {
\Log::debug('The target is a user');
/**
* Send an email if the asset requires acceptance,
* so the user can accept or decline the asset
*/
if (($this->item->requireAcceptance()) || ($this->item->getEula()) || ($this->item->checkin_email())) {
$notifyBy[] = 'mail';
}
/**
* Send an email if the asset requires acceptance,
* so the user can accept or decline the asset
*/
if ($this->item->requireAcceptance()) {
\Log::debug('This accessory requires acceptance');
}
/**
* Send an email if the item has a EULA, since the user should always receive it
*/
if ($this->item->getEula()) {
\Log::debug('This accessory has a EULA');
}
/**
* Send an email if an email should be sent at checkin/checkout
*/
if ($this->item->checkin_email()) {
\Log::debug('This accessory has a checkin_email()');
}
}
\Log::debug('checkin_email on this category is '.$this->item->checkin_email());
return $notifyBy;
}
@ -86,6 +127,7 @@ class CheckinAccessoryNotification extends Notification
*/
public function toMail()
{
\Log::debug('to email called');
return (new MailMessage)->markdown('notifications.markdown.checkin-accessory',
[
'item' => $this->item,