diff --git a/app/Console/Commands/SendAcceptanceReminder.php b/app/Console/Commands/SendAcceptanceReminder.php index 1551348046..754d189720 100644 --- a/app/Console/Commands/SendAcceptanceReminder.php +++ b/app/Console/Commands/SendAcceptanceReminder.php @@ -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; } + } diff --git a/app/Mail/UnacceptedAssetReminderMail.php b/app/Mail/UnacceptedAssetReminderMail.php new file mode 100644 index 0000000000..1436bbc84e --- /dev/null +++ b/app/Mail/UnacceptedAssetReminderMail.php @@ -0,0 +1,67 @@ +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 + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Notifications/UnacceptedAssetReminderNotification.php b/app/Notifications/UnacceptedAssetReminderNotification.php deleted file mode 100644 index e05b007033..0000000000 --- a/app/Notifications/UnacceptedAssetReminderNotification.php +++ /dev/null @@ -1,73 +0,0 @@ -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 [ - // - ]; - } -} diff --git a/tests/Feature/Console/SendAcceptanceReminderTest.php b/tests/Feature/Console/SendAcceptanceReminderTest.php new file mode 100644 index 0000000000..9743a9db49 --- /dev/null +++ b/tests/Feature/Console/SendAcceptanceReminderTest.php @@ -0,0 +1,53 @@ +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); + } +}