Skip to content

Commit f726359

Browse files
authored
Merge pull request #15925 from Godmartinz/refactor-unaccepted-assets-reminder-notif
2 parents a137e31 + 5120cdd commit f726359

File tree

4 files changed

+138
-104
lines changed

4 files changed

+138
-104
lines changed

app/Console/Commands/SendAcceptanceReminder.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace App\Console\Commands;
44

5+
use App\Mail\UnacceptedAssetReminderMail;
56
use App\Models\Asset;
67
use App\Models\CheckoutAcceptance;
78
use App\Models\Setting;
89
use App\Models\User;
910
use App\Notifications\CheckoutAssetNotification;
1011
use App\Notifications\CurrentInventory;
11-
use App\Notifications\UnacceptedAssetReminderNotification;
1212
use Illuminate\Console\Command;
13-
use Illuminate\Support\Facades\Notification;
13+
use Illuminate\Support\Facades\Mail;
1414

1515
class SendAcceptanceReminder extends Command
1616
{
@@ -65,42 +65,29 @@ public function handle()
6565
return $item['acceptance']->assignedTo ? $item['acceptance']->assignedTo->id : '';
6666
});
6767

68-
$no_mail_address = [];
69-
7068
foreach($unacceptedAssetGroups as $unacceptedAssetGroup) {
69+
// The [0] is weird, but it allows for the item_count to work and grabs the appropriate info for each user.
70+
// Collapsing and flattening the collection doesn't work above.
71+
$acceptance = $unacceptedAssetGroup[0]['acceptance'];
72+
$locale = $acceptance->assignedTo?->locale;
73+
$email = $acceptance->assignedTo?->email;
74+
if(!$email){
75+
$this->info($acceptance->assignedTo->present()->fullName().' has no email address.');
76+
}
7177
$item_count = $unacceptedAssetGroup->count();
72-
foreach ($unacceptedAssetGroup as $unacceptedAsset) {
73-
// if ($unacceptedAsset['acceptance']->assignedTo->email == ''){
74-
// $no_mail_address[] = $unacceptedAsset['checkoutable']->assignedTo->present()->fullName;
75-
// }
76-
if ($unacceptedAsset['acceptance']->assignedTo) {
7778

78-
if (!$unacceptedAsset['acceptance']->assignedTo->locale) {
79-
Notification::locale(Setting::getSettings()->locale)->send(
80-
$unacceptedAsset['acceptance']->assignedTo,
81-
new UnacceptedAssetReminderNotification($unacceptedAsset['assetItem'], $count)
82-
);
83-
} else {
84-
Notification::send(
85-
$unacceptedAsset['acceptance']->assignedTo,
86-
new UnacceptedAssetReminderNotification($unacceptedAsset, $item_count)
87-
);
88-
}
89-
$count++;
90-
}
91-
}
92-
}
79+
if ($locale && $email) {
80+
Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count))->locale($locale));
9381

94-
if (!empty($no_mail_address)) {
95-
foreach($no_mail_address as $user) {
96-
return $user.' has no email.';
82+
} elseif ($email) {
83+
Mail::to($email)->send((new UnacceptedAssetReminderMail($acceptance, $item_count)));
9784
}
98-
99-
85+
$count++;
10086
}
10187

102-
103-
10488
$this->info($count.' users notified.');
89+
90+
return 0;
10591
}
92+
10693
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Mail\Mailables\Address;
9+
use Illuminate\Mail\Mailables\Content;
10+
use Illuminate\Mail\Mailables\Envelope;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class UnacceptedAssetReminderMail extends Mailable
14+
{
15+
use Queueable, SerializesModels;
16+
17+
/**
18+
* Create a new message instance.
19+
*/
20+
public function __construct($checkout_info, $count)
21+
{
22+
$this->count = $count;
23+
$this->target = $checkout_info['acceptance']?->assignedTo;
24+
$this->acceptance = $checkout_info['acceptance'];
25+
}
26+
27+
/**
28+
* Get the message envelope.
29+
*/
30+
public function envelope(): Envelope
31+
{
32+
$from = new Address(config('mail.from.address'), config('mail.from.name'));
33+
34+
return new Envelope(
35+
from: $from,
36+
subject: trans('mail.unaccepted_asset_reminder'),
37+
);
38+
}
39+
40+
/**
41+
* Get the message content definition.
42+
*/
43+
public function content(): Content
44+
{
45+
$accept_url = route('account.accept');
46+
47+
return new Content(
48+
markdown: 'notifications.markdown.asset-reminder',
49+
with: [
50+
'count' => $this->count,
51+
'assigned_to' => $this->target?->present()->fullName,
52+
'link' => route('account.accept'),
53+
'accept_url' => $accept_url,
54+
]
55+
);
56+
}
57+
58+
/**
59+
* Get the attachments for the message.
60+
*
61+
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
62+
*/
63+
public function attachments(): array
64+
{
65+
return [];
66+
}
67+
}

app/Notifications/UnacceptedAssetReminderNotification.php

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Tests\Feature\Console;
4+
5+
use App\Mail\UnacceptedAssetReminderMail;
6+
use App\Models\CheckoutAcceptance;
7+
use App\Models\User;
8+
use Illuminate\Support\Facades\Mail;
9+
use Tests\TestCase;
10+
class SendAcceptanceReminderTest extends TestCase
11+
{
12+
public function testAcceptanceReminderCommand()
13+
{
14+
Mail::fake();
15+
$userA = User::factory()->create(['email' => '[email protected]']);
16+
$userB = User::factory()->create(['email' => '[email protected]']);
17+
18+
CheckoutAcceptance::factory()->pending()->count(2)->create([
19+
'assigned_to_id' => $userA->id,
20+
]);
21+
CheckoutAcceptance::factory()->pending()->create([
22+
'assigned_to_id' => $userB->id,
23+
]);
24+
25+
$this->artisan('snipeit:acceptance-reminder')->assertExitCode(0);
26+
27+
Mail::assertSent(UnacceptedAssetReminderMail::class, function ($mail) {
28+
return $mail->hasTo('[email protected]');
29+
});
30+
31+
Mail::assertSent(UnacceptedAssetReminderMail::class, function ($mail) {
32+
return $mail->hasTo('[email protected]');
33+
});
34+
35+
Mail::assertSent(UnacceptedAssetReminderMail::class,2);
36+
}
37+
38+
public function testAcceptanceReminderCommandHandlesUserWithoutEmail()
39+
{
40+
Mail::fake();
41+
$userA = User::factory()->create(['email' => '']);
42+
43+
CheckoutAcceptance::factory()->pending()->create([
44+
'assigned_to_id' => $userA->id,
45+
]);
46+
47+
$this->artisan('snipeit:acceptance-reminder')
48+
->expectsOutput($userA->present()->fullName().' has no email address.')
49+
->assertExitCode(0);
50+
51+
Mail::assertNotSent(UnacceptedAssetReminderMail::class);
52+
}
53+
}

0 commit comments

Comments
 (0)