Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions app/Console/Commands/DeleteStaleUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Services\UserService;
use App\Enums\UserStateEnum;

final class DeleteStaleUsers extends Command
{
protected $signature = 'users:delete-stale {--force : Skip deletion confirmation}';
protected $description = 'Delete stale users from database';
protected UserService $userService;


public function __construct(UserService $userService)
{
parent::__construct();
$this->userService = $userService;
}

public function handle(): int
{
$this->info('Finding stale users...');

$staleUsers = $this->userService->getStaleUsers();

if ($staleUsers->isEmpty()) {
$this->info('No stale users found.');
return Command::SUCCESS;
}
$count = $staleUsers->count();
$this->warn("Found {$count} stale user(s).");

$this->table(
['ID', 'Username', 'Email', 'Last Updated'],
$staleUsers->map(function ($user) {
return [
$user->id,
$user->username,
$user->email,
$user->updated_at->format('Y-m-d H:i:s'),
];
})->all()
);

if (!$this->option('force')) {
if (!$this->confirm("Are you sure you want to delete these {$count} user(s)? This action cannot be undone.")) {
$this->info('Operation cancelled by user.');
return Command::SUCCESS;
}
}

$this->info('Proceeding with deletion...');

$deletedCount = 0;
$errorCount = 0;

foreach ($staleUsers as $user) {
try {
if ($user->forceDelete()) {
$this->info("User ID: {$user->id} ({$user->username}) deleted successfully.");
$deletedCount++;
} else {
$this->error("Failed to delete User ID: {$user->id} ({$user->username}).");
$errorCount++;
}
} catch (Throwable $e) {
$this->error("Error deleting User ID: {$user->id} ({$user->username}): " . $e->getMessage());
$errorCount++;
}

}

$this->info("Deletion process completed. {$deletedCount} user(s) deleted.");
if ($errorCount > 0) {
$this->error("{$errorCount} user(s) could not be deleted. Check logs for details.");
return Command::FAILURE;
}

return Command::SUCCESS;
}
}
14 changes: 14 additions & 0 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use App\Dtos\UserDto;
use App\Models\User;
use App\Enums\UserStateEnum;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;


final class UserRepository extends BaseRepository implements UserRepositoryInterface
{
Expand All @@ -31,4 +35,14 @@ public function updateState(User $user, string $state): void

$user->save();
}

public function findStaleUsers(): Collection
{
return User::query()
->where('state', UserStateEnum::Pending->value)
->whereNull('email_verified_at')
->whereNull('pm_type')
->where('updated_at', '<', Carbon::now()->subHours(24))
->get();
}
}
4 changes: 4 additions & 0 deletions app/Repositories/UserRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use App\Dtos\UserDto;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;


interface UserRepositoryInterface extends BaseRepositoryInterface
{
public function createUser(UserDto $dto);

public function updateState(User $user, string $state);

public function findStaleUsers(): Collection;
}
13 changes: 13 additions & 0 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use App\Repositories\UserRepositoryInterface;
use App\Traits\LoggingTrait;
use Exception;
use Illuminate\Database\Eloquent\Collection;


final class UserService
{
Expand Down Expand Up @@ -41,4 +43,15 @@ public function update(int $userId, array $data): ?User
{
return $this->userRepository->update($userId, $data);
}

public function getStaleUsers(): Collection
{
try {
return $this->userRepository->findStaleUsers();
} catch (Exception $exception) {
$this->logError($exception);

return collect();
}
}
}
15 changes: 6 additions & 9 deletions resources/views/members/partials/info.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<section>
<h2>{{ trans('Info') }}</h2>

<table class="profile">
<tbody>
<tr>
<th scope="row">Joined:</th>
{{-- TODO: Link to archive page --}}
<td class="nowrap">June 18, 2017</td>
</tr>
</tbody>
</table>
<div class="profile">
<dl>
<dt>Joined</dt>
<dd><x-dates.formatted-date-time-component :date="$user->created_at" format="F j, Y" /></dl>
</dl>
</div>
</section>