Skip to content

Commit

Permalink
fix duplicate emails being sent in unaccepted reminder command
Browse files Browse the repository at this point in the history
  • Loading branch information
Godmartinz committed Dec 5, 2024
1 parent 52b051e commit 3f8916e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
34 changes: 10 additions & 24 deletions app/Console/Commands/SendAcceptanceReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
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
{
Expand Down Expand Up @@ -69,39 +68,26 @@ public function handle()
$no_mail_address = [];

foreach($unacceptedAssetGroups as $unacceptedAssetGroup) {

$locale = $unacceptedAssetGroup[0]['acceptance']->assignedTo?->locale;
$email = $unacceptedAssetGroup[0]['acceptance']->assignedTo?->email;
$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 UnacceptedAssetReminderMail($unacceptedAsset['assetItem'], $count)
);
} else {
Notification::send(
$unacceptedAsset['acceptance']->assignedTo,
new UnacceptedAssetReminderMail($unacceptedAsset, $item_count)
);
}
$count++;
}
if ($locale && $email) {
Mail::to($email)->send((new UnacceptedAssetReminderMail($unacceptedAssetGroup[0]['acceptance'], $item_count))->locale($locale));

} elseif ($email) {
Mail::to($email)->send((new UnacceptedAssetReminderMail($unacceptedAssetGroup[0]['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.');
}
}
4 changes: 2 additions & 2 deletions app/Mail/UnacceptedAssetReminderMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnacceptedAssetReminderMail extends Mailable
public function __construct($checkout_info, $count)
{
$this->count = $count;
$this->target = $checkout_info['acceptance']->assignedTo;
$this->target = $checkout_info['acceptance']?->assignedTo;
$this->acceptance = $checkout_info['acceptance'];
}

Expand Down Expand Up @@ -48,7 +48,7 @@ public function content(): Content
markdown: 'notifications.markdown.asset-reminder',
with: [
'count' => $this->count,
'assigned_to' => $this->target->present()->fullName,
'assigned_to' => $this->target?->present()->fullName,
'link' => route('account.accept'),
'accept_url' => $accept_url,
]
Expand Down

0 comments on commit 3f8916e

Please sign in to comment.