mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-03 18:07:41 -08:00
Merge pull request #15925 from Godmartinz/refactor-unaccepted-assets-reminder-notif
This commit is contained in:
commit
f72635955d
|
@ -2,15 +2,15 @@
|
||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Mail\UnacceptedAssetReminderMail;
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use App\Models\CheckoutAcceptance;
|
use App\Models\CheckoutAcceptance;
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Notifications\CheckoutAssetNotification;
|
use App\Notifications\CheckoutAssetNotification;
|
||||||
use App\Notifications\CurrentInventory;
|
use App\Notifications\CurrentInventory;
|
||||||
use App\Notifications\UnacceptedAssetReminderNotification;
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Facades\Notification;
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
|
||||||
class SendAcceptanceReminder extends Command
|
class SendAcceptanceReminder extends Command
|
||||||
{
|
{
|
||||||
|
@ -65,42 +65,29 @@ class SendAcceptanceReminder extends Command
|
||||||
return $item['acceptance']->assignedTo ? $item['acceptance']->assignedTo->id : '';
|
return $item['acceptance']->assignedTo ? $item['acceptance']->assignedTo->id : '';
|
||||||
});
|
});
|
||||||
|
|
||||||
$no_mail_address = [];
|
|
||||||
|
|
||||||
foreach($unacceptedAssetGroups as $unacceptedAssetGroup) {
|
foreach($unacceptedAssetGroups as $unacceptedAssetGroup) {
|
||||||
|
// The [0] is weird, but it allows for the item_count to work and grabs the appropriate info for each user.
|
||||||
|
// Collapsing and flattening the collection doesn't work above.
|
||||||
|
$acceptance = $unacceptedAssetGroup[0]['acceptance'];
|
||||||
|
$locale = $acceptance->assignedTo?->locale;
|
||||||
|
$email = $acceptance->assignedTo?->email;
|
||||||
|
if(!$email){
|
||||||
|
$this->info($acceptance->assignedTo->present()->fullName().' has no email address.');
|
||||||
|
}
|
||||||
$item_count = $unacceptedAssetGroup->count();
|
$item_count = $unacceptedAssetGroup->count();
|
||||||
foreach ($unacceptedAssetGroup as $unacceptedAsset) {
|
|
||||||
// if ($unacceptedAsset['acceptance']->assignedTo->email == ''){
|
|
||||||
// $no_mail_address[] = $unacceptedAsset['checkoutable']->assignedTo->present()->fullName;
|
|
||||||
// }
|
|
||||||
if ($unacceptedAsset['acceptance']->assignedTo) {
|
|
||||||
|
|
||||||
if (!$unacceptedAsset['acceptance']->assignedTo->locale) {
|
if ($locale && $email) {
|
||||||
Notification::locale(Setting::getSettings()->locale)->send(
|
Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count))->locale($locale));
|
||||||
$unacceptedAsset['acceptance']->assignedTo,
|
|
||||||
new UnacceptedAssetReminderNotification($unacceptedAsset['assetItem'], $count)
|
} elseif ($email) {
|
||||||
);
|
Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count)));
|
||||||
} else {
|
|
||||||
Notification::send(
|
|
||||||
$unacceptedAsset['acceptance']->assignedTo,
|
|
||||||
new UnacceptedAssetReminderNotification($unacceptedAsset, $item_count)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$count++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($no_mail_address)) {
|
|
||||||
foreach($no_mail_address as $user) {
|
|
||||||
return $user.' has no email.';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$this->info($count.' users notified.');
|
$this->info($count.' users notified.');
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
67
app/Mail/UnacceptedAssetReminderMail.php
Normal file
67
app/Mail/UnacceptedAssetReminderMail.php
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Mail;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Mail\Mailables\Address;
|
||||||
|
use Illuminate\Mail\Mailables\Content;
|
||||||
|
use Illuminate\Mail\Mailables\Envelope;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class UnacceptedAssetReminderMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*/
|
||||||
|
public function __construct($checkout_info, $count)
|
||||||
|
{
|
||||||
|
$this->count = $count;
|
||||||
|
$this->target = $checkout_info['acceptance']?->assignedTo;
|
||||||
|
$this->acceptance = $checkout_info['acceptance'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message envelope.
|
||||||
|
*/
|
||||||
|
public function envelope(): Envelope
|
||||||
|
{
|
||||||
|
$from = new Address(config('mail.from.address'), config('mail.from.name'));
|
||||||
|
|
||||||
|
return new Envelope(
|
||||||
|
from: $from,
|
||||||
|
subject: trans('mail.unaccepted_asset_reminder'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message content definition.
|
||||||
|
*/
|
||||||
|
public function content(): Content
|
||||||
|
{
|
||||||
|
$accept_url = route('account.accept');
|
||||||
|
|
||||||
|
return new Content(
|
||||||
|
markdown: 'notifications.markdown.asset-reminder',
|
||||||
|
with: [
|
||||||
|
'count' => $this->count,
|
||||||
|
'assigned_to' => $this->target?->present()->fullName,
|
||||||
|
'link' => route('account.accept'),
|
||||||
|
'accept_url' => $accept_url,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the attachments for the message.
|
||||||
|
*
|
||||||
|
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||||
|
*/
|
||||||
|
public function attachments(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,73 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Notifications;
|
|
||||||
|
|
||||||
use App\Models\Asset;
|
|
||||||
use App\Models\User;
|
|
||||||
use Illuminate\Bus\Queueable;
|
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Notifications\Messages\MailMessage;
|
|
||||||
use Illuminate\Notifications\Notification;
|
|
||||||
|
|
||||||
class UnacceptedAssetReminderNotification extends Notification
|
|
||||||
{
|
|
||||||
use Queueable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new notification instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct($checkout_info, $count)
|
|
||||||
{
|
|
||||||
$this->count = $count;
|
|
||||||
$this->target = $checkout_info['acceptance']->assignedTo;
|
|
||||||
$this->acceptance = $checkout_info['acceptance'];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the notification's delivery channels.
|
|
||||||
*
|
|
||||||
* @param mixed $notifiable
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function via()
|
|
||||||
{
|
|
||||||
return ['mail'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the mail representation of the notification.
|
|
||||||
*
|
|
||||||
* @param mixed $notifiable
|
|
||||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
||||||
*/
|
|
||||||
public function toMail()
|
|
||||||
{
|
|
||||||
$accept_url = route('account.accept');
|
|
||||||
$message = (new MailMessage)->markdown('notifications.markdown.asset-reminder',
|
|
||||||
[
|
|
||||||
'count' => $this->count,
|
|
||||||
'assigned_to' => $this->target->present()->fullName,
|
|
||||||
'link' => route('account.accept'),
|
|
||||||
'accept_url' => $accept_url,
|
|
||||||
])
|
|
||||||
->subject(trans('mail.unaccepted_asset_reminder'));
|
|
||||||
|
|
||||||
return $message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the array representation of the notification.
|
|
||||||
*
|
|
||||||
* @param mixed $notifiable
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function toArray($notifiable)
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
//
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
53
tests/Feature/Console/SendAcceptanceReminderTest.php
Normal file
53
tests/Feature/Console/SendAcceptanceReminderTest.php
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Console;
|
||||||
|
|
||||||
|
use App\Mail\UnacceptedAssetReminderMail;
|
||||||
|
use App\Models\CheckoutAcceptance;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
use Tests\TestCase;
|
||||||
|
class SendAcceptanceReminderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testAcceptanceReminderCommand()
|
||||||
|
{
|
||||||
|
Mail::fake();
|
||||||
|
$userA = User::factory()->create(['email' => 'userA@test.com']);
|
||||||
|
$userB = User::factory()->create(['email' => 'userB@test.com']);
|
||||||
|
|
||||||
|
CheckoutAcceptance::factory()->pending()->count(2)->create([
|
||||||
|
'assigned_to_id' => $userA->id,
|
||||||
|
]);
|
||||||
|
CheckoutAcceptance::factory()->pending()->create([
|
||||||
|
'assigned_to_id' => $userB->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('snipeit:acceptance-reminder')->assertExitCode(0);
|
||||||
|
|
||||||
|
Mail::assertSent(UnacceptedAssetReminderMail::class, function ($mail) {
|
||||||
|
return $mail->hasTo('userA@test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
Mail::assertSent(UnacceptedAssetReminderMail::class, function ($mail) {
|
||||||
|
return $mail->hasTo('userB@test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
Mail::assertSent(UnacceptedAssetReminderMail::class,2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAcceptanceReminderCommandHandlesUserWithoutEmail()
|
||||||
|
{
|
||||||
|
Mail::fake();
|
||||||
|
$userA = User::factory()->create(['email' => '']);
|
||||||
|
|
||||||
|
CheckoutAcceptance::factory()->pending()->create([
|
||||||
|
'assigned_to_id' => $userA->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->artisan('snipeit:acceptance-reminder')
|
||||||
|
->expectsOutput($userA->present()->fullName().' has no email address.')
|
||||||
|
->assertExitCode(0);
|
||||||
|
|
||||||
|
Mail::assertNotSent(UnacceptedAssetReminderMail::class);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue