Add pilot ID change fields to the admin screen
This commit is contained in:
@@ -15,6 +15,7 @@ roles:
|
||||
|
||||
users:
|
||||
- id: 1
|
||||
pilot_id: 1
|
||||
name: Admin User
|
||||
email: admin@phpvms.net
|
||||
password: admin
|
||||
@@ -34,6 +35,7 @@ users:
|
||||
created_at: now
|
||||
updated_at: now
|
||||
- id: 2
|
||||
pilot_id: 2
|
||||
name: Carla Walters
|
||||
email: carla.walters68@example.com
|
||||
password: admin
|
||||
@@ -50,6 +52,7 @@ users:
|
||||
opt_in: 1
|
||||
toc_accepted: 1
|
||||
- id: 3
|
||||
pilot_id: 3
|
||||
name: Raymond Pearson
|
||||
email: raymond.pearson56@example.com
|
||||
password: admin
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace App\Http\Requests;
|
||||
use App\Models\Pirep;
|
||||
use App\Repositories\PirepFieldRepository;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Log;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CreatePirepRequest extends FormRequest
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use function request;
|
||||
|
||||
class UpdateUserRequest extends FormRequest
|
||||
{
|
||||
@@ -24,6 +25,13 @@ class UpdateUserRequest extends FormRequest
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return User::$rules;
|
||||
$rules = User::$rules;
|
||||
|
||||
$user_id = request('id', null);
|
||||
|
||||
// Validate if the pilot ID is already being used or not
|
||||
$rules['pilot_id'] = 'required|integer|unique:users,pilot_id,'.$user_id.',id';
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,8 +96,9 @@ class User extends Authenticatable
|
||||
];
|
||||
|
||||
public static $rules = [
|
||||
'name' => 'required',
|
||||
'email' => 'required|email',
|
||||
'pilot_id' => 'required|integer|unique:users,pilot_id',
|
||||
'name' => 'required',
|
||||
'email' => 'required|email',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -108,6 +108,11 @@ class UserService extends Service
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function isPilotIdAlreadyUsed(int $pilot_id): bool
|
||||
{
|
||||
return User::where('pilot_id', '=', $pilot_id)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a user's pilot ID
|
||||
*
|
||||
@@ -120,7 +125,11 @@ class UserService extends Service
|
||||
*/
|
||||
public function changePilotId(User $user, int $pilot_id): User
|
||||
{
|
||||
if (User::where('pilot_id', '=', $pilot_id)->exists()) {
|
||||
if ($user->pilot_id === $pilot_id) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
if ($this->isPilotIdAlreadyUsed($pilot_id)) {
|
||||
Log::error('User with id '.$pilot_id.' already exists');
|
||||
|
||||
throw new UserPilotIdExists();
|
||||
|
||||
Reference in New Issue
Block a user