From 2a8d22aa1e850707be34e9a23e3b5e41634d2e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 6 Mar 2024 17:38:09 +0100 Subject: [PATCH] Add ClientRegisterService class for client registration --- .../Authentication/ClientRegisterService.php | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 app/Livewire/BillingPortal/Authentication/ClientRegisterService.php diff --git a/app/Livewire/BillingPortal/Authentication/ClientRegisterService.php b/app/Livewire/BillingPortal/Authentication/ClientRegisterService.php new file mode 100644 index 000000000000..f24b0b7a4bcf --- /dev/null +++ b/app/Livewire/BillingPortal/Authentication/ClientRegisterService.php @@ -0,0 +1,100 @@ +company->client_registration_fields as $field) { + if ($field['visible'] ?? true) { + $rules[$field['key']] = $field['required'] ? ['bail', 'required'] : ['sometimes']; + } + } + + foreach ($rules as $field => $properties) { + if ($field === 'email') { + $rules[$field] = array_merge($rules[$field], ['email:rfc,dns', 'max:191', Rule::unique('client_contacts')->where('company_id', $this->company->id)]); + } + + if ($field === 'current_password') { + $rules[$field] = array_merge($rules[$field], ['string', 'min:6', 'confirmed']); + } + } + + if ($this->company->settings->client_portal_terms || $this->company->settings->client_portal_privacy_policy) { + $rules['terms'] = ['required']; + } + + return $rules; + } + + public function createClient(array $data): Client + { + $client = ClientFactory::create($this->company->id, $this->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'] : $this->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 createClientContact(array $data, Client $client): ClientContact + { + $client_contact = ClientContactFactory::create($this->company->id, $this->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; + } +} \ No newline at end of file