Files
fitness/app/Console/Commands/SendInactivityReminders.php
T

55 lines
1.6 KiB
PHP
Raw Normal View History

2026-08-10 12:32:17 +03:00
<?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;
}
}