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:
2026-08-10 12:32:17 +03:00
co-authored by Claude Sonnet 5
parent e8173ff90a
commit bbdf684a6a
6 changed files with 234 additions and 0 deletions
@@ -0,0 +1,54 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use App\Notifications\InactivityReminder;
use Illuminate\Console\Command;
class SendInactivityReminders extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:send-inactivity-reminders {--inactive-days=5} {--cooldown-days=7}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Email users who have not completed a workout session recently, at most once per cooldown period';
/**
* Execute the console command.
*/
public function handle(): int
{
$inactiveSince = now()->subDays((int) $this->option('inactive-days'));
$cooldownSince = now()->subDays((int) $this->option('cooldown-days'));
$users = User::query()
->whereNotNull('email_verified_at')
->where('created_at', '<=', $inactiveSince)
->whereDoesntHave('workoutSessions', function ($q) use ($inactiveSince) {
$q->whereNotNull('completed_at')->where('completed_at', '>=', $inactiveSince);
})
->where(function ($q) use ($cooldownSince) {
$q->whereNull('last_reminder_sent_at')->orWhere('last_reminder_sent_at', '<', $cooldownSince);
})
->get();
foreach ($users as $user) {
$user->notify(new InactivityReminder());
$user->last_reminder_sent_at = now();
$user->save();
}
$this->info("Sent inactivity reminders to {$users->count()} user(s).");
return self::SUCCESS;
}
}
+5
View File
@@ -74,6 +74,11 @@ class User extends Authenticatable
return $this->hasMany(Program::class);
}
public function workoutSessions()
{
return $this->hasMany(WorkoutSession::class);
}
// Trainer/client relationships
public function clients()
{
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class InactivityReminder extends Notification implements ShouldQueue
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject("We haven't seen you in a while, {$notifiable->name}")
->line('It has been a few days since your last completed workout.')
->line('A short session today is better than a perfect one you keep putting off.')
->action('Start a Workout', route('workouts.index'))
->line('See you back soon!');
}
}
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('last_reminder_sent_at')->nullable()->after('minutes_per_session');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('last_reminder_sent_at');
});
}
};
+3
View File
@@ -2,7 +2,10 @@
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');
Schedule::command('app:send-inactivity-reminders')->dailyAt('09:00');
@@ -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);
}
}