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>
55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?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;
|
|
}
|
|
}
|