agents table updated and roles logic created
This commit is contained in:
parent
2335820883
commit
2154ca1bec
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http\Controllers\Company;
|
namespace App\Http\Controllers\Company;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
@ -19,13 +20,27 @@ public function __invoke(Request $request)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$admin = $admin->first();
|
$admin = $admin->first();
|
||||||
if (!$this->isUnique($request->email, $request->phone))
|
|
||||||
|
$user = User::where('email', $request->email)->orWhere('phone', $request->phone)->first();
|
||||||
|
if ($user)
|
||||||
|
{
|
||||||
|
if ($user->id !== auth()->id()) //если это не текущий пользователь-админ, который хочет себя сделать агентом, то ошибка
|
||||||
|
{
|
||||||
|
if (Agent::where('user_id', $user->id)->count()) // и если этот пользователь уже агент
|
||||||
{
|
{
|
||||||
return back()->with('error', __('Agent is not unique'));
|
return back()->with('error', __('Agent is not unique'));
|
||||||
}
|
}
|
||||||
if ($user = User::create($request->all()))
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
|
$user = $user = User::create($request->all());
|
||||||
$user->setForcedPassword();
|
$user->setForcedPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user)
|
||||||
|
{
|
||||||
|
Agent::where('user_id', $user->id)->delete(); //на случай, если где-то этот пользователь уже был агентом
|
||||||
$agent = Agent::create([
|
$agent = Agent::create([
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
'company_id' => $admin->company_id
|
'company_id' => $admin->company_id
|
||||||
@ -33,13 +48,4 @@ public function __invoke(Request $request)
|
|||||||
}
|
}
|
||||||
return to_route('company.agents.table');
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
28
app/Http/Controllers/Company/DeleteAgentController.php
Normal file
28
app/Http/Controllers/Company/DeleteAgentController.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?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 DeleteAgentController extends Controller
|
||||||
|
{
|
||||||
|
public function __invoke(Agent $agent)
|
||||||
|
{
|
||||||
|
$admin = CompanyAdmin::where('user_id', auth()->id())
|
||||||
|
->where(
|
||||||
|
'company_id',
|
||||||
|
$agent->company_id
|
||||||
|
);
|
||||||
|
if (!$admin->count())
|
||||||
|
{
|
||||||
|
abort(404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$agent->delete();
|
||||||
|
return to_route('company.agents.table');
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -27,7 +27,7 @@ public function render()
|
|||||||
$agents = Agent::where('company_id', $admin->company_id);
|
$agents = Agent::where('company_id', $admin->company_id);
|
||||||
if ($this->status == AgentStatus::DISMISSED)
|
if ($this->status == AgentStatus::DISMISSED)
|
||||||
{
|
{
|
||||||
$agents->whereNotNull('deleted_at');
|
$agents->onlyTrashed();
|
||||||
}
|
}
|
||||||
return view(
|
return view(
|
||||||
'livewire.agents-table',
|
'livewire.agents-table',
|
||||||
|
|||||||
@ -31,11 +31,8 @@ public function getDeals()
|
|||||||
{
|
{
|
||||||
$deals = false;
|
$deals = false;
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
if ($agent = Agent::where('user_id', $user->id)->first())
|
|
||||||
{
|
if ($admin = CompanyAdmin::where('user_id', $user->id)->first())
|
||||||
$deals = Deal::where('agent_id', $agent->id);
|
|
||||||
}
|
|
||||||
elseif ($admin = CompanyAdmin::where('user_id', $user->id)->first())
|
|
||||||
{
|
{
|
||||||
$deals = Deal::whereIn('agent_id', function ($query) use ($admin)
|
$deals = Deal::whereIn('agent_id', function ($query) use ($admin)
|
||||||
{
|
{
|
||||||
@ -44,6 +41,10 @@ public function getDeals()
|
|||||||
$query->where('company_id', $admin->company_id);
|
$query->where('company_id', $admin->company_id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
elseif ($agent = Agent::where('user_id', $user->id)->first())
|
||||||
|
{
|
||||||
|
$deals = Deal::where('agent_id', $agent->id);
|
||||||
|
}
|
||||||
return $deals;
|
return $deals;
|
||||||
}
|
}
|
||||||
public function render()
|
public function render()
|
||||||
|
|||||||
@ -28,11 +28,13 @@ class CreateClientForm extends Component
|
|||||||
'client.secondName.required' => 'Необходимо указать фамилию клиента',
|
'client.secondName.required' => 'Необходимо указать фамилию клиента',
|
||||||
'client.phone.required' => 'Необходимо указать телефон без кода страны "+7" или "8"',
|
'client.phone.required' => 'Необходимо указать телефон без кода страны "+7" или "8"',
|
||||||
'client.phone.regex' => 'Телефон должен содержать 10 симвлов без указания кода страны "+7" или "8"',
|
'client.phone.regex' => 'Телефон должен содержать 10 симвлов без указания кода страны "+7" или "8"',
|
||||||
'client.phone.unique' => 'Клиент с таким телефоном уже существует'
|
'client.phone.unique' => 'Клиент с таким телефоном уже существует',
|
||||||
|
'agent.integer' => 'Необходимо указать агента, от которого добавляется контакт'
|
||||||
];
|
];
|
||||||
protected function rules()
|
protected function rules()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'agent' => ['required', 'integer'],
|
||||||
'client.firstName' => ['required', 'string', 'max:255'],
|
'client.firstName' => ['required', 'string', 'max:255'],
|
||||||
'client.secondName' => ['required', 'string', 'max:255'],
|
'client.secondName' => ['required', 'string', 'max:255'],
|
||||||
'client.phone' => ['required', 'string', 'regex:/\(?([0-9]{3})\)([-]{1})([0-9]{3})([-]{1})([0-9]{4})/i']
|
'client.phone' => ['required', 'string', 'regex:/\(?([0-9]{3})\)([-]{1})([0-9]{3})([-]{1})([0-9]{4})/i']
|
||||||
@ -49,8 +51,15 @@ public function mount()
|
|||||||
'complexId' => ''
|
'complexId' => ''
|
||||||
];
|
];
|
||||||
$this->status = self::NEW;
|
$this->status = self::NEW;
|
||||||
|
if ($agent = Agent::where('user_id', auth()->user()->id)->first())
|
||||||
|
{
|
||||||
|
$this->agent = $agent->id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$this->agent = false;
|
$this->agent = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public function update()
|
public function update()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -127,7 +136,6 @@ public function back()
|
|||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
$validated = $this->validate($this->rules());
|
$validated = $this->validate($this->rules());
|
||||||
$agent = $this->agent || Agent::where('user_id', auth()->user()->id)->first()->id;
|
|
||||||
$phone = '+7' . $this->client['phone'];
|
$phone = '+7' . $this->client['phone'];
|
||||||
$newUser = User::updateOrCreate(
|
$newUser = User::updateOrCreate(
|
||||||
['phone' => $phone],
|
['phone' => $phone],
|
||||||
@ -137,7 +145,7 @@ public function save()
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
$data = [
|
$data = [
|
||||||
'agent_id' => $agent
|
'agent_id' => $this->agent
|
||||||
,
|
,
|
||||||
'client_id' => $newUser->id
|
'client_id' => $newUser->id
|
||||||
,
|
,
|
||||||
|
|||||||
@ -13,6 +13,8 @@
|
|||||||
use App\Models\User\Role;
|
use App\Models\User\Role;
|
||||||
use App\Models\Deal\Deal;
|
use App\Models\Deal\Deal;
|
||||||
|
|
||||||
|
use App\Notifications\AgentCreated;
|
||||||
|
|
||||||
class Agent extends Model
|
class Agent extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
@ -45,6 +47,7 @@ protected static function booted(): void
|
|||||||
'user_id' => $agent->user_id,
|
'user_id' => $agent->user_id,
|
||||||
'role_id' => Role::AGENT
|
'role_id' => Role::AGENT
|
||||||
]);
|
]);
|
||||||
|
$agent->notify();
|
||||||
});
|
});
|
||||||
static::updated(function (Agent $agent)
|
static::updated(function (Agent $agent)
|
||||||
{
|
{
|
||||||
@ -64,4 +67,9 @@ protected static function booted(): void
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
private function notify()
|
||||||
|
{
|
||||||
|
$this->user->notify(new AgentCreated($this));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
59
app/Notifications/AgentCreated.php
Normal file
59
app/Notifications/AgentCreated.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Notifications;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Notifications\Messages\MailMessage;
|
||||||
|
use Illuminate\Notifications\Notification;
|
||||||
|
use Illuminate\Support\HtmlString;
|
||||||
|
|
||||||
|
use App\Models\Agent\Agent;
|
||||||
|
|
||||||
|
class AgentCreated extends Notification
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
private $agent;
|
||||||
|
public $login;
|
||||||
|
/**
|
||||||
|
* Create a new notification instance.
|
||||||
|
*/
|
||||||
|
public function __construct(Agent $agent)
|
||||||
|
{
|
||||||
|
$this->agent = $agent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the notification's delivery channels.
|
||||||
|
*
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
public function via(object $notifiable): array
|
||||||
|
{
|
||||||
|
return ['mail'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mail representation of the notification.
|
||||||
|
*/
|
||||||
|
public function toMail(object $notifiable): MailMessage
|
||||||
|
{
|
||||||
|
return (new MailMessage)
|
||||||
|
->subject(__('Agent was created'))
|
||||||
|
->line(__('Your account was attached as agent'))
|
||||||
|
->line($this->agent->company->name)
|
||||||
|
->action(__('Go to login'), url(path: '/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the array representation of the notification.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(object $notifiable): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -30,5 +30,7 @@
|
|||||||
"Email": "Электронная почта",
|
"Email": "Электронная почта",
|
||||||
"SelfEmployer name": "ФИО полностью",
|
"SelfEmployer name": "ФИО полностью",
|
||||||
"SoleProperty name": "ФИО полностью",
|
"SoleProperty name": "ФИО полностью",
|
||||||
"contract status new": "Новый"
|
"contract status new": "Новый",
|
||||||
|
"Agent was created": "Создана новая учетная запись агента",
|
||||||
|
"Your account was attached as agent": "Ваша учетная запись была привязана в качестве агента в"
|
||||||
}
|
}
|
||||||
@ -88,6 +88,9 @@
|
|||||||
@include('left-panel')
|
@include('left-panel')
|
||||||
</div>
|
</div>
|
||||||
<div class="col-10 px-0 px-md-4">
|
<div class="col-10 px-0 px-md-4">
|
||||||
|
@foreach ($errors->all() as $error)
|
||||||
|
{{ $error }}
|
||||||
|
@endforeach
|
||||||
@yield('content')
|
@yield('content')
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
<title>Laravel</title>
|
<title>{{ config('app.name', 'Laravel') }}</title>
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="preconnect" href="https://fonts.bunny.net">
|
<link rel="preconnect" href="https://fonts.bunny.net">
|
||||||
|
|||||||
@ -16,6 +16,23 @@
|
|||||||
<td class="align-middle">
|
<td class="align-middle">
|
||||||
{{ $agent->user->email }}
|
{{ $agent->user->email }}
|
||||||
</td>
|
</td>
|
||||||
|
@if (!$agent->trashed())
|
||||||
|
<td class="align-middle text-center bg-white">
|
||||||
|
<a class="btn" href="{{ route('company.agents.delete', ['agent' => $agent->id]) }}">
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25"
|
||||||
|
fill="currentColor" class="bi bi-person-dash" viewBox="0 0 16 16">
|
||||||
|
<path
|
||||||
|
d="M12.5 16a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7M11 12h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1 0-1m0-7a3 3 0 1 1-6 0 3 3 0 0 1 6 0M8 7a2 2 0 1 0 0-4 2 2 0 0 0 0 4" />
|
||||||
|
<path
|
||||||
|
d="M8.256 14a4.5 4.5 0 0 1-.229-1.004H3c.001-.246.154-.986.832-1.664C4.484 10.68 5.711 10 8 10q.39 0 .74.025c.226-.341.496-.65.804-.918Q8.844 9.002 8 9c-5 0-6 3-6 4s1 1 1 1z" />
|
||||||
|
</svg>
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
@else
|
||||||
|
<td class="align-middle">
|
||||||
|
{{ $agent->deleted_at->diffForHumans() }}
|
||||||
|
</td>
|
||||||
|
@endif
|
||||||
</tr>
|
</tr>
|
||||||
@endforeach
|
@endforeach
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
<div>
|
<div>
|
||||||
<ul class="nav flex flex-md-column ">
|
<ul class="nav flex flex-md-column ">
|
||||||
@if (in_array($roles::AGENT, $userRoles))
|
@if (in_array($roles::AGENT, $userRoles) || in_array($roles::COMPANY_ADMIN, $userRoles))
|
||||||
<li class="nav-item text-center m-2">
|
<li class="nav-item text-center m-2">
|
||||||
<a href="{{ route('home') }}"
|
<a href="{{ route('home') }}"
|
||||||
class="nav-link d-flex align-items-center gap-2 fs-5 border rounded-4 active" aria-current="page">
|
class="nav-link d-flex align-items-center gap-2 fs-5 border rounded-4 active" aria-current="page">
|
||||||
|
|||||||
@ -43,8 +43,8 @@
|
|||||||
Route::get('/company/details/', [App\Http\Controllers\Company\DetailsController::class, 'index'])->name('company.details');
|
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::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('company.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');
|
Route::post('/company/agents/store/', App\Http\Controllers\Company\CreateAgentController::class)->name('company.agents.store');
|
||||||
|
Route::get('/company/agents/{agent}/delete', App\Http\Controllers\Company\DeleteAgentController::class)->name('company.agents.delete');
|
||||||
});
|
});
|
||||||
|
|
||||||
//МАКЕТЫ
|
//МАКЕТЫ
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user