2018-09-10 07:40:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2023-04-19 12:31:12 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
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;
|
|
|
|
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;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2022-09-15 13:18:42 -07:00
|
|
|
use Exception;
|
|
|
|
use 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
|
|
|
|
*/
|
|
|
|
$acceptance = $this->getCheckoutAcceptance($event);
|
|
|
|
|
2022-09-15 13:18:42 -07:00
|
|
|
try {
|
2023-04-19 12:26:48 -07:00
|
|
|
if ($this->shouldSendWebhookNotification()) {
|
|
|
|
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
|
|
|
|
->notify($this->getCheckoutNotification($event));
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:18:42 -07:00
|
|
|
if (! $event->checkedOutTo->locale) {
|
|
|
|
Notification::locale(Setting::getSettings()->locale)->send(
|
|
|
|
$this->getNotifiables($event),
|
|
|
|
$this->getCheckoutNotification($event, $acceptance)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Notification::send(
|
|
|
|
$this->getNotifiables($event),
|
|
|
|
$this->getCheckoutNotification($event, $acceptance)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error("Exception caught during checkout notification: ".$e->getMessage());
|
2019-01-22 14:02:08 -08: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)
|
|
|
|
{
|
2022-05-17 06:56:43 -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
|
|
|
}
|
|
|
|
}
|
2022-05-17 06:56:43 -07:00
|
|
|
|
2022-09-15 13:18:42 -07:00
|
|
|
try {
|
2023-04-19 12:26:48 -07:00
|
|
|
if ($this->shouldSendWebhookNotification()) {
|
|
|
|
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
|
|
|
|
->notify($this->getCheckinNotification($event));
|
|
|
|
}
|
|
|
|
|
2022-09-15 13:18:42 -07:00
|
|
|
// Use default locale
|
|
|
|
if (! $event->checkedOutTo->locale) {
|
|
|
|
Notification::locale(Setting::getSettings()->locale)->send(
|
|
|
|
$this->getNotifiables($event),
|
|
|
|
$this->getCheckinNotification($event)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Notification::send(
|
|
|
|
$this->getNotifiables($event),
|
|
|
|
$this->getCheckinNotification($event)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error("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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
\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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|