refactor: ♻️ Move referral_code generation to trait
This commit is contained in:
parent
fd8dbb10a5
commit
5ae631ee4f
5 changed files with 44 additions and 58 deletions
|
@ -4,6 +4,7 @@ namespace App\Console\Commands;
|
|||
|
||||
use App\Classes\Pterodactyl;
|
||||
use App\Models\User;
|
||||
use App\Traits\Referral;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
@ -11,6 +12,8 @@ use Illuminate\Support\Str;
|
|||
|
||||
class MakeUserCommand extends Command
|
||||
{
|
||||
use Referral;
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
|
@ -38,18 +41,6 @@ class MakeUserCommand extends Command
|
|||
$this->pterodactyl = $pterodactyl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userid
|
||||
* @return string
|
||||
*/
|
||||
public function generateCode(){
|
||||
$random = STR::random(8);
|
||||
if (User::where('referral_code', '=', $random)->doesntExist()) {
|
||||
return $random;
|
||||
} else {
|
||||
$this->generateCode();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
|
@ -97,7 +88,7 @@ class MakeUserCommand extends Command
|
|||
'email' => $response['email'],
|
||||
'role' => 'admin',
|
||||
'password' => Hash::make($password),
|
||||
'referral_code' => $this->generateCode(),
|
||||
'referral_code' => $this->createReferralCode(),
|
||||
'pterodactyl_id' => $response['id'],
|
||||
]);
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ use App\Http\Controllers\Controller;
|
|||
use App\Models\DiscordUser;
|
||||
use App\Models\User;
|
||||
use App\Notifications\ReferralNotification;
|
||||
use App\Traits\Referral;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
@ -27,6 +28,8 @@ use Spatie\QueryBuilder\QueryBuilder;
|
|||
|
||||
class UserController extends Controller
|
||||
{
|
||||
use Referral;
|
||||
|
||||
const ALLOWED_INCLUDES = ['servers', 'notifications', 'payments', 'vouchers', 'discordUser'];
|
||||
|
||||
const ALLOWED_FILTERS = ['name', 'server_limit', 'email', 'pterodactyl_id', 'role', 'suspended'];
|
||||
|
@ -92,7 +95,7 @@ class UserController extends Controller
|
|||
|
||||
//Update Users Password on Pterodactyl
|
||||
//Username,Mail,First and Lastname are required aswell
|
||||
$response = Pterodactyl::client()->patch('/application/users/'.$user->pterodactyl_id, [
|
||||
$response = Pterodactyl::client()->patch('/application/users/' . $user->pterodactyl_id, [
|
||||
'username' => $request->name,
|
||||
'first_name' => $request->name,
|
||||
'last_name' => $request->name,
|
||||
|
@ -229,7 +232,7 @@ class UserController extends Controller
|
|||
$discordUser = DiscordUser::find($id);
|
||||
$user = $discordUser ? $discordUser->user : User::findOrFail($id);
|
||||
|
||||
if (! $user->isSuspended()) {
|
||||
if (!$user->isSuspended()) {
|
||||
throw ValidationException::withMessages([
|
||||
'error' => 'You cannot unsuspend an User who is not suspended.',
|
||||
]);
|
||||
|
@ -240,21 +243,6 @@ class UserController extends Controller
|
|||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a unique Referral Code for User
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function createReferralCode()
|
||||
{
|
||||
$referralcode = STR::random(8);
|
||||
if (User::where('referral_code', '=', $referralcode)->exists()) {
|
||||
$this->createReferralCode();
|
||||
}
|
||||
|
||||
return $referralcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
|
@ -267,7 +255,7 @@ class UserController extends Controller
|
|||
]);
|
||||
|
||||
// Prevent the creation of new users via API if this is enabled.
|
||||
if (! config('SETTINGS::SYSTEM:CREATION_OF_NEW_USERS', 'true')) {
|
||||
if (!config('SETTINGS::SYSTEM:CREATION_OF_NEW_USERS', 'true')) {
|
||||
throw ValidationException::withMessages([
|
||||
'error' => 'The creation of new users has been blocked by the system administrator.',
|
||||
]);
|
||||
|
@ -305,7 +293,7 @@ class UserController extends Controller
|
|||
'pterodactyl_id' => $response->json()['attributes']['id'],
|
||||
]);
|
||||
//INCREMENT REFERRAL-USER CREDITS
|
||||
if (! empty($request->input('referral_code'))) {
|
||||
if (!empty($request->input('referral_code'))) {
|
||||
$ref_code = $request->input('referral_code');
|
||||
$new_user = $user->id;
|
||||
if ($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) {
|
||||
|
|
|
@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
|
|||
use App\Models\User;
|
||||
use App\Notifications\ReferralNotification;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use App\Traits\Referral;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Auth\RegistersUsers;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
@ -30,7 +31,7 @@ class RegisterController extends Controller
|
|||
|
|
||||
*/
|
||||
|
||||
use RegistersUsers;
|
||||
use RegistersUsers, Referral;
|
||||
|
||||
/**
|
||||
* Where to redirect users after registration.
|
||||
|
@ -87,21 +88,6 @@ class RegisterController extends Controller
|
|||
return Validator::make($data, $validationRules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a unique Referral Code for User
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function createReferralCode()
|
||||
{
|
||||
$referralcode = STR::random(8);
|
||||
if (User::where('referral_code', '=', $referralcode)->exists()) {
|
||||
$this->createReferralCode();
|
||||
}
|
||||
|
||||
return $referralcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user instance after a valid registration.
|
||||
*
|
||||
|
|
23
app/Traits/Referral.php
Normal file
23
app/Traits/Referral.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
||||
trait Referral
|
||||
{
|
||||
public function createReferralCode()
|
||||
{
|
||||
$code = Str::random(8);
|
||||
|
||||
// check if code already exists
|
||||
if (User::where('referral_code', $code)->exists()) {
|
||||
// if exists, generate another code
|
||||
return $this->generateReferralCode();
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Traits\Referral;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
@ -9,16 +10,13 @@ use Illuminate\Support\Str;
|
|||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function generateCode($userid)
|
||||
use Referral;
|
||||
public function setReferralCode($userid)
|
||||
{
|
||||
$random = STR::random(8);
|
||||
if (User::where('referral_code', '=', $random)->doesntExist()) {
|
||||
$code = $this->createReferralCode();
|
||||
DB::table('users')
|
||||
->where('id', '=', $userid)
|
||||
->update(['referral_code' => $random]);
|
||||
} else {
|
||||
$this->generateCode($userid);
|
||||
}
|
||||
->update(['referral_code' => $code]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,7 +33,7 @@ return new class extends Migration
|
|||
$existing_user = User::where('referral_code', '')->orWhere('referral_code', null)->get();
|
||||
|
||||
foreach ($existing_user as $user) {
|
||||
$this->generateCode($user->id);
|
||||
$this->setReferralCode($user->id);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue