add agents creation form

This commit is contained in:
Thekindbull 2024-12-16 00:51:01 +08:00
parent bbfba9e593
commit 06d0b88a73
12 changed files with 153 additions and 41 deletions

View File

@ -9,14 +9,14 @@
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
{
/**
* Validate and update the given user's profile information.
*
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
@ -29,16 +29,21 @@ public function update(User $user, array $input): void
],
])->validateWithBag('updateProfileInformation');
if ($input['email'] !== $user->email &&
$user instanceof MustVerifyEmail) {
if (
$input['email'] !== $user->email &&
$user instanceof MustVerifyEmail
)
{
$this->updateVerifiedUser($user, $input);
} else {
}
else
{
$user->forceFill([
'name' => $input['name'],
'email' => $input['email'],
])->save();
}
}
}
/**
* Update the given verified user's profile information.
@ -46,7 +51,7 @@ public function update(User $user, array $input): void
* @param array<string, string> $input
*/
protected function updateVerifiedUser(User $user, array $input): void
{
{
$user->forceFill([
'name' => $input['name'],
'email' => $input['email'],
@ -54,5 +59,5 @@ protected function updateVerifiedUser(User $user, array $input): void
])->save();
$user->sendEmailVerificationNotification();
}
}
}

View File

@ -4,15 +4,13 @@
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Agent\Agent;
use App\Models\Company\Company;
use App\Models\Company\CompanyAdmin;
use App\Models\Agent\Agent;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use App\Notifications\UserRegistered;
class RegisterController extends Controller
{
@ -70,14 +68,12 @@ protected function validator(array $data)
*/
protected function create(array $data)
{
$newUserPassword = Str::password(8);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'password' => Hash::make($newUserPassword),
'phone' => $data['phone']
]);
$user->notify(new UserRegistered(login: $user->email, password: $newUserPassword));
$user->setForcedPassword();
$company = Company::create([
'name' => $data['name'],
@ -88,6 +84,11 @@ protected function create(array $data)
'status' => 'new'
]);
CompanyAdmin::create([
'user_id' => $user->id,
'company_id' => $company->id
]);
Agent::create([
'user_id' => $user->id,
'company_id' => $company->id

View File

@ -36,7 +36,7 @@ public function index(Request $request)
}
else
{
echo 'has no permissions';
abort(code: 401);
return;
}
}

View File

@ -9,8 +9,6 @@
use App\Models\Company\Company;
use App\Models\Company\CompanyAdmin;
use App\Models\User;
use App\Notifications\UserRegistered;
use Illuminate\Support\Str;
class ConfirmCompanyController extends Controller
{
@ -29,14 +27,12 @@ public function __invoke(Request $request)
}
else
{
$newUserPassword = Str::password(8);
$user = User::create([
'name' => $request->user_name,
'email' => $company->email,
'phone' => $request->user_phone,
'password' => Hash::make($newUserPassword),
'phone' => $request->user_phone
]);
$user->notify(new UserRegistered($user->email, password: $newUserPassword));
$user->setForcedPassword();
}
CompanyAdmin::create([
'user_id' => $user->id,

View File

@ -0,0 +1,45 @@
<?php
namespace App\Http\Controllers\Company;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Company\CompanyAdmin;
use App\Models\Agent\Agent;
use App\Models\User;
class CreateAgentController extends Controller
{
public function __invoke(Request $request)
{
$request->only(['name', 'email', 'phone']);
$admin = CompanyAdmin::where('user_id', auth()->id());
if (!$admin->count())
{
abort(404);
return;
}
$admin = $admin->first();
if (!$this->isUnique($request->email, $request->phone))
{
return back()->with('error', __('Agent is not unique'));
}
if ($user = User::create($request->all()))
{
$user->setForcedPassword();
$agent = Agent::create([
'user_id' => $user->id,
'company_id' => $admin->company_id
]);
}
return to_route('company.agents.table');
}
public function isUnique($email, $phone)
{
if (User::where('email', $email)->count() || User::where('phone', $phone)->count())
{
return false;
}
return true;
}
}

View File

@ -9,24 +9,27 @@
use App\Models\Bitrix\SendCompany;
class CreateCompanyController extends Controller
{
{
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
{
$request->request->add([
'secret' => bin2hex(random_bytes(16)),
'status' => 'new',
'type' => 'AGENCY'
]);
$company = Company::create($request->only('name', 'email', 'inn', 'legal_address', 'secret', 'status', 'type'));
if ($company) {
if ($company)
{
$companyConfirmByBitrix = new SendCompany($company);
$companyConfirmByBitrix->send();
return view('company.created');
} else {
}
else
{
return back()->withInputs();
}
}
}
}

View File

@ -7,7 +7,7 @@
use App\Models\Company\Company;
use App\Models\Company\Details;
use App\Models\Agent\Agent;
use App\Models\Company\CompanyAdmin;
class DetailsController extends Controller
{
@ -15,15 +15,15 @@ public function index()
{
$company = false;
$userId = auth()->user()->id;
$agent = Agent::where('user_id', $userId)->get();
if ($agent->count() == 1)
$admin = CompanyAdmin::where('user_id', $userId)->get();
if ($admin->count() == 1)
{
$agent = $agent->first();
$agent = $admin->first();
$company = Company::find($agent->company_id);
}
else
{
return back();
abort(code: 401);
}
;

View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use App\Notifications\UserRegistered;
trait ForcedPassword
{
public function setForcedPassword()
{
$newPassword = Str::password(8);
$this->password = Hash::make($newPassword);
$this->save();
$this->notify(instance: new UserRegistered($this->email, password: $newPassword));
return true;
}
}

View File

@ -7,9 +7,11 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Models\ForcedPassword;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable, ForcedPassword;
/**
* The attributes that are mass assignable.

View File

@ -2,8 +2,8 @@
@section('content')
<div>
<form class="d-flex mb-3" method="GET" action="{{ route('agents.table') }}">
<div class="p-2 border rounded-3 border-1 p-1 bg-white">
<form class="d-flex mb-3" method="GET" action="{{ route('company.agents.table') }}">
<div class="p-2 border rounded-3 border-1 bg-white">
<input type="radio" class="btn-check" name="status" value="all" id="option5" autocomplete="off"
onclick="this.form.submit()" {{ $status == 'all' || !$status ? 'checked' : '' }}>
<label class="btn p-2 fs-5" for="option5">Все</label>
@ -16,6 +16,7 @@
autocomplete="off" onclick="this.form.submit()" {{ $status == $statuses::DISMISSED ? 'checked' : '' }}>
<label class="btn p-2 fs-5" for="option7">Уволенные</label>
</div>
<div class="ms-auto p-2">
<select class="form-select form-select-lg" aria-label="Large select example">
<option selected="">Город: любой</option>
@ -24,6 +25,12 @@
@endforeach
</select>
</div>
<div class="p-2">
<button type="button" class="btn btn-primary py-2 px-3 fs-5" data-bs-toggle="modal"
data-bs-target="#createAgentModal">
<i class="bi bi-person-plus"></i>
</button>
</div>
</form>
@if (!$status || $status == 'all' || $status == $statuses::ACTIVE)
<h4 class="fw-bold mt-5 mb-3">Активные</h4>
@ -39,4 +46,38 @@
</div>
@endif
</div>
<!-- Modal -->
<div class="modal fade" id="createAgentModal" tabindex="-1" aria-labelledby="createAgentModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<form class="modal-content" action="{{ route('company.agents.store') }}" method="post">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">{{ __('Create new agent') }}</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
@csrf
<div class="mb-3">
<label for="agentName" class="form-label">Полное имя (ФИО)</label>
<input type="text" class="form-control" id="agentName" name="name">
</div>
<div class="mb-3">
<label for="agentEmail" class="form-label">Электронная почта</label>
<input type="text" class="form-control" id="agentEmail" name="email">
</div>
<div class="mb-3">
<label for="agentPhone" class="form-label">Телефон</label>
<input type="text" class="form-control" id="agentPhone" name="phone">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('Close') }}</button>
<input type="submit" class="btn btn-primary" value="{{ __('Add agent') }}">
</div>
</form>
</div>
</div>
@endsection

View File

@ -12,7 +12,7 @@ class="nav-link d-flex align-items-center gap-2 fs-5 border rounded-4 active" ar
</a>
</li>
<li class="nav-item text-center m-2">
<a href="{{ route('agents.table') }}"
<a href="{{ route('company.agents.table') }}"
class="nav-link d-flex align-items-center gap-2 fs-5 border rounded-4 active" aria-current="page">
<i class="bi bi-people"></i> Агенты
</a>

View File

@ -1,11 +1,9 @@
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use App\Http\Controllers\Company\CreateCompanyController;
use App\Http\Controllers\Company\AgentController;
/*
|--------------------------------------------------------------------------
| Web Routes
@ -46,5 +44,7 @@
Route::get('/clients/table', [App\Http\Controllers\ClientsTableController::class, 'index'])->name('clients.table');
Route::get('/company/details/', [App\Http\Controllers\Company\DetailsController::class, 'index'])->name('company.details');
Route::post('/company/{company}/details/', [App\Http\Controllers\Company\DetailsController::class, 'store'])->name('company.details.store');
Route::get('/agents/table', [App\Http\Controllers\Company\AgentsTableController::class, 'index'])->name('agents.table');
Route::get('/agents/table', [App\Http\Controllers\Company\AgentsTableController::class, 'index'])->name('company.agents.table');
Route::post('/company/details/', App\Http\Controllers\Company\CreateAgentController::class)->name('company.agents.store');
});