Merge pull request #15925 from Godmartinz/refactor-unaccepted-assets-reminder-notif

This commit is contained in:
snipe 2024-12-09 16:40:21 +00:00 committed by GitHub
commit f72635955d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 139 additions and 105 deletions

View file

@ -2,15 +2,15 @@
namespace App\Console\Commands;
use App\Mail\UnacceptedAssetReminderMail;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckoutAssetNotification;
use App\Notifications\CurrentInventory;
use App\Notifications\UnacceptedAssetReminderNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Mail;
class SendAcceptanceReminder extends Command
{
@ -65,42 +65,29 @@ class SendAcceptanceReminder extends Command
return $item['acceptance']->assignedTo ? $item['acceptance']->assignedTo->id : '';
});
$no_mail_address = [];
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();
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) {
Notification::locale(Setting::getSettings()->locale)->send(
$unacceptedAsset['acceptance']->assignedTo,
new UnacceptedAssetReminderNotification($unacceptedAsset['assetItem'], $count)
);
} else {
Notification::send(
$unacceptedAsset['acceptance']->assignedTo,
new UnacceptedAssetReminderNotification($unacceptedAsset, $item_count)
);
}
$count++;
}
if ($locale && $email) {
Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count))->locale($locale));
} elseif ($email) {
Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count)));
}
$count++;
}
if (!empty($no_mail_address)) {
foreach($no_mail_address as $user) {
return $user.' has no email.';
}
}
$this->info($count.' users notified.');
return 0;
}
}

View 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 [];
}
}

View file

@ -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 [
//
];
}
}

View 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);
}
}