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,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;
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user