From 6c2ddae6ad801c17c15fb1a98bb47b57d4472b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 6 Mar 2024 17:38:18 +0100 Subject: [PATCH] Refactor client registration process --- .../Auth/ContactRegisterController.php | 54 +++---------------- 1 file changed, 7 insertions(+), 47 deletions(-) diff --git a/app/Http/Controllers/Auth/ContactRegisterController.php b/app/Http/Controllers/Auth/ContactRegisterController.php index eb942c1780f3..b47dce8e54d4 100644 --- a/app/Http/Controllers/Auth/ContactRegisterController.php +++ b/app/Http/Controllers/Auth/ContactRegisterController.php @@ -11,17 +11,14 @@ namespace App\Http\Controllers\Auth; -use App\Factory\ClientContactFactory; -use App\Factory\ClientFactory; use App\Http\Controllers\Controller; use App\Http\Requests\ClientPortal\RegisterRequest; -use App\Models\Client; +use App\Livewire\BillingPortal\Authentication\ClientRegisterService; use App\Models\Company; use App\Utils\Ninja; use App\Utils\Traits\GeneratesCounter; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Hash; class ContactRegisterController extends Controller { @@ -53,53 +50,16 @@ class ContactRegisterController extends Controller public function register(RegisterRequest $request) { $request->merge(['company' => $request->company()]); + + $service = new ClientRegisterService( + company: $request->company(), + ); - $client = $this->getClient($request->all()); - $client_contact = $this->getClientContact($request->all(), $client); + $client = $service->createClient($request->all()); + $client_contact = $service->createClientContact($request->all(), $client); Auth::guard('contact')->loginUsingId($client_contact->id, true); return redirect()->intended(route('client.dashboard')); } - - private function getClient(array $data) - { - $client = ClientFactory::create($data['company']->id, $data['company']->owner()->id); - - $client->fill($data); - - $client->save(); - - if (isset($data['currency_id'])) { - $settings = $client->settings; - $settings->currency_id = isset($data['currency_id']) ? $data['currency_id'] : $data['company']->settings->currency_id; - $client->settings = $settings; - } - - $client->number = $this->getNextClientNumber($client); - $client->save(); - - if (! array_key_exists('country_id', $data) && strlen($client->company->settings->country_id) > 1) { - $client->update(['country_id' => $client->company->settings->country_id]); - } - - return $client; - } - - public function getClientContact(array $data, Client $client) - { - $client_contact = ClientContactFactory::create($data['company']->id, $data['company']->owner()->id); - $client_contact->fill($data); - - $client_contact->client_id = $client->id; - $client_contact->is_primary = true; - - if (array_key_exists('password', $data)) { - $client_contact->password = Hash::make($data['password']); - } - - $client_contact->save(); - - return $client_contact; - } }