Add inactivity reminder emails
No app/Notifications existed beyond Laravel's stock email verification and password reset - nothing brought lapsed users back. Adds a queued InactivityReminder mail notification and a scheduled command (app:send-inactivity-reminders, daily at 09:00) that emails verified users with no completed workout session in the last 5 days, at most once per 7-day cooldown so it can't spam. Skips brand-new signups so it doesn't nag someone who registered an hour ago. Verified the full pipeline live: scheduled command queues the notification (QUEUE_CONNECTION=database), a queue worker processes it, and the rendered email lands in the log driver with a working link back to /workouts. Production still needs cron actually invoking `php artisan schedule:run` every minute and a running queue worker - that's part of the pending deployment setup, not something this change can provide on its own. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Workout;
|
||||
use App\Models\WorkoutSession;
|
||||
use App\Notifications\InactivityReminder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SendInactivityRemindersTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_reminds_a_verified_inactive_user(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'email_verified_at' => now()->subDays(10),
|
||||
'created_at' => now()->subDays(10),
|
||||
]);
|
||||
|
||||
$this->artisan('app:send-inactivity-reminders')->assertSuccessful();
|
||||
|
||||
Notification::assertSentTo($user, InactivityReminder::class);
|
||||
$this->assertNotNull($user->fresh()->last_reminder_sent_at);
|
||||
}
|
||||
|
||||
public function test_it_does_not_remind_a_user_with_a_recent_completed_session(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'email_verified_at' => now()->subDays(10),
|
||||
'created_at' => now()->subDays(10),
|
||||
]);
|
||||
$workout = Workout::factory()->create(['user_id' => $user->id]);
|
||||
WorkoutSession::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'workout_id' => $workout->id,
|
||||
'completed_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$this->artisan('app:send-inactivity-reminders');
|
||||
|
||||
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||||
}
|
||||
|
||||
public function test_it_does_not_remind_a_user_still_within_the_cooldown(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'email_verified_at' => now()->subDays(10),
|
||||
'created_at' => now()->subDays(10),
|
||||
'last_reminder_sent_at' => now()->subDays(2),
|
||||
]);
|
||||
|
||||
$this->artisan('app:send-inactivity-reminders');
|
||||
|
||||
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||||
}
|
||||
|
||||
public function test_it_reminds_again_once_the_cooldown_has_passed(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'email_verified_at' => now()->subDays(10),
|
||||
'created_at' => now()->subDays(10),
|
||||
'last_reminder_sent_at' => now()->subDays(8),
|
||||
]);
|
||||
|
||||
$this->artisan('app:send-inactivity-reminders');
|
||||
|
||||
Notification::assertSentTo($user, InactivityReminder::class);
|
||||
}
|
||||
|
||||
public function test_it_does_not_remind_an_unverified_user(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->unverified()->create([
|
||||
'created_at' => now()->subDays(10),
|
||||
]);
|
||||
|
||||
$this->artisan('app:send-inactivity-reminders');
|
||||
|
||||
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||||
}
|
||||
|
||||
public function test_it_does_not_remind_a_brand_new_user(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'email_verified_at' => now(),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$this->artisan('app:send-inactivity-reminders');
|
||||
|
||||
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user