2018-09-10 07:40:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2023-04-19 12:31:12 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
2024-10-15 12:49:52 -07:00
|
|
|
use App\Mail\CheckoutAssetMail;
|
2018-09-10 07:40:26 -07:00
|
|
|
use App\Models\Accessory;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\CheckoutAcceptance;
|
2023-04-18 13:07:55 -07:00
|
|
|
use App\Models\Component;
|
2018-09-10 07:40:26 -07:00
|
|
|
use App\Models\Consumable;
|
|
|
|
use App\Models\LicenseSeat;
|
|
|
|
use App\Models\Recipients\AdminRecipient;
|
|
|
|
use App\Models\Setting;
|
2024-09-18 16:36:24 -07:00
|
|
|
use App\Models\User;
|
2018-09-10 07:40:26 -07:00
|
|
|
use App\Notifications\CheckinAccessoryNotification;
|
|
|
|
use App\Notifications\CheckinAssetNotification;
|
|
|
|
use App\Notifications\CheckinLicenseSeatNotification;
|
|
|
|
use App\Notifications\CheckoutAccessoryNotification;
|
|
|
|
use App\Notifications\CheckoutAssetNotification;
|
|
|
|
use App\Notifications\CheckoutConsumableNotification;
|
|
|
|
use App\Notifications\CheckoutLicenseSeatNotification;
|
2023-10-10 15:06:08 -07:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2024-10-15 12:49:52 -07:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2018-09-10 07:40:26 -07:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2022-09-15 13:18:42 -07:00
|
|
|
use Exception;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-09-10 07:40:26 -07:00
|
|
|
|
|
|
|
class CheckoutableListener
|
|
|
|
{
|
2023-04-18 13:07:55 -07:00
|
|
|
private array $skipNotificationsFor = [
|
|
|
|
Component::class,
|
|
|
|
];
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
/**
|
2023-04-19 17:22:56 -07:00
|
|
|
* Notify the user and post to webhook about the checked out checkoutable
|
|
|
|
* and add a record to the checkout_requests table.
|
2018-09-10 07:40:26 -07:00
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function onCheckedOut($event)
|
|
|
|
{
|
2023-04-18 13:07:55 -07:00
|
|
|
if ($this->shouldNotSendAnyNotifications($event->checkoutable)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
/**
|
|
|
|
* Make a checkout acceptance and attach it in the notification
|
|
|
|
*/
|
2023-10-10 15:18:37 -07:00
|
|
|
$acceptance = $this->getCheckoutAcceptance($event);
|
2024-10-15 12:49:52 -07:00
|
|
|
$notifiable = $this->getNotifiables($event);
|
|
|
|
$mailable = (new CheckoutAssetMail(
|
|
|
|
$event->checkoutable,
|
|
|
|
$event->checkedOutTo,
|
|
|
|
$event->checkedOutBy,
|
|
|
|
$acceptance,
|
|
|
|
$event->note
|
|
|
|
));
|
2018-09-10 07:40:26 -07:00
|
|
|
|
2024-09-18 15:23:44 -07:00
|
|
|
// Send email notifications
|
2022-09-15 13:18:42 -07:00
|
|
|
try {
|
2024-10-15 12:49:52 -07:00
|
|
|
if (! $event->checkedOutTo->locale){
|
|
|
|
$mailable->locale($event->checkedOutTo->locale);
|
2024-09-18 15:23:44 -07:00
|
|
|
}
|
2024-10-15 12:49:52 -07:00
|
|
|
Mail::to($notifiable)->send($mailable);
|
2023-10-10 15:13:46 -07:00
|
|
|
|
2024-10-15 12:49:52 -07:00
|
|
|
// Send Webhook notification
|
|
|
|
if ($this->shouldSendWebhookNotification()) {
|
2024-09-18 15:23:44 -07:00
|
|
|
// 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)
|
2024-09-18 16:19:35 -07:00
|
|
|
->notify($this->getCheckoutNotification($event, $acceptance));
|
2024-09-18 15:23:44 -07:00
|
|
|
} else {
|
|
|
|
Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint)
|
|
|
|
->notify($this->getCheckoutNotification($event, $acceptance));
|
|
|
|
}
|
2023-10-10 15:13:46 -07:00
|
|
|
}
|
2023-10-10 15:18:55 -07:00
|
|
|
} catch (ClientException $e) {
|
2024-07-04 12:49:22 -07:00
|
|
|
Log::debug("Exception caught during checkout notification: " . $e->getMessage());
|
2023-10-10 15:18:55 -07:00
|
|
|
} catch (Exception $e) {
|
2024-07-04 12:49:22 -07:00
|
|
|
Log::debug("Exception caught during checkout notification: " . $e->getMessage());
|
2019-01-22 14:02:08 -08:00
|
|
|
}
|
2018-09-10 07:40:26 -07:00
|
|
|
}
|
|
|
|
|
2024-09-18 15:23:44 -07:00
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
/**
|
2023-04-05 12:39:41 -07:00
|
|
|
* Notify the user and post to webhook about the checked in checkoutable
|
2018-09-10 07:40:26 -07:00
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function onCheckedIn($event)
|
|
|
|
{
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('onCheckedIn in the Checkoutable listener fired');
|
2020-09-11 16:10:18 -07:00
|
|
|
|
2023-04-18 13:07:55 -07:00
|
|
|
if ($this->shouldNotSendAnyNotifications($event->checkoutable)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
/**
|
|
|
|
* Send the appropriate notification
|
|
|
|
*/
|
2023-09-13 15:35:10 -07:00
|
|
|
if ($event->checkedOutTo && $event->checkoutable){
|
|
|
|
$acceptances = CheckoutAcceptance::where('checkoutable_id', $event->checkoutable->id)
|
|
|
|
->where('assigned_to_id', $event->checkedOutTo->id)
|
|
|
|
->get();
|
2021-09-07 10:01:32 -07:00
|
|
|
|
2023-09-13 15:35:10 -07:00
|
|
|
foreach($acceptances as $acceptance){
|
|
|
|
if($acceptance->isPending()){
|
|
|
|
$acceptance->delete();
|
|
|
|
}
|
2021-09-07 10:01:32 -07:00
|
|
|
}
|
|
|
|
}
|
2024-09-18 16:19:35 -07:00
|
|
|
|
2024-09-18 15:23:44 -07:00
|
|
|
$notifiables = $this->getNotifiables($event);
|
|
|
|
// Send email notifications
|
2022-09-15 13:18:42 -07:00
|
|
|
try {
|
2024-09-18 15:23:44 -07:00
|
|
|
foreach ($notifiables as $notifiable) {
|
|
|
|
if ($notifiable instanceof User && $notifiable->email != '') {
|
|
|
|
if (! $event->checkedOutTo->locale){
|
2024-09-18 16:19:35 -07:00
|
|
|
Notification::locale(Setting::getSettings()->locale)->send($notifiable, $this->getCheckoutNotification($event, $acceptance));
|
2024-09-18 15:23:44 -07:00
|
|
|
}
|
|
|
|
else {
|
2024-09-18 16:19:35 -07:00
|
|
|
Notification::send($notifiable, $this->getCheckinNotification($event));
|
2024-09-18 15:23:44 -07:00
|
|
|
}
|
|
|
|
}
|
2022-09-15 13:18:42 -07:00
|
|
|
}
|
2024-09-18 15:23:44 -07:00
|
|
|
// 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') {
|
2024-01-23 11:10:04 -08:00
|
|
|
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
|
|
|
|
->notify($this->getCheckinNotification($event));
|
2024-09-18 15:23:44 -07:00
|
|
|
} else {
|
|
|
|
Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint)
|
2024-09-18 16:19:35 -07:00
|
|
|
->notify($this->getCheckinNotification($event));
|
2024-01-23 11:10:04 -08:00
|
|
|
}
|
2023-10-10 15:16:12 -07:00
|
|
|
}
|
2024-01-23 11:10:04 -08:00
|
|
|
|
2023-10-10 15:18:55 -07:00
|
|
|
} catch (ClientException $e) {
|
2024-02-15 04:28:12 -08:00
|
|
|
Log::warning("Exception caught during checkout notification: " . $e->getMessage());
|
2023-10-10 15:18:55 -07:00
|
|
|
} catch (Exception $e) {
|
2024-02-15 04:28:12 -08:00
|
|
|
Log::warning("Exception caught during checkin notification: " . $e->getMessage());
|
2019-01-22 14:02:08 -08:00
|
|
|
}
|
2018-09-10 07:40:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a checkout acceptance
|
|
|
|
* @param Event $event
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
private function getCheckoutAcceptance($event)
|
|
|
|
{
|
2024-08-07 14:53:06 -07:00
|
|
|
$checkedOutToType = get_class($event->checkedOutTo);
|
|
|
|
if ($checkedOutToType != "App\Models\User") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$event->checkoutable->requireAcceptance()) {
|
2018-09-10 07:40:26 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$acceptance = new CheckoutAcceptance;
|
|
|
|
$acceptance->checkoutable()->associate($event->checkoutable);
|
|
|
|
$acceptance->assignedTo()->associate($event->checkedOutTo);
|
|
|
|
$acceptance->save();
|
|
|
|
|
|
|
|
return $acceptance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the entities to be notified of the passed event
|
|
|
|
*
|
|
|
|
* @param Event $event
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
private function getNotifiables($event)
|
|
|
|
{
|
2018-09-10 07:40:26 -07:00
|
|
|
$notifiables = collect();
|
|
|
|
|
|
|
|
/**
|
2023-09-13 12:56:27 -07:00
|
|
|
* Notify who checked out the item as long as the model can route notifications
|
2018-09-10 07:40:26 -07:00
|
|
|
*/
|
2023-09-13 12:56:27 -07:00
|
|
|
if (method_exists($event->checkedOutTo, 'routeNotificationFor')) {
|
|
|
|
$notifiables->push($event->checkedOutTo);
|
|
|
|
}
|
2018-09-10 07:40:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify Admin users if the settings is activated
|
|
|
|
*/
|
2021-12-02 20:01:03 -08:00
|
|
|
if ((Setting::getSettings()) && (Setting::getSettings()->admin_cc_email != '')) {
|
2018-09-10 07:40:26 -07:00
|
|
|
$notifiables->push(new AdminRecipient());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notifiables;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the appropriate notification for the event
|
|
|
|
*
|
|
|
|
* @param CheckoutableCheckedIn $event
|
|
|
|
* @return Notification
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
private function getCheckinNotification($event)
|
|
|
|
{
|
2018-09-10 07:40:26 -07:00
|
|
|
|
|
|
|
$notificationClass = null;
|
|
|
|
|
|
|
|
switch (get_class($event->checkoutable)) {
|
|
|
|
case Accessory::class:
|
|
|
|
$notificationClass = CheckinAccessoryNotification::class;
|
|
|
|
break;
|
|
|
|
case Asset::class:
|
|
|
|
$notificationClass = CheckinAssetNotification::class;
|
|
|
|
break;
|
|
|
|
case LicenseSeat::class:
|
|
|
|
$notificationClass = CheckinLicenseSeatNotification::class;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-11 16:10:18 -07:00
|
|
|
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('Notification class: '.$notificationClass);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the appropriate notification for the event
|
|
|
|
*
|
2023-04-19 12:31:12 -07:00
|
|
|
* @param CheckoutableCheckedOut $event
|
|
|
|
* @param CheckoutAcceptance|null $acceptance
|
2018-09-10 07:40:26 -07:00
|
|
|
* @return Notification
|
|
|
|
*/
|
2023-03-23 17:03:48 -07:00
|
|
|
private function getCheckoutNotification($event, $acceptance = null)
|
2021-06-10 13:15:52 -07:00
|
|
|
{
|
2018-09-10 07:40:26 -07:00
|
|
|
$notificationClass = null;
|
|
|
|
|
|
|
|
switch (get_class($event->checkoutable)) {
|
|
|
|
case Accessory::class:
|
|
|
|
$notificationClass = CheckoutAccessoryNotification::class;
|
|
|
|
break;
|
|
|
|
case Asset::class:
|
|
|
|
$notificationClass = CheckoutAssetNotification::class;
|
|
|
|
break;
|
|
|
|
case Consumable::class:
|
|
|
|
$notificationClass = CheckoutConsumableNotification::class;
|
|
|
|
break;
|
|
|
|
case LicenseSeat::class:
|
|
|
|
$notificationClass = CheckoutLicenseSeatNotification::class;
|
2024-01-23 13:05:39 -08:00
|
|
|
break;
|
2018-09-10 07:40:26 -07:00
|
|
|
}
|
|
|
|
|
2024-01-23 13:05:39 -08:00
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the listeners for the subscriber.
|
|
|
|
*
|
|
|
|
* @param Illuminate\Events\Dispatcher $events
|
|
|
|
*/
|
|
|
|
public function subscribe($events)
|
|
|
|
{
|
|
|
|
$events->listen(
|
2021-06-10 13:16:56 -07:00
|
|
|
\App\Events\CheckoutableCheckedIn::class,
|
2018-09-10 07:40:26 -07:00
|
|
|
'App\Listeners\CheckoutableListener@onCheckedIn'
|
|
|
|
);
|
|
|
|
|
|
|
|
$events->listen(
|
2021-06-10 13:16:56 -07:00
|
|
|
\App\Events\CheckoutableCheckedOut::class,
|
2018-09-10 07:40:26 -07:00
|
|
|
'App\Listeners\CheckoutableListener@onCheckedOut'
|
|
|
|
);
|
|
|
|
}
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2023-04-18 13:07:55 -07:00
|
|
|
private function shouldNotSendAnyNotifications($checkoutable): bool
|
|
|
|
{
|
|
|
|
return in_array(get_class($checkoutable), $this->skipNotificationsFor);
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:36:24 -07:00
|
|
|
private function shouldSendWebhookNotification(): bool
|
|
|
|
{
|
|
|
|
return Setting::getSettings() && Setting::getSettings()->webhook_endpoint;
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|