Fixed #5965: Allow multiple alert email addresses (#6233)

* Fixed #5965: Allow multiple alert email addresses

* Style changes based on PR feedback.
This commit is contained in:
Wes Hulette 2018-09-26 17:07:41 -04:00 committed by snipe
parent 82affd5154
commit 16e56646b8
7 changed files with 63 additions and 90 deletions

4
.gitignore vendored
View file

@ -52,3 +52,7 @@ tests/_support/_generated/*
*.cache
.vagrant
\.php_cs\.dist
phpmd\.xml

View file

@ -2,17 +2,15 @@
namespace App\Console\Commands;
use App\Models\Asset;
use App\Models\Setting;
use Illuminate\Console\Command;
use App\Notifications\ExpectedCheckinNotification;
use App\Notifications\ExpectedCheckinAdminNotification;
use App\Notifications\ExpectedCheckinNotification;
use Carbon\Carbon;
use Illuminate\Console\Command;
class SendExpectedCheckinAlerts extends Command
{
/**
* The console command name.
*
@ -29,8 +27,6 @@ class SendExpectedCheckinAlerts extends Command
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
@ -44,25 +40,25 @@ class SendExpectedCheckinAlerts extends Command
*/
public function handle()
{
$settings = Setting::getSettings();
$settings = Setting::getSettings();
$whenNotify = Carbon::now()->addDays(7);
$assets = Asset::with('assignedTo')->whereNotNull('assigned_to')->whereNotNull('expected_checkin')->where('expected_checkin', '<=', $whenNotify)->get();
$assets = Asset::with('assignedTo')->whereNotNull('assigned_to')->whereNotNull('expected_checkin')->where('expected_checkin', '<=', $whenNotify)->get();
$this->info($whenNotify.' is deadline');
$this->info($assets->count().' assets');
$this->info($whenNotify . ' is deadline');
$this->info($assets->count() . ' assets');
foreach ($assets as $asset) {
if ($asset->assigned && $asset->checkedOutToUser()) {
if ($asset->assigned && $asset->checkedOutToUser()) {
$asset->assigned->notify((new ExpectedCheckinNotification($asset)));
}
}
// Send a rollup to the admin, if settings dictate
$recipient = new \App\Models\Recipients\AlertRecipient();
if (($assets) && ($assets->count() > 0) && ($settings->alerts_enabled && $settings->alert_email != '')) {
$recipient->notify(new ExpectedCheckinAdminNotification($assets));
if (($assets) && ($assets->count() > 0) && ($settings->alert_email != '')) {
// Send a rollup to the admin, if settings dictate
$recipients = collect(explode(',', $settings->alert_email))->map(function ($item, $key) {
return new AlertRecipient($item);
});
Notification::send($recipients, new ExpectedCheckinAdminNotification($assets));
}
}
}

View file

@ -4,16 +4,14 @@ namespace App\Console\Commands;
use App\Models\Asset;
use App\Models\License;
use App\Models\Recipients\AlertRecipient;
use App\Models\Setting;
use DB;
use App\Notifications\ExpiringLicenseNotification;
use App\Notifications\ExpiringAssetsNotification;
use App\Notifications\ExpiringLicenseNotification;
use Illuminate\Console\Command;
class SendExpirationAlerts extends Command
{
/**
* The console command name.
*
@ -30,8 +28,6 @@ class SendExpirationAlerts extends Command
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
@ -45,46 +41,35 @@ class SendExpirationAlerts extends Command
*/
public function handle()
{
$settings = Setting::getSettings();
$settings = Setting::getSettings();
$threshold = $settings->alert_interval;
if (($settings->alert_email != '') && ($settings->alerts_enabled == 1)) {
// Expiring Assets
$assets = Asset::getExpiringWarrantee(Setting::getSettings()->alert_interval);
$this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count'=>$assets->count(), 'threshold' => $threshold]));
// Expiring licenses
$licenses = License::getExpiringLicenses($threshold);
$this->info(trans_choice('mail.license_expiring_alert', $licenses->count(), ['count'=>$licenses->count(), 'threshold' => $threshold]));
$recipient = new \App\Models\Recipients\AlertRecipient();
if ((Setting::getSettings()->alert_email!='') && ($settings->alerts_enabled==1)) {
// Send a rollup to the admin, if settings dictate
$recipients = collect(explode(',', $settings->alert_email))->map(function ($item, $key) {
return new AlertRecipient($item);
});
// Expiring Assets
$assets = Asset::getExpiringWarrantee(Setting::getSettings()->alert_interval);
if ($assets->count() > 0) {
// Send a rollup to the admin, if settings dictate
$recipient->notify(new ExpiringAssetsNotification($assets, $threshold));
$this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count' => $assets->count(), 'threshold' => $threshold]));
Notification::send($recipients, new ExpiringAssetsNotification($assets, $threshold));
}
// Expiring licenses
$licenses = License::getExpiringLicenses($threshold);
if ($licenses->count() > 0) {
$recipient->notify(new ExpiringLicenseNotification($licenses, $threshold));
$this->info(trans_choice('mail.license_expiring_alert', $licenses->count(), ['count' => $licenses->count(), 'threshold' => $threshold]));
Notification::send($recipients, new ExpiringLicenseNotification($licenses, $threshold));
}
} else {
if ($settings->alert_email=='') {
if ($settings->alert_email == '') {
$this->error('Could not send email. No alert email configured in settings');
} elseif ($settings->alerts_enabled!=1) {
} elseif (1 != $settings->alerts_enabled) {
$this->info('Alerts are disabled in the settings. No mail will be sent');
}
}
}
}

View file

@ -2,13 +2,12 @@
namespace App\Console\Commands;
use App\Models\Setting;
use DB;
use Mail;
use App\Helpers\Helper;
use App\Models\Recipients\AlertRecipient;
use App\Models\Setting;
use App\Notifications\InventoryAlert;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;
class SendInventoryAlerts extends Command
{
@ -28,8 +27,6 @@ class SendInventoryAlerts extends Command
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
@ -45,28 +42,24 @@ class SendInventoryAlerts extends Command
{
$settings = Setting::getSettings();
if (($settings->alert_email!='') && ($settings->alerts_enabled==1)) {
if (($settings->alert_email != '') && ($settings->alerts_enabled == 1)) {
$items = Helper::checkLowInventory();
// Send a rollup to the admin, if settings dictate
$recipient = new \App\Models\Recipients\AlertRecipient();
if (($items) && (count($items) > 0)) {
$this->info(trans_choice('mail.low_inventory_alert', count($items)));
// Send a rollup to the admin, if settings dictate
$recipients = collect(explode(',', $settings->alert_email))->map(function ($item, $key) {
return new AlertRecipient($item);
});
if (($items) && (count($items) > 0) && ($settings->alert_email!='')) {
$this->info( trans_choice('mail.low_inventory_alert',count($items)) );
$recipient->notify(new InventoryAlert($items, $settings->alert_threshold));
Notification::send($recipients, new InventoryAlert($items, $settings->alert_threshold));
}
} else {
if (Setting::getSettings()->alert_email=='') {
if ($settings->alert_email == '') {
$this->error('Could not send email. No alert email configured in settings');
} elseif (Setting::getSettings()->alerts_enabled!=1) {
} elseif (1 != $settings->alerts_enabled) {
$this->info('Alerts are disabled in the settings. No mail will be sent');
}
}
}
}

View file

@ -1,14 +1,11 @@
<?php
namespace App\Models\Recipients;
use App\Models\Setting;
class AlertRecipient extends Recipient{
public function __construct()
class AlertRecipient extends Recipient
{
public function __construct(string $email)
{
$settings = Setting::getSettings();
$this->email = $settings->alert_email;
$this->email = trim($email);
}
}

View file

@ -1,12 +1,12 @@
<?php
namespace App\Models\Recipients;
use Illuminate\Notifications\Notifiable;
abstract class Recipient {
abstract class Recipient
{
use Notifiable;
protected $email;
}

View file

@ -3,8 +3,8 @@
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class InventoryAlert extends Notification
{
@ -33,14 +33,13 @@ class InventoryAlert extends Notification
*/
public function via($notifiable)
{
$notifyBy = [];
$notifyBy[]='mail';
$notifyBy[] = 'mail';
return $notifyBy;
}
public function toSlack($notifiable)
{
}
/**
@ -51,17 +50,16 @@ class InventoryAlert extends Notification
*/
public function toMail($params)
{
$message = (new MailMessage)->markdown('notifications.markdown.report-low-inventory',
$message = (new MailMessage)->markdown(
'notifications.markdown.report-low-inventory',
[
'items' => $this->items,
'threshold' => $this->threshold,
])
]
)
->subject(trans('mail.Low_Inventory_Report'));
return $message;
}
/**