135 lines
3.8 KiB
PHP
135 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\UserUtil;
|
|
use App\User;
|
|
use Illuminate\Console\Command;
|
|
|
|
class UserRestore extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:restore {user_id}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'User Restore: Reactivates an inactive user and restores their downline structure';
|
|
|
|
private $timeStart;
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('RUN Command user:restore');
|
|
\Log::channel('cleanup')->info('COMMAND [user:restore] started.');
|
|
|
|
$this->timeStart = microtime(true);
|
|
|
|
$result = $this->restoreInactiveUsers();
|
|
|
|
\Log::channel('cleanup')->info('COMMAND [user:restore] finished.');
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Stellt einen deaktivierten User wieder her
|
|
* - Reaktiviert den User (setzt active=true, stellt m_sponsor wieder her)
|
|
* - Stellt die Vertriebspartner-Kinder (Downline) wieder her
|
|
* - Nutzt UserCleanUpLog um die ursprüngliche Struktur wiederherzustellen
|
|
*
|
|
* @return int
|
|
*/
|
|
private function restoreInactiveUsers()
|
|
{
|
|
$methodStartTime = microtime(true);
|
|
$this->info('START Command restoreInactiveUsers');
|
|
|
|
$user_id = $this->argument('user_id');
|
|
|
|
if (! $user_id) {
|
|
$this->error('ERROR: No user_id provided as argument');
|
|
\Log::channel('cleanup')->error('restoreInactiveUsers: No user_id provided');
|
|
|
|
return 1;
|
|
}
|
|
|
|
$this->info('Restoring user with ID: '.$user_id);
|
|
|
|
$user = User::find($user_id);
|
|
|
|
if (! $user) {
|
|
$this->error('ERROR: User not found with ID: '.$user_id);
|
|
\Log::channel('cleanup')->error('restoreInactiveUsers: User not found, user_id: '.$user_id);
|
|
|
|
return 1;
|
|
}
|
|
|
|
// Prüfe ob User bereits aktiv ist
|
|
if ($user->active) {
|
|
$this->warn('WARNING: User is already active, user_id: '.$user_id);
|
|
\Log::channel('cleanup')->warning('restoreInactiveUsers: User is already active, user_id: '.$user_id);
|
|
|
|
return 0;
|
|
}
|
|
|
|
\DB::beginTransaction();
|
|
|
|
try {
|
|
$data = [
|
|
'user_id' => $user->id,
|
|
'email' => $user->email,
|
|
'm_account' => $user->account ? $user->account->m_account : '',
|
|
'm_first_name' => $user->account ? $user->account->m_first_name : '',
|
|
'm_last_name' => $user->account ? $user->account->m_last_name : '',
|
|
];
|
|
|
|
// Reaktiviere User (setzt active=true, stellt m_sponsor aus pre_sponsor wieder her)
|
|
UserUtil::reactiveUser($user);
|
|
|
|
// Stelle alle Vertriebspartner-Kinder wieder her
|
|
UserUtil::resetChildsToSponsor($user->id);
|
|
|
|
\DB::commit();
|
|
|
|
$diff = microtime(true) - $methodStartTime;
|
|
$sec = intval($diff);
|
|
$micro = $diff - $sec;
|
|
|
|
$this->info('SUCCESS: User restored successfully');
|
|
$this->info('END Command restoreInactiveUsers | Time: '.$sec.'sec :'.round($micro * 1000, 4).' ms');
|
|
|
|
\Log::channel('cleanup')->info('restoreInactiveUsers SUCCESS: '.json_encode($data));
|
|
|
|
return 0;
|
|
} catch (\Exception $e) {
|
|
\DB::rollBack();
|
|
|
|
$this->error('ERROR: Failed to restore user: '.$e->getMessage());
|
|
\Log::channel('cleanup')->error('restoreInactiveUsers FAILED for user_id: '.$user_id.' | Error: '.$e->getMessage());
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
}
|