diff --git a/app/Console/Commands/SendDailyEmailsCommand.php b/app/Console/Commands/SendDailyEmailsCommand.php deleted file mode 100644 index f4a9caf4d..000000000 --- a/app/Console/Commands/SendDailyEmailsCommand.php +++ /dev/null @@ -1,38 +0,0 @@ -whereHas('notifications') - ->each(fn (User $user) => Mail::to($user)->queue(new PendingNotifications($user))); - } -} diff --git a/app/Console/Commands/SendUnreadNotificationEmailsCommand.php b/app/Console/Commands/SendUnreadNotificationEmailsCommand.php new file mode 100644 index 000000000..d0a471f13 --- /dev/null +++ b/app/Console/Commands/SendUnreadNotificationEmailsCommand.php @@ -0,0 +1,46 @@ +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))); + } +} diff --git a/app/Console/Commands/SendWeeklyEmailsCommand.php b/app/Console/Commands/SendWeeklyEmailsCommand.php deleted file mode 100644 index b04f06b7f..000000000 --- a/app/Console/Commands/SendWeeklyEmailsCommand.php +++ /dev/null @@ -1,38 +0,0 @@ -whereHas('notifications') - ->each(fn (User $user) => Mail::to($user)->queue(new PendingNotifications($user))); - } -} diff --git a/app/Mail/PendingNotifications.php b/app/Mail/PendingNotifications.php index f1857a838..b260f2edf 100644 --- a/app/Mail/PendingNotifications.php +++ b/app/Mail/PendingNotifications.php @@ -20,7 +20,8 @@ final class PendingNotifications extends Mailable implements ShouldQueue * Create a new message instance. */ public function __construct( - private readonly User $user, + public readonly User $user, + public readonly int $pendingNotificationsCount, ) { // } @@ -30,10 +31,8 @@ public function __construct( */ public function envelope(): Envelope { - $notificationsCount = $this->user->notifications()->count(); - return new Envelope( - subject: '🌸 Pinkary: You Have '.$notificationsCount.' '.str('Notification')->plural($notificationsCount).'! - '.now()->format('F j, Y'), + subject: '🌸 Pinkary: You Have '.$this->pendingNotificationsCount.' '.str('Notification')->plural($this->pendingNotificationsCount).'! - '.now()->format('F j, Y'), ); } @@ -47,7 +46,7 @@ public function content(): Content with: [ 'date' => now()->format('Y-m-d'), 'user' => $this->user, - 'pendingNotificationsCount' => $this->user->notifications()->count(), + 'pendingNotificationsCount' => $this->pendingNotificationsCount, ], ); } diff --git a/public/img/logo-text.png b/public/img/logo-text.png new file mode 100644 index 000000000..c1f061014 Binary files /dev/null and b/public/img/logo-text.png differ diff --git a/resources/views/vendor/mail/html/header.blade.php b/resources/views/vendor/mail/html/header.blade.php index e802703c1..d119cdc8b 100644 --- a/resources/views/vendor/mail/html/header.blade.php +++ b/resources/views/vendor/mail/html/header.blade.php @@ -2,7 +2,7 @@ - + diff --git a/routes/console.php b/routes/console.php index 1a5846dd2..264740fc9 100644 --- a/routes/console.php +++ b/routes/console.php @@ -4,15 +4,13 @@ use App\Console\Commands\DeleteNonEmailVerifiedUsersCommand; use App\Console\Commands\PerformDatabaseBackupCommand; -use App\Console\Commands\SendDailyEmailsCommand; -use App\Console\Commands\SendWeeklyEmailsCommand; +use App\Console\Commands\SendUnreadNotificationEmailsCommand; use App\Console\Commands\SyncVerifiedUsersCommand; use App\Jobs\CleanUnusedUploadedImages; -use Carbon\Carbon; use Illuminate\Support\Facades\Schedule; -Schedule::command(SendDailyEmailsCommand::class)->dailyAt('13:00'); -Schedule::command(SendWeeklyEmailsCommand::class)->weeklyOn(Carbon::MONDAY, '13:00'); +Schedule::command(SendUnreadNotificationEmailsCommand::class)->dailyAt('13:00'); +Schedule::command(SendUnreadNotificationEmailsCommand::class, ['--weekly' => true])->weekly()->mondays()->at('13:00'); Schedule::command(PerformDatabaseBackupCommand::class)->everySixHours(); Schedule::command(DeleteNonEmailVerifiedUsersCommand::class)->hourly(); Schedule::command(SyncVerifiedUsersCommand::class)->daily(); diff --git a/tests/Console/SendDailyEmailsCommandTest.php b/tests/Console/SendDailyEmailsCommandTest.php deleted file mode 100644 index 1920e4805..000000000 --- a/tests/Console/SendDailyEmailsCommandTest.php +++ /dev/null @@ -1,40 +0,0 @@ -create(); - - User::factory(2)->create([ - 'mail_preference_time' => 'weekly', - ]); - - User::factory(2)->create([ - 'mail_preference_time' => 'never', - ]); - - $questioner = User::factory()->create([ - 'mail_preference_time' => '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(SendDailyEmailsCommand::class) - ->assertExitCode(0); - - Mail::assertQueuedCount(5); -}); diff --git a/tests/Console/SendReminderEmailsCommandTest.php b/tests/Console/SendReminderEmailsCommandTest.php new file mode 100644 index 000000000..506f18d76 --- /dev/null +++ b/tests/Console/SendReminderEmailsCommandTest.php @@ -0,0 +1,107 @@ +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; + }); +}); diff --git a/tests/Console/SendWeeklyEmailsCommandTest.php b/tests/Console/SendWeeklyEmailsCommandTest.php deleted file mode 100644 index 689424516..000000000 --- a/tests/Console/SendWeeklyEmailsCommandTest.php +++ /dev/null @@ -1,40 +0,0 @@ -create(); - - User::factory(2)->create([ - 'mail_preference_time' => 'weekly', - ]); - - User::factory(2)->create([ - 'mail_preference_time' => 'never', - ]); - - $questioner = User::factory()->create([ - 'mail_preference_time' => '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(SendWeeklyEmailsCommand::class) - ->assertExitCode(0); - - Mail::assertQueuedCount(2); -}); diff --git a/tests/Unit/Mail/PendingNotificationsTest.php b/tests/Unit/Mail/PendingNotificationsTest.php index cf064bc18..c6b02ef5c 100644 --- a/tests/Unit/Mail/PendingNotificationsTest.php +++ b/tests/Unit/Mail/PendingNotificationsTest.php @@ -3,17 +3,12 @@ declare(strict_types=1); use App\Mail\PendingNotifications; -use App\Models\Question; use App\Models\User; test('envelope', function () { $user = User::factory()->create(); - Question::factory()->create([ - 'to_id' => $user->id, - ]); - - $mail = new PendingNotifications($user); + $mail = new PendingNotifications($user, 1); $envelope = $mail->envelope(); @@ -24,11 +19,7 @@ test('content', function () { $user = User::factory()->create(); - Question::factory()->create([ - 'to_id' => $user->id, - ]); - - $mail = new PendingNotifications($user); + $mail = new PendingNotifications($user, 1); foreach ([ '# Hello, '.$user->name.'!',