A free open source IT asset/license management system
refactor Checkout Notifications to accept company integration settings
Godfrey M6a2c5ff8aac1
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.phpindex b5239d5eca..3df7dc0a93 100644--- a/app/Listeners/CheckoutableListener.php+++ b/app/Listeners/CheckoutableListener.php@@ -655,8 +655,13 @@ class CheckoutableListenerprivate function webhookSource(Model $checkoutable): Company|Setting{- if ($checkoutable->company_id) {- $company = Company::find($checkoutable->company_id);+ $companyId = match (true) {+ $checkoutable instanceof LicenseSeat => $checkoutable->license->company_id,+ default => $checkoutable->company_id,+ };++ if ($companyId) {+ $company = Company::find($companyId);if ($company?->webhook_endpoint) {return $company;diff --git a/app/Notifications/CheckoutAccessoryNotification.php b/app/Notifications/CheckoutAccessoryNotification.phpindex 8ae0dd7e1e..efca9524e8 100644--- a/app/Notifications/CheckoutAccessoryNotification.php+++ b/app/Notifications/CheckoutAccessoryNotification.php@@ -5,6 +5,7 @@ namespace App\Notifications;use App\Models\Accessory;use App\Models\Setting;use App\Models\User;+use App\Models\Company;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Messages\MailMessage;@@ -35,7 +36,8 @@ class CheckoutAccessoryNotification extends Notification implements ShouldQueuepublic $target,public User $admin,public $acceptance,- public $note+ public $note,+ public Company|Setting $webhookSource,){$this->checkout_qty = $item->checkout_qty;@@ -49,17 +51,17 @@ class CheckoutAccessoryNotification extends Notification implements ShouldQueuepublic function via(){$notifyBy = [];- if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {+ if ($this->webhookSource->webhook_selected == 'google' && $this->webhookSource->webhook_endpoint) {$notifyBy[] = GoogleChatChannel::class;}- if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {+ if ($this->webhookSource->webhook_selected == 'microsoft' && $this->webhookSource->webhook_endpoint) {$notifyBy[] = MicrosoftTeamsChannel::class;}- if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general') {+ if ($this->webhookSource->webhook_selected == 'slack' || $this->webhookSource->webhook_selected == 'general') {$notifyBy[] = 'slack';}@@ -100,8 +102,8 @@ class CheckoutAccessoryNotification extends Notification implements ShouldQueue$admin = $this->admin;$item = $this->item;$note = $this->note;- $botname = (Setting::getSettings()->webhook_botname) ? Setting::getSettings()->webhook_botname : 'Snipe-Bot';- $channel = (Setting::getSettings()->webhook_channel) ? Setting::getSettings()->webhook_channel : '';+ $botname = ($this->webhookSource->webhook_botname) ? $this->webhookSource->webhook_botname : 'Snipe-Bot';+ $channel = ($this->webhookSource->webhook_channel) ? $this->webhookSource->webhook_channel : '';$fields = [trans('general.to') => '<'.$target->present()->viewUrl().'|'.$target->display_name.'>',@@ -134,9 +136,9 @@ class CheckoutAccessoryNotification extends Notification implements ShouldQueue$item = $this->item;$note = $this->note;- if (! Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {+ if (!Str::contains($this->webhookSource->webhook_endpoint, 'workflows')) {return MicrosoftTeamsMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->type('success')->addStartGroupToSection('activityTitle')->title(trans('mail.Accessory_Checkout_Notification'))@@ -171,7 +173,7 @@ class CheckoutAccessoryNotification extends Notification implements ShouldQueue$note = $this->note;return GoogleChatMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->card(Card::create()->header(diff --git a/app/Notifications/CheckoutComponentNotification.php b/app/Notifications/CheckoutComponentNotification.phpindex 3f4186de72..78ef2ca7ca 100644--- a/app/Notifications/CheckoutComponentNotification.php+++ b/app/Notifications/CheckoutComponentNotification.php@@ -5,6 +5,7 @@ namespace App\Notifications;use App\Models\Component;use App\Models\Setting;use App\Models\User;+use App\Models\Company;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Channels\SlackWebhookChannel;@@ -35,7 +36,8 @@ class CheckoutComponentNotification extends Notification implements ShouldQueuepublic $target,public User $admin,public $acceptance,- public $note+ public $note,+ public Company|Setting $webhookSource,){$this->qty = $item->checkout_qty;@@ -49,17 +51,17 @@ class CheckoutComponentNotification extends Notification implements ShouldQueuepublic function via(){$notifyBy = [];- if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {+ if ($this->webhookSource->webhook_selected == 'google' && $this->webhookSource->webhook_endpoint) {$notifyBy[] = GoogleChatChannel::class;}- if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {+ if ($this->webhookSource->webhook_selected == 'microsoft' && $this->webhookSource->webhook_endpoint) {$notifyBy[] = MicrosoftTeamsChannel::class;}- if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general') {+ if ($this->webhookSource->webhook_selected == 'slack' || $this->webhookSource->webhook_selected == 'general') {$notifyBy[] = SlackWebhookChannel::class;}@@ -72,8 +74,8 @@ class CheckoutComponentNotification extends Notification implements ShouldQueue$admin = $this->admin;$item = $this->item;$note = $this->note;- $botname = (Setting::getSettings()->webhook_botname) ? Setting::getSettings()->webhook_botname : 'Snipe-Bot';- $channel = (Setting::getSettings()->webhook_channel) ? Setting::getSettings()->webhook_channel : '';+ $botname = ($this->webhookSource->webhook_botname) ? $this->webhookSource->webhook_botname : 'Snipe-Bot';+ $channel = ($this->webhookSource->webhook_channel) ? $this->webhookSource->webhook_channel : '';$fields = [trans('general.to') => '<'.$target->present()->viewUrl().'|'.$target->display_name.'>',@@ -106,9 +108,9 @@ class CheckoutComponentNotification extends Notification implements ShouldQueue$item = $this->item;$note = $this->note;- if (! Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {+ if (!Str::contains($this->webhookSource->webhook_endpoint, 'workflows')) {return MicrosoftTeamsMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->type('success')->addStartGroupToSection('activityTitle')->title(trans('mail.Component_checkout_notification'))@@ -139,7 +141,7 @@ class CheckoutComponentNotification extends Notification implements ShouldQueue$note = $this->note;return GoogleChatMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->card(Card::create()->header(diff --git a/app/Notifications/CheckoutConsumableNotification.php b/app/Notifications/CheckoutConsumableNotification.phpindex 9b6ec820bf..29241dcca6 100644--- a/app/Notifications/CheckoutConsumableNotification.php+++ b/app/Notifications/CheckoutConsumableNotification.php@@ -5,6 +5,7 @@ namespace App\Notifications;use App\Models\Consumable;use App\Models\Setting;use App\Models\User;+use App\Models\Company;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Channels\SlackWebhookChannel;@@ -34,7 +35,8 @@ class CheckoutConsumableNotification extends Notification implements ShouldQueuepublic $target,public User $admin,public $acceptance,- public $note+ public $note,+ public Company|Setting $webhookSource,){$this->qty = $item->checkout_qty;@@ -48,17 +50,17 @@ class CheckoutConsumableNotification extends Notification implements ShouldQueuepublic function via(){$notifyBy = [];- if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {+ if ($this->webhookSource->webhook_selected == 'google' && $this->webhookSource->webhook_endpoint) {$notifyBy[] = GoogleChatChannel::class;}- if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) {+ if ($this->webhookSource->webhook_selected == 'microsoft' && $this->webhookSource->webhook_endpoint) {$notifyBy[] = MicrosoftTeamsChannel::class;}- if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general') {+ if ($this->webhookSource->webhook_selected == 'slack' || $this->webhookSource->webhook_selected == 'general') {$notifyBy[] = SlackWebhookChannel::class;}@@ -71,8 +73,8 @@ class CheckoutConsumableNotification extends Notification implements ShouldQueue$admin = $this->admin;$item = $this->item;$note = $this->note;- $botname = (Setting::getSettings()->webhook_botname) ? Setting::getSettings()->webhook_botname : 'Snipe-Bot';- $channel = (Setting::getSettings()->webhook_channel) ? Setting::getSettings()->webhook_channel : '';+ $botname = ($this->webhookSource->webhook_botname) ? $this->webhookSource->webhook_botname : 'Snipe-Bot';+ $channel = ($this->webhookSource->webhook_channel) ? $this->webhookSource->webhook_channel : '';$fields = [trans('general.to') => '<'.$target->present()->viewUrl().'|'.$target->display_name.'>',@@ -105,9 +107,9 @@ class CheckoutConsumableNotification extends Notification implements ShouldQueue$item = $this->item;$note = $this->note;- if (! Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {+ if (!Str::contains($this->webhookSource->webhook_endpoint, 'workflows')) {return MicrosoftTeamsMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->type('success')->addStartGroupToSection('activityTitle')->title(trans('mail.Consumable_checkout_notification'))@@ -138,7 +140,7 @@ class CheckoutConsumableNotification extends Notification implements ShouldQueue$note = $this->note;return GoogleChatMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->card(Card::create()->header(diff --git a/app/Notifications/CheckoutLicenseSeatNotification.php b/app/Notifications/CheckoutLicenseSeatNotification.phpindex de53341f39..03a08f086b 100644--- a/app/Notifications/CheckoutLicenseSeatNotification.php+++ b/app/Notifications/CheckoutLicenseSeatNotification.php@@ -2,6 +2,7 @@namespace App\Notifications;+use App\Models\Company;use App\Models\License;use App\Models\LicenseSeat;use App\Models\Setting;@@ -32,10 +33,12 @@ class CheckoutLicenseSeatNotification extends Notification implements ShouldQueupublic $target;++/*** Create a new notification instance.*/- public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $checkedOutBy, $acceptance, $note)+ public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $checkedOutBy, $acceptance, $note, public Company|Setting $webhookSource){$this->item = $licenseSeat->license;$this->admin = $checkedOutBy;@@ -53,16 +56,16 @@ class CheckoutLicenseSeatNotification extends Notification implements ShouldQueu{$notifyBy = [];- if (Setting::getSettings()->webhook_selected == 'google') {+ if ($this->webhookSource->webhook_selected == 'google') {$notifyBy[] = GoogleChatChannel::class;}- if (Setting::getSettings()->webhook_selected == 'microsoft') {+ if ($this->webhookSource->webhook_selected == 'microsoft') {$notifyBy[] = MicrosoftTeamsChannel::class;}- if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general') {+ if ($this->webhookSource->webhook_selected == 'slack' || $this->webhookSource->webhook_selected == 'general') {$notifyBy[] = SlackWebhookChannel::class;}@@ -75,8 +78,8 @@ class CheckoutLicenseSeatNotification extends Notification implements ShouldQueu$admin = $this->admin;$item = $this->item;$note = $this->note;- $botname = (Setting::getSettings()->webhook_botname) ? Setting::getSettings()->webhook_botname : 'Snipe-Bot';- $channel = (Setting::getSettings()->webhook_channel) ? Setting::getSettings()->webhook_channel : '';+ $botname = ($this->webhookSource->webhook_botname) ? $this->webhookSource->webhook_botname : 'Snipe-Bot';+ $channel = ($this->webhookSource->webhook_channel) ? $this->webhookSource->webhook_channel : '';$fields = [trans('general.to') => '<'.$target->present()->viewUrl().'|'.$target->display_name.'>',@@ -110,9 +113,9 @@ class CheckoutLicenseSeatNotification extends Notification implements ShouldQueu$item = $this->item;$note = $this->note;- if (! Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {+ if (!Str::contains($this->webhookSource->webhook_endpoint, 'workflows')) {return MicrosoftTeamsMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->type('success')->addStartGroupToSection('activityTitle')->title(trans('mail.License_Checkout_Notification'))@@ -143,7 +146,7 @@ class CheckoutLicenseSeatNotification extends Notification implements ShouldQueu$note = $this->note;return GoogleChatMessage::create()- ->to(Setting::getSettings()->webhook_endpoint)+ ->to($this->webhookSource->webhook_endpoint)->card(Card::create()->header(