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 *.cache
.vagrant .vagrant
\.php_cs\.dist
phpmd\.xml

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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