Skip to content

Commit 83855d4

Browse files
authored
Merge pull request #16251 from Godmartinz/Audit_Checkin_warning_fix
refactors audit notification to mail, adds test, adds alerts check to scheduler
2 parents 6f84729 + 4c43a06 commit 83855d4

File tree

5 files changed

+123
-15
lines changed

5 files changed

+123
-15
lines changed

app/Console/Commands/SendUpcomingAuditReport.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace App\Console\Commands;
44

5+
use App\Mail\SendUpcomingAuditMail;
56
use App\Models\Asset;
6-
use App\Models\Recipients\AlertRecipient;
77
use App\Models\Setting;
8-
use App\Notifications\SendUpcomingAuditNotification;
98
use Carbon\Carbon;
10-
use Illuminate\Support\Facades\DB;
119
use Illuminate\Console\Command;
10+
use Illuminate\Support\Facades\Mail;
1211

1312
class SendUpcomingAuditReport extends Command
1413
{
@@ -48,19 +47,19 @@ public function handle()
4847
$today = Carbon::now();
4948
$interval_date = $today->copy()->addDays($interval);
5049

51-
$assets = Asset::whereNull('deleted_at')->DueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'desc')->get();
52-
$this->info($assets->count().' assets must be audited in on or before '.$interval_date.' is deadline');
50+
$assets = Asset::whereNull('deleted_at')->dueOrOverdueForAudit($settings)->orderBy('assets.next_audit_date', 'desc')->get();
51+
$this->info($assets->count() . ' assets must be audited in on or before ' . $interval_date . ' is deadline');
5352

5453

55-
if (($assets) && ($assets->count() > 0) && ($settings->alert_email != '')) {
54+
if ((count($assets) !== 0) && ($assets->count() > 0) && ($settings->alert_email != '')) {
5655
// Send a rollup to the admin, if settings dictate
57-
$recipients = collect(explode(',', $settings->alert_email))->map(function ($item) {
58-
return new AlertRecipient($item);
59-
});
56+
$recipients = collect(explode(',', $settings->alert_email))
57+
->map(fn($item) => trim($item))
58+
->all();
6059

61-
$this->info('Sending Admin SendUpcomingAuditNotification to: '.$settings->alert_email);
62-
\Notification::send($recipients, new SendUpcomingAuditNotification($assets, $settings->audit_warning_days));
6360

61+
$this->info('Sending Admin SendUpcomingAuditNotification to: ' . $settings->alert_email);
62+
Mail::to($recipients)->send(new SendUpcomingAuditMail($assets, $settings->audit_warning_days));
6463
}
6564

6665
}

app/Console/Kernel.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Console\Commands\ImportLocations;
66
use App\Console\Commands\ReEncodeCustomFieldNames;
77
use App\Console\Commands\RestoreDeletedUsers;
8+
use App\Models\Setting;
89
use Illuminate\Console\Scheduling\Schedule;
910
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
1011

@@ -18,12 +19,14 @@ class Kernel extends ConsoleKernel
1819
*/
1920
protected function schedule(Schedule $schedule)
2021
{
21-
$schedule->command('snipeit:inventory-alerts')->daily();
22-
$schedule->command('snipeit:expiring-alerts')->daily();
23-
$schedule->command('snipeit:expected-checkin')->daily();
22+
if(Setting::getSettings()->alerts_enabled === 1) {
23+
$schedule->command('snipeit:inventory-alerts')->daily();
24+
$schedule->command('snipeit:expiring-alerts')->daily();
25+
$schedule->command('snipeit:expected-checkin')->daily();
26+
$schedule->command('snipeit:upcoming-audits')->daily();
27+
}
2428
$schedule->command('snipeit:backup')->weekly();
2529
$schedule->command('backup:clean')->daily();
26-
$schedule->command('snipeit:upcoming-audits')->daily();
2730
$schedule->command('auth:clear-resets')->everyFifteenMinutes();
2831
$schedule->command('saml:clear_expired_nonces')->weekly();
2932
}

app/Mail/SendUpcomingAuditMail.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Mail\Mailables\Address;
9+
use Illuminate\Mail\Mailables\Content;
10+
use Illuminate\Mail\Mailables\Envelope;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class SendUpcomingAuditMail extends Mailable
14+
{
15+
use Queueable, SerializesModels;
16+
17+
/**
18+
* Create a new message instance.
19+
*/
20+
public function __construct($params, $threshold)
21+
{
22+
$this->assets = $params;
23+
$this->threshold = $threshold;
24+
}
25+
26+
/**
27+
* Get the message envelope.
28+
*/
29+
public function envelope(): Envelope
30+
{
31+
$from = new Address(config('mail.from.address'), config('mail.from.name'));
32+
33+
return new Envelope(
34+
from: $from,
35+
subject: trans_choice('mail.upcoming-audits', $this->assets->count(), ['count' => $this->assets->count(), 'threshold' => $this->threshold]),
36+
);
37+
}
38+
39+
/**
40+
* Get the message content definition.
41+
*/
42+
public function content(): Content
43+
{
44+
45+
46+
return new Content(
47+
48+
markdown: 'notifications.markdown.upcoming-audits',
49+
with: [
50+
'assets' => $this->assets,
51+
'threshold' => $this->threshold,
52+
],
53+
);
54+
}
55+
56+
/**
57+
* Get the attachments for the message.
58+
*
59+
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
60+
*/
61+
public function attachments(): array
62+
{
63+
return [];
64+
}
65+
}

tests/Feature/Notifications/Email/ExpiringAlertsNotificationTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Mail\ExpiringAssetsMail;
66
use App\Mail\ExpiringLicenseMail;
7+
use App\Mail\SendUpcomingAuditMail;
78
use App\Models\Asset;
89
use App\Models\License;
910
use App\Models\Setting;
@@ -88,4 +89,38 @@ public function testExpiringLicensesEmailNotification()
8889
return $mail->licenses->contains($expiredLicense) || $mail->licenses->contains($notExpiringLicense);
8990
});
9091
}
92+
93+
public function testAuditWarningThresholdEmailNotification()
94+
{
95+
$this->markIncompleteIfSqlite();
96+
Mail::fake();
97+
$this->settings->enableAlertEmail('[email protected]');
98+
$this->settings->setAuditWarningDays(15);
99+
100+
$alert_email = Setting::first()->alert_email;
101+
102+
$upcomingAuditableAsset = Asset::factory()->create([
103+
'next_audit_date' => now()->addDays(14)->format('Y-m-d'),
104+
'deleted_at' => null,
105+
]);
106+
107+
$overDueForAuditableAsset = Asset::factory()->create([
108+
'next_audit_date' => now()->subDays(1)->format('Y-m-d'),
109+
'deleted_at' => null,
110+
]);
111+
112+
$notAuditableAsset = Asset::factory()->create([
113+
'next_audit_date' => now()->addDays(30)->format('Y-m-d'),
114+
'deleted_at' => null,
115+
]);
116+
117+
$this->artisan('snipeit:upcoming-audits')->assertExitCode(0);
118+
119+
Mail::assertSent(SendUpcomingAuditMail::class, function($mail) use ($alert_email, $upcomingAuditableAsset, $overDueForAuditableAsset) {
120+
return $mail->hasTo($alert_email) && ($mail->assets->contains($upcomingAuditableAsset) && $mail->assets->contains($overDueForAuditableAsset));
121+
});
122+
Mail::assertNotSent(SendUpcomingAuditMail::class, function($mail) use ($alert_email, $notAuditableAsset) {
123+
return $mail->hasTo($alert_email) && $mail->assets->contains($notAuditableAsset);
124+
});
125+
}
91126
}

tests/Support/Settings.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public function setAlertInterval(int $days): Settings
3232
'alert_threshold' => $days,
3333
]);
3434
}
35+
public function setAuditWarningDays(int $days): Settings
36+
{
37+
return $this->update([
38+
'audit_warning_days' => $days,
39+
]);
40+
}
3541
public function disableAlertEmail(): Settings
3642
{
3743
return $this->update([

0 commit comments

Comments
 (0)