Skip to content

Commit

Permalink
Merge pull request #514 from pinkary-project/refactor/email-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Sep 13, 2024
2 parents 3e973f8 + 226a884 commit b55ead6
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 178 deletions.
38 changes: 0 additions & 38 deletions app/Console/Commands/SendDailyEmailsCommand.php

This file was deleted.

46 changes: 46 additions & 0 deletions app/Console/Commands/SendUnreadNotificationEmailsCommand.php
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)));
}
}
38 changes: 0 additions & 38 deletions app/Console/Commands/SendWeeklyEmailsCommand.php

This file was deleted.

9 changes: 4 additions & 5 deletions app/Mail/PendingNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
//
}
Expand All @@ -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'),
);
}

Expand All @@ -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,
],
);
}
Expand Down
Binary file added public/img/logo-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion resources/views/vendor/mail/html/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<tr>
<td class="header">
<a href="{{ $url }}" style="display: inline-block;">
<img src="{{ url('img/logo-text.svg') }}" class="logo" alt="Pinkary Logo">
<img src="{{ asset('img/logo-text.png') }}" class="logo" alt="Pinkary Logo">
</a>
</td>
</tr>
8 changes: 3 additions & 5 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
40 changes: 0 additions & 40 deletions tests/Console/SendDailyEmailsCommandTest.php

This file was deleted.

107 changes: 107 additions & 0 deletions tests/Console/SendReminderEmailsCommandTest.php
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;
});
});
40 changes: 0 additions & 40 deletions tests/Console/SendWeeklyEmailsCommandTest.php

This file was deleted.

Loading

0 comments on commit b55ead6

Please sign in to comment.