-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from pinkary-project/refactor/email-commands
- Loading branch information
Showing
11 changed files
with
163 additions
and
178 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
app/Console/Commands/SendUnreadNotificationEmailsCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Enums\UserMailPreference; | ||
use App\Mail\PendingNotifications; | ||
use App\Models\User; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Facades\Mail; | ||
|
||
final class SendUnreadNotificationEmailsCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'send:unread-notification-emails | ||
{--weekly : Send the unread notification emails to the users who have set their mail preference to weekly.}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send the unread notification emails to the users.'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
User::query() | ||
->when($this->option('weekly'), function (Builder $query): void { | ||
$query->where('mail_preference_time', UserMailPreference::Weekly); | ||
}, function (Builder $query): void { | ||
$query->where('mail_preference_time', UserMailPreference::Daily); | ||
}) | ||
->whereHas('notifications') | ||
->withCount('notifications') | ||
->each(fn (User $user) => Mail::to($user)->queue(new PendingNotifications($user, $user->notifications_count))); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Console\Commands\SendUnreadNotificationEmailsCommand; | ||
use App\Enums\UserMailPreference; | ||
use App\Mail\PendingNotifications; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Mail; | ||
|
||
test('sends daily emails', function () { | ||
User::factory(5)->create(); | ||
|
||
User::factory(2)->create([ | ||
'mail_preference_time' => UserMailPreference::Weekly, | ||
]); | ||
|
||
User::factory(2)->create([ | ||
'mail_preference_time' => UserMailPreference::Never, | ||
]); | ||
|
||
$questioner = User::factory()->create([ | ||
'mail_preference_time' => UserMailPreference::Never, | ||
]); | ||
|
||
User::all()->each(fn (User $user) => $questioner->questionsSent()->create([ | ||
'to_id' => $user->id, | ||
'content' => 'What is the meaning of life?', | ||
])); | ||
|
||
$questioner->questionsSent()->create([ | ||
'to_id' => $questioner->id, | ||
'content' => 'Sharing updates will not create a new notification.', | ||
]); | ||
|
||
Mail::fake(); | ||
|
||
$this->artisan(SendUnreadNotificationEmailsCommand::class) | ||
->assertExitCode(0); | ||
|
||
Mail::assertQueued(PendingNotifications::class, 5); | ||
}); | ||
|
||
test('sends weekly emails', function () { | ||
User::factory(5)->create(); | ||
|
||
User::factory(2)->create([ | ||
'mail_preference_time' => UserMailPreference::Weekly, | ||
]); | ||
|
||
User::factory(2)->create([ | ||
'mail_preference_time' => UserMailPreference::Never, | ||
]); | ||
|
||
$questioner = User::factory()->create([ | ||
'mail_preference_time' => UserMailPreference::Never, | ||
]); | ||
|
||
User::all()->each(fn (User $user) => $questioner->questionsSent()->create([ | ||
'to_id' => $user->id, | ||
'content' => 'What is the meaning of life?', | ||
])); | ||
|
||
$questioner->questionsSent()->create([ | ||
'to_id' => $questioner->id, | ||
'content' => 'Sharing updates will not create a new notification.', | ||
]); | ||
|
||
Mail::fake(); | ||
|
||
$this->artisan(SendUnreadNotificationEmailsCommand::class, ['--weekly' => true]) | ||
->assertExitCode(0); | ||
|
||
Mail::assertQueued(PendingNotifications::class, 2); | ||
}); | ||
|
||
test('mails getting notification count', function () { | ||
$user = User::factory()->create([ | ||
'mail_preference_time' => UserMailPreference::Daily, | ||
]); | ||
|
||
$questioner = User::factory()->create(); | ||
|
||
$questioner->questionsSent()->create([ | ||
'to_id' => $user->id, | ||
'content' => 'What is the meaning of life?', | ||
]); | ||
|
||
$questioner->questionsSent()->create([ | ||
'to_id' => $user->id, | ||
'content' => 'What is the meaning of life? ignoring?', | ||
]); | ||
|
||
$questioner->questionsSent()->create([ | ||
'to_id' => $user->id, | ||
'content' => 'What is the meaning of life? please answer me.', | ||
]); | ||
|
||
Mail::fake(); | ||
|
||
$this->artisan(SendUnreadNotificationEmailsCommand::class) | ||
->assertExitCode(0); | ||
|
||
Mail::assertQueued(PendingNotifications::class, function (PendingNotifications $mail) { | ||
return $mail->pendingNotificationsCount === 3; | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.