Skip to content

Commit 3965bcd

Browse files
authored
Merge pull request #15773 from Godmartinz/Fix_failing_failing_tests
Fixed notifications for licenses and asset to asset checkoutables
2 parents 287f4ad + 2362cb5 commit 3965bcd

File tree

6 files changed

+64
-38
lines changed

6 files changed

+64
-38
lines changed

app/Listeners/CheckoutableListener.php

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Models\Component;
1717
use App\Models\Consumable;
1818
use App\Models\LicenseSeat;
19+
use App\Models\Location;
1920
use App\Models\Setting;
2021
use App\Models\User;
2122
use App\Notifications\CheckinAccessoryNotification;
@@ -60,14 +61,14 @@ public function onCheckedOut($event)
6061
$adminCcEmailsArray = array_map('trim', explode(',', $adminCcEmail));
6162
}
6263
$ccEmails = array_filter($adminCcEmailsArray);
63-
$notifiable = $event->checkedOutTo;
6464
$mailable = $this->getCheckoutMailType($event, $acceptance);
65+
$notifiable = $this->getNotifiables($event);
66+
67+
if (!$event->checkedOutTo->locale){
68+
$mailable->locale($event->checkedOutTo->locale);
69+
}
6570
// Send email notifications
6671
try {
67-
if (!$event->checkedOutTo->locale){
68-
$mailable->locale($event->checkedOutTo->locale);
69-
}
70-
7172
/**
7273
* Send an email if any of the following conditions are met:
7374
* 1. The asset requires acceptance
@@ -77,15 +78,20 @@ public function onCheckedOut($event)
7778

7879
if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() ||
7980
(method_exists($event->checkoutable, 'checkin_email') && $event->checkoutable->checkin_email())) {
80-
if (!empty($notifiable->email)) {
81+
if (!empty($notifiable)) {
8182
Mail::to($notifiable)->cc($ccEmails)->send($mailable);
82-
} else {
83+
} elseif (!empty($ccEmails)) {
8384
Mail::cc($ccEmails)->send($mailable);
8485
}
85-
Log::info('Sending email, Locale: ' . ($event->checkedOutTo->locale ?? 'default'));
86-
}
87-
86+
Log::info('Sending email, Locale: ' . ($event->checkedOutTo->locale ?? 'default'));
87+
}
88+
} catch (ClientException $e) {
89+
Log::debug("Exception caught during checkout email: " . $e->getMessage());
90+
} catch (Exception $e) {
91+
Log::debug("Exception caught during checkout email: " . $e->getMessage());
92+
}
8893
// Send Webhook notification
94+
try{
8995
if ($this->shouldSendWebhookNotification()) {
9096
if (Setting::getSettings()->webhook_selected === 'microsoft') {
9197
$message = $this->getCheckoutNotification($event)->toMicrosoftTeams();
@@ -137,38 +143,43 @@ public function onCheckedIn($event)
137143
$adminCcEmailsArray = array_map('trim', explode(',', $adminCcEmail));
138144
}
139145
$ccEmails = array_filter($adminCcEmailsArray);
140-
$notifiable = $event->checkedOutTo;
141146
$mailable = $this->getCheckinMailType($event);
147+
$notifiable = $this->getNotifiables($event);
142148

149+
if (!$event->checkedOutTo->locale){
150+
$mailable->locale($event->checkedOutTo->locale);
151+
}
143152
// Send email notifications
144153
try {
145-
if (!$event->checkedOutTo->locale){
146-
$mailable->locale($event->checkedOutTo->locale);
147-
}
148154
/**
149155
* Send an email if any of the following conditions are met:
150156
* 1. The asset requires acceptance
151157
* 2. The item has a EULA
152158
* 3. The item should send an email at check-in/check-out
153159
*/
154-
155160
if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() ||
156161
(method_exists($event->checkoutable, 'checkin_email') && $event->checkoutable->checkin_email())) {
157-
if (!empty($notifiable->email)) {
162+
if (!empty($notifiable)) {
158163
Mail::to($notifiable)->cc($ccEmails)->send($mailable);
159-
} else {
164+
} elseif (!empty($ccEmails)){
160165
Mail::cc($ccEmails)->send($mailable);
161166
}
162167
Log::info('Sending email, Locale: ' . $event->checkedOutTo->locale);
163168
}
169+
} catch (ClientException $e) {
170+
Log::debug("Exception caught during checkin email: " . $e->getMessage());
171+
} catch (Exception $e) {
172+
Log::debug("Exception caught during checkin email: " . $e->getMessage());
173+
}
164174

165-
// Send Webhook notification
175+
// Send Webhook notification
176+
try {
166177
if ($this->shouldSendWebhookNotification()) {
167178
Notification::route(Setting::getSettings()->webhook_selected, Setting::getSettings()->webhook_endpoint)
168179
->notify($this->getCheckinNotification($event));
169180
}
170181
} catch (ClientException $e) {
171-
Log::warning("Exception caught during checkout notification: " . $e->getMessage());
182+
Log::warning("Exception caught during checkin notification: " . $e->getMessage());
172183
} catch (Exception $e) {
173184
Log::warning("Exception caught during checkin notification: " . $e->getMessage());
174185
}
@@ -278,6 +289,19 @@ private function getCheckinMailType($event){
278289
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
279290

280291
}
292+
private function getNotifiables($event){
293+
294+
if($event->checkedOutTo instanceof Asset){
295+
$event->checkedOutTo->load('assignedTo');
296+
return $event->checkedOutTo->assignedto?->email ?? '';
297+
}
298+
else if($event->checkedOutTo instanceof Location) {
299+
return $event->checkedOutTo->manager?->email ?? '';
300+
}
301+
else{
302+
return $event->checkedOutTo->email;
303+
}
304+
}
281305

282306
/**
283307
* Register the listeners for the subscriber.

app/Mail/CheckinLicenseMail.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CheckinLicenseMail extends Mailable
2323
public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $checkedInBy, $note)
2424
{
2525
$this->target = $checkedOutTo;
26-
$this->item = $licenseSeat->license;
26+
$this->item = $licenseSeat;
2727
$this->admin = $checkedInBy;
2828
$this->note = $note;
2929
$this->settings = Setting::getSettings();
@@ -50,7 +50,8 @@ public function content(): Content
5050
return new Content(
5151
markdown: 'mail.markdown.checkin-license',
5252
with: [
53-
'item' => $this->item,
53+
'license_seat' => $this->item,
54+
'license' => $this->item->license,
5455
'admin' => $this->admin,
5556
'note' => $this->note,
5657
'target' => $this->target,

app/Mail/CheckoutLicenseMail.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CheckoutLicenseMail extends Mailable
2222
*/
2323
public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
2424
{
25-
$this->item = $licenseSeat->license;
25+
$this->item = $licenseSeat;
2626
$this->admin = $checkedOutBy;
2727
$this->note = $note;
2828
$this->target = $checkedOutTo;
@@ -53,11 +53,11 @@ public function content(): Content
5353
$req_accept = method_exists($this->item, 'requireAcceptance') ? $this->item->requireAcceptance() : 0;
5454

5555
$accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
56-
5756
return new Content(
5857
markdown: 'mail.markdown.checkout-license',
5958
with: [
60-
'item' => $this->item,
59+
'license_seat' => $this->item,
60+
'license' => $this->item->license,
6161
'admin' => $this->admin,
6262
'note' => $this->note,
6363
'target' => $this->target,

app/Notifications/CheckinAssetNotification.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Models\Setting;
88
use App\Models\User;
99
use Illuminate\Bus\Queueable;
10+
use Illuminate\Notifications\Channels\SlackWebhookChannel;
1011
use Illuminate\Notifications\Messages\MailMessage;
1112
use Illuminate\Notifications\Messages\SlackMessage;
1213
use Illuminate\Notifications\Notification;
@@ -62,7 +63,7 @@ public function via()
6263
}
6364
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
6465
Log::debug('use webhook');
65-
$notifyBy[] = 'slack';
66+
$notifyBy[] = SlackWebhookChannel::class;
6667
}
6768

6869
return $notifyBy;

resources/views/mail/markdown/checkin-license.blade.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
@component('mail::table')
77
| | |
88
| ------------- | ------------- |
9-
| **{{ trans('mail.asset_name') }}** | {{ $item->name }} |
10-
@if (isset($item->manufacturer))
11-
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
9+
| **{{ trans('mail.asset_name') }}** | {{ $license->name }} |
10+
@if (isset($license->manufacturer))
11+
| **{{ trans('general.manufacturer') }}** | {{ $license->manufacturer->name }} |
1212
@endif
13-
@if ($target->can('update', $item))
14-
| **Key** | {{ $item->serial }} |
13+
@if (($target instanceof \App\Models\User && $target->can('view', $license)) ||($target instanceof \App\Models\Asset && $license_seat->user->can('view', $license)))
14+
| **Key** | {{ $license->serial }} |
1515
@endif
1616
@if (isset($item->category))
17-
| **{{ trans('general.category') }}** | {{ $item->category->name }} |
17+
| **{{ trans('general.category') }}** | {{ $license->category->name }} |
1818
@endif
1919
@if ($admin)
2020
| **{{ trans('general.administrator') }}** | {{ $admin->present()->fullName() }} |

resources/views/mail/markdown/checkout-license.blade.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
@if (isset($checkout_date))
1010
| **{{ trans('mail.checkout_date') }}** | {{ $checkout_date }} |
1111
@endif
12-
| **{{ trans('general.license') }}** | {{ $item->name }} |
13-
@if (isset($item->manufacturer))
14-
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
12+
| **{{ trans('general.license') }}** | {{ $license->name}} |
13+
@if (isset($license->manufacturer))
14+
| **{{ trans('general.manufacturer') }}** | {{ $license->manufacturer->name }} |
1515
@endif
16-
@if (isset($item->category))
17-
| **{{ trans('general.category') }}** | {{ $item->category->name }} |
16+
@if (isset($license->category))
17+
| **{{ trans('general.category') }}** | {{ $license->category->name }} |
1818
@endif
19-
@if ($target->can('view', $item))
20-
| **Key** | {{ $item->serial }} |
19+
@if (($target instanceof \App\Models\User && $target->can('view', $license)) || ($target instanceof \App\Models\Asset && $license_seat->user->can('view', $license)))
20+
| **Key** | {{ $license->serial }} |
2121
@endif
2222
@if ($note)
2323
| **{{ trans('mail.additional_notes') }}** | {{ $note }} |

0 commit comments

Comments
 (0)