From 2154ca1bec3faafa165f53cbec9787b26befcdc7 Mon Sep 17 00:00:00 2001 From: Thekindbull Date: Thu, 3 Apr 2025 17:21:05 +0800 Subject: [PATCH] agents table updated and roles logic created --- .../Company/CreateAgentController.php | 46 ++++++++------- .../Company/DeleteAgentController.php | 28 +++++++++ app/Livewire/AgentsTable.php | 16 ++--- app/Livewire/ClientsTable.php | 47 +++++++-------- app/Livewire/CreateClientForm.php | 16 +++-- app/Models/Agent/Agent.php | 44 ++++++++------ app/Notifications/AgentCreated.php | 59 +++++++++++++++++++ lang/ru.json | 4 +- resources/views/layouts/app.blade.php | 3 + resources/views/layouts/guest.blade.php | 2 +- .../views/livewire/agents-table.blade.php | 17 ++++++ resources/views/livewire/main-menu.blade.php | 2 +- routes/web.php | 32 +++++----- 13 files changed, 224 insertions(+), 92 deletions(-) create mode 100644 app/Http/Controllers/Company/DeleteAgentController.php create mode 100644 app/Notifications/AgentCreated.php diff --git a/app/Http/Controllers/Company/CreateAgentController.php b/app/Http/Controllers/Company/CreateAgentController.php index c686d6c..9ee0947 100644 --- a/app/Http/Controllers/Company/CreateAgentController.php +++ b/app/Http/Controllers/Company/CreateAgentController.php @@ -1,4 +1,5 @@ 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)) + + $user = User::where('email', $request->email)->orWhere('phone', $request->phone)->first(); + if ($user) + { + if ($user->id !== auth()->id()) //если это не текущий пользователь-админ, который хочет себя сделать агентом, то ошибка { - return back()->with('error', __('Agent is not unique')); + if (Agent::where('user_id', $user->id)->count()) // и если этот пользователь уже агент + { + return back()->with('error', __('Agent is not unique')); + } } - if ($user = User::create($request->all())) - { + } + else + { + $user = $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) { - if (User::where('email', $email)->count() || User::where('phone', $phone)->count()) - { - return false; - } - return true; + Agent::where('user_id', $user->id)->delete(); //на случай, если где-то этот пользователь уже был агентом + $agent = Agent::create([ + 'user_id' => $user->id, + 'company_id' => $admin->company_id + ]); } + return to_route('company.agents.table'); } +} diff --git a/app/Http/Controllers/Company/DeleteAgentController.php b/app/Http/Controllers/Company/DeleteAgentController.php new file mode 100644 index 0000000..d6aff9e --- /dev/null +++ b/app/Http/Controllers/Company/DeleteAgentController.php @@ -0,0 +1,28 @@ +id()) + ->where( + 'company_id', + $agent->company_id + ); + if (!$admin->count()) + { + abort(404); + return; + } + $agent->delete(); + return to_route('company.agents.table'); + } +} diff --git a/app/Livewire/AgentsTable.php b/app/Livewire/AgentsTable.php index 7bf416c..3c0abe0 100644 --- a/app/Livewire/AgentsTable.php +++ b/app/Livewire/AgentsTable.php @@ -11,29 +11,29 @@ use App\Models\Deal\Deal; class AgentsTable extends Component - { +{ use WithPagination; public $status; public function mount($status = null) - { + { $this->status = $status; - } + } public function render() - { + { $user = auth()->user(); $admin = CompanyAdmin::where('user_id', $user->id)->first(); $agents = Agent::where('company_id', $admin->company_id); if ($this->status == AgentStatus::DISMISSED) - { - $agents->whereNotNull('deleted_at'); - } + { + $agents->onlyTrashed(); + } return view( 'livewire.agents-table', [ 'agents' => $agents->paginate(10) ] ); - } } +} diff --git a/app/Livewire/ClientsTable.php b/app/Livewire/ClientsTable.php index 9998701..5fdcbfa 100644 --- a/app/Livewire/ClientsTable.php +++ b/app/Livewire/ClientsTable.php @@ -13,7 +13,7 @@ use App\Models\Deal\DealStatus; class ClientsTable extends Component - { +{ use WithPagination, WithoutUrlPagination; public $status; @@ -21,58 +21,59 @@ class ClientsTable extends Component public $mode;//short || full public function mount($status = null, $count = 10, $mode = 'full') - { + { $this->status = $status; $this->count = $count; $this->mode = $mode; - } + } public function getDeals() - { + { $deals = false; $user = auth()->user(); - if ($agent = Agent::where('user_id', $user->id)->first()) - { - $deals = Deal::where('agent_id', $agent->id); - } - elseif ($admin = CompanyAdmin::where('user_id', $user->id)->first()) - { + + if ($admin = CompanyAdmin::where('user_id', $user->id)->first()) + { $deals = Deal::whereIn('agent_id', function ($query) use ($admin) - { + { $query->select('id'); $query->from('agents'); $query->where('company_id', $admin->company_id); - }); - } - return $deals; + }); } - public function render() + elseif ($agent = Agent::where('user_id', $user->id)->first()) { + $deals = Deal::where('agent_id', $agent->id); + } + return $deals; + } + public function render() + { $deals = $this->getDeals(); if ($this->status && $this->status == DealStatus::UNIQUE) - { + { $deals = $deals ->whereIn('status', [DealStatus::UNIQUE]) ->orderBy('id', 'desc')->paginate($this->count, ['*'], 'unique_clients'); - } + } elseif ($this->status && $this->status == DealStatus::NOT_UNIQUE) - { + { $deals = $deals ->whereIn('status', [DealStatus::MODERATION, DealStatus::NEW , DealStatus::NOT_UNIQUE]) ->orderBy('id', 'desc')->paginate($this->count, ['*'], 'not_unique_clients'); - } + } else - { + { $deals = $deals->orderBy('id', 'desc')->paginate($this->count, ['*'], 'all_clients'); - } + } ; return view( 'livewire.clients-table', [ - 'deals' => $deals, + 'deals' => $deals, 'statuses' => DealStatus::class ] ); - } } +} diff --git a/app/Livewire/CreateClientForm.php b/app/Livewire/CreateClientForm.php index 2007c7e..6f7d2da 100644 --- a/app/Livewire/CreateClientForm.php +++ b/app/Livewire/CreateClientForm.php @@ -28,11 +28,13 @@ class CreateClientForm extends Component 'client.secondName.required' => 'Необходимо указать фамилию клиента', 'client.phone.required' => 'Необходимо указать телефон без кода страны "+7" или "8"', 'client.phone.regex' => 'Телефон должен содержать 10 симвлов без указания кода страны "+7" или "8"', - 'client.phone.unique' => 'Клиент с таким телефоном уже существует' + 'client.phone.unique' => 'Клиент с таким телефоном уже существует', + 'agent.integer' => 'Необходимо указать агента, от которого добавляется контакт' ]; protected function rules() { return [ + 'agent' => ['required', 'integer'], 'client.firstName' => ['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'] @@ -49,7 +51,14 @@ public function mount() 'complexId' => '' ]; $this->status = self::NEW; - $this->agent = false; + if ($agent = Agent::where('user_id', auth()->user()->id)->first()) + { + $this->agent = $agent->id; + } + else + { + $this->agent = false; + } } public function update() { @@ -127,7 +136,6 @@ public function back() public function save() { $validated = $this->validate($this->rules()); - $agent = $this->agent || Agent::where('user_id', auth()->user()->id)->first()->id; $phone = '+7' . $this->client['phone']; $newUser = User::updateOrCreate( ['phone' => $phone], @@ -137,7 +145,7 @@ public function save() ] ); $data = [ - 'agent_id' => $agent + 'agent_id' => $this->agent , 'client_id' => $newUser->id , diff --git a/app/Models/Agent/Agent.php b/app/Models/Agent/Agent.php index 2a7c7a5..7ba37e8 100644 --- a/app/Models/Agent/Agent.php +++ b/app/Models/Agent/Agent.php @@ -13,55 +13,63 @@ use App\Models\User\Role; use App\Models\Deal\Deal; +use App\Notifications\AgentCreated; + class Agent extends Model - { +{ use HasFactory; use SoftDeletes; - public const STATUS_ACTIVE = "ACTIVE"; + public const STATUS_ACTIVE = "ACTIVE"; public const STATUS_DISMISSED = "DISMISSED"; protected $fillable = [ 'user_id', 'company_id' ]; public function company() - { + { return $this->belongsTo(Company::class); - } + } public function user() - { + { return $this->belongsTo(User::class); - } + } public function deals() - { + { return $this->hasMany(Deal::class); - } + } protected static function booted(): void - { + { static::created(function (Agent $agent) - { + { UserRole::create([ 'user_id' => $agent->user_id, 'role_id' => Role::AGENT ]); - }); + $agent->notify(); + }); static::updated(function (Agent $agent) - { + { if ($agent->trashed()) - { + { UserRole::where([ 'user_id' => $agent->user_id, 'role_id' => Role::AGENT ])->delete(); - } + } else - { + { UserRole::create([ 'user_id' => $agent->user_id, 'role_id' => Role::AGENT ]); - } - }); - } + } + }); } + private function notify() + { + $this->user->notify(new AgentCreated($this)); + return true; + } +} diff --git a/app/Notifications/AgentCreated.php b/app/Notifications/AgentCreated.php new file mode 100644 index 0000000..7cdcd92 --- /dev/null +++ b/app/Notifications/AgentCreated.php @@ -0,0 +1,59 @@ +agent = $agent; + } + + /** + * Get the notification's delivery channels. + * + * @return array + */ + 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 + */ + public function toArray(object $notifiable): array + { + return [ + // + ]; + } +} diff --git a/lang/ru.json b/lang/ru.json index f6a9108..cb018a6 100644 --- a/lang/ru.json +++ b/lang/ru.json @@ -30,5 +30,7 @@ "Email": "Электронная почта", "SelfEmployer name": "ФИО полностью", "SoleProperty name": "ФИО полностью", - "contract status new": "Новый" + "contract status new": "Новый", + "Agent was created": "Создана новая учетная запись агента", + "Your account was attached as agent": "Ваша учетная запись была привязана в качестве агента в" } \ No newline at end of file diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 64c5d1c..962189f 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -88,6 +88,9 @@ @include('left-panel')
+ @foreach ($errors->all() as $error) + {{ $error }} + @endforeach @yield('content')
diff --git a/resources/views/layouts/guest.blade.php b/resources/views/layouts/guest.blade.php index 3e15cb0..e28bf60 100644 --- a/resources/views/layouts/guest.blade.php +++ b/resources/views/layouts/guest.blade.php @@ -5,7 +5,7 @@ - Laravel + {{ config('app.name', 'Laravel') }} diff --git a/resources/views/livewire/agents-table.blade.php b/resources/views/livewire/agents-table.blade.php index a89168d..2971ac6 100644 --- a/resources/views/livewire/agents-table.blade.php +++ b/resources/views/livewire/agents-table.blade.php @@ -16,6 +16,23 @@ {{ $agent->user->email }} + @if (!$agent->trashed()) + + + + + + + + + @else + + {{ $agent->deleted_at->diffForHumans() }} + + @endif @endforeach diff --git a/resources/views/livewire/main-menu.blade.php b/resources/views/livewire/main-menu.blade.php index b5e712f..7c14ef8 100644 --- a/resources/views/livewire/main-menu.blade.php +++ b/resources/views/livewire/main-menu.blade.php @@ -1,6 +1,6 @@