Files
FlightsAPI/app/Console/Commands/RecalculateAchievements.php
T

38 lines
1.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Attributes\Argument;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('app:recalculate-achievements {username? : The username of a single user to recalculate}')]
#[Description('Recalculate achievements for all users, or a single user by username')]
class RecalculateAchievements extends Command
{
public function handle(): void
{
$username = $this->argument('username');
if ($username) {
$user = User::where('name', $username)->first();
if (! $user) {
$this->error("No user found with username [{$username}].");
return;
}
$user->calculateAchievements();
$this->info("Recalculated achievements for {$username}.");
return;
}
$users = User::all();
$this->withProgressBar($users, fn (User $user) => $user->calculateAchievements());
$this->newLine();
$this->info("Recalculated achievements for {$users->count()} users.");
}
}