-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Ad hoc email command #12325
Changes from 6 commits
0087ba4
fd70c76
48e30fd
c9b6086
f26209c
f5632f7
cbbaab7
957001b
57dafe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Enums\NotificationFamily; | ||
use App\Models\User; | ||
use App\Notifications\AdHocEmail; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Contracts\Console\PromptsForMissingInput; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
class SendNotificationsAdHocEmail extends Command implements PromptsForMissingInput | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'send-notifications:ad-hoc-email | ||
{templateIdEn : The template ID for the English email message} | ||
{templateIdFr : The template ID for the French email message} | ||
{--emailAddress=* : The list of contact email addresses to send the notification to} | ||
{--notificationFamily=* : The list of notification families to send the notification to} | ||
{--notifyAllUsers : Send the notification to all users} | ||
'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send ad hoc email notifications from given GC Notify templates'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$successCount = 0; | ||
$failureCount = 0; | ||
|
||
$users = $this->getUsersToSendNotificationTo(); | ||
$userCount = $users->count(); | ||
|
||
if ($this->confirm('Do you wish to send notifications to '.$userCount.' users?')) { | ||
$progressBar = $this->output->createProgressBar($userCount); | ||
$notification = new AdHocEmail($this->argument('templateIdEn'), $this->argument('templateIdFr')); | ||
|
||
$users->chunk(200, function (Collection $chunkOfUsers) use (&$successCount, &$failureCount, $progressBar, $notification) { | ||
/** @var \App\Models\User $user */ | ||
foreach ($chunkOfUsers as $user) { | ||
try { | ||
$user->notify($notification); | ||
$successCount++; | ||
} catch (\Throwable $e) { | ||
$this->error("Failed to send notification to user $user->id: ".$e->getMessage()); | ||
$failureCount++; | ||
} finally { | ||
$progressBar->advance(); | ||
} | ||
} | ||
}); | ||
$this->newLine(); | ||
|
||
$this->info("Success: $successCount Failure: $failureCount"); | ||
if ($failureCount > 0) { | ||
return Command::FAILURE; | ||
} else { | ||
return Command::SUCCESS; | ||
} | ||
} else { | ||
$this->info('Notification sending cancelled'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} | ||
|
||
private function getUsersToSendNotificationTo(): Builder | ||
{ | ||
$emailAddresses = $this->option('emailAddress'); | ||
$notificationFamilies = $this->option('notificationFamily'); | ||
$notifyAllUsers = $this->option('notifyAllUsers'); | ||
|
||
$options = $this->options(); | ||
|
||
// How many types of options were used? | ||
$optionTypesCount = | ||
(count($emailAddresses) > 0 ? 1 : 0) + | ||
(count($notificationFamilies) > 0 ? 1 : 0) + | ||
($notifyAllUsers ? 1 : 0); | ||
|
||
if ($optionTypesCount != 1) { | ||
throw new \Error('Must filter users using exactly one of the option types'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can simply use InvalidArgumentException .. but your choice :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right - that's more descriptive. |
||
} | ||
|
||
if (count($emailAddresses) > 0) { | ||
return $this->builderFromEmailAddresses($emailAddresses); | ||
} | ||
|
||
if (count($notificationFamilies) > 0) { | ||
return $this->builderFromNotifictionFamilies($notificationFamilies); | ||
} | ||
|
||
if ($notifyAllUsers) { | ||
return User::query(); | ||
} | ||
|
||
throw new \Error('Unexpected function end point for options: '.json_encode($options)); | ||
} | ||
|
||
private function builderFromEmailAddresses(array $requestedEmailAddresses): Builder | ||
{ | ||
$builder = User::whereIn('email', $requestedEmailAddresses); | ||
|
||
// Check for missing email addresses | ||
$dbEmailAddresses = $builder->pluck('email')->toArray(); | ||
$missingEmailAddresses = array_diff($requestedEmailAddresses, $dbEmailAddresses); | ||
if (count($missingEmailAddresses) > 0) { | ||
$this->alert('The following email addresses were not found:'); | ||
foreach ($missingEmailAddresses as $missingEmailAddress) { | ||
$this->warn($missingEmailAddress); | ||
} | ||
} | ||
|
||
return $builder; | ||
} | ||
|
||
private function builderFromNotifictionFamilies(array $notificationFamilies): Builder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor typo - builderFromNotificationFamilies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
{ | ||
// Check for bad notification families | ||
$allNotificationFamilies = array_column(NotificationFamily::cases(), 'name'); | ||
foreach ($notificationFamilies as $notificationFamily) { | ||
if (! in_array($notificationFamily, $allNotificationFamilies)) { | ||
throw new \Error('Invalid notification family: '.$notificationFamily); | ||
} | ||
} | ||
|
||
$builder = User::whereJsonContains('enabled_email_notifications', $notificationFamilies[0]); | ||
for ($i = 1; $i < count($notificationFamilies); $i++) { | ||
$builder->orWhereJsonContains('enabled_email_notifications', $notificationFamilies[$i]); | ||
} | ||
|
||
return $builder; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use App\Enums\Language; | ||
use App\Models\User; | ||
use App\Notifications\Messages\GcNotifyEmailMessage; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class AdHocEmail extends Notification implements CanBeSentViaGcNotifyEmail | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new notification instance. | ||
*/ | ||
public function __construct( | ||
public string $templateIdEn, | ||
public string $templateIdFr, | ||
) {} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public function via(): array | ||
{ | ||
return [GcNotifyEmailChannel::class]; | ||
} | ||
|
||
/** | ||
* Get the GC Notify representation of the notification. | ||
*/ | ||
public function toGcNotifyEmail(User $notifiable): GcNotifyEmailMessage | ||
{ | ||
$locale = $this->locale ?? $notifiable->preferredLocale(); | ||
$templateId = match ($locale) { | ||
Language::EN->value => $this->templateIdEn, | ||
Language::FR->value => $this->templateIdFr, | ||
default => throw new \InvalidArgumentException("Unsupported locale: $locale"), | ||
}; | ||
|
||
$message = new GcNotifyEmailMessage( | ||
$templateId, | ||
$notifiable->email, | ||
[ | ||
'first_name' => $notifiable->first_name, | ||
'last_name' => $notifiable->last_name, | ||
] | ||
); | ||
|
||
return $message; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oo.. progressbar!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying out some of the new Laravel 11 features. π