Refactor client registration fields and add additional fields

This commit is contained in:
Benjamin Beganović 2024-04-01 19:05:05 +02:00
parent 6c6dd8f957
commit 2eb830619c

View File

@ -41,7 +41,9 @@ class RegisterOrLogin extends Component
'initial_completed' => false, 'initial_completed' => false,
]; ];
public array $fields = []; public array $register_fields = [];
public array $additional_fields = [];
public function initial() public function initial()
{ {
@ -161,6 +163,7 @@ class RegisterOrLogin extends Component
{ {
$service = new ClientRegisterService( $service = new ClientRegisterService(
company: $this->subscription->company, company: $this->subscription->company,
additional: $this->additional_fields,
); );
$rules = $service->rules(); $rules = $service->rules();
@ -197,45 +200,43 @@ class RegisterOrLogin extends Component
return; return;
} }
$this->fields = [...collect($this->subscription->company->client_registration_fields ?? [])->toArray()]; $this->register_fields = [...collect($this->subscription->company->client_registration_fields ?? [])->toArray()];
$registration_keys = collect($this->fields)->pluck('key')->toArray();
$mappings = [
'contact_first_name' => 'first_name',
'contact_last_name' => 'last_name',
'contact_email' => 'email',
'client_city' => 'city',
'client_address_line_1' => 'address1',
];
$first_gateway = collect($this->subscription->company->company_gateways) $first_gateway = collect($this->subscription->company->company_gateways)
->sortBy('sort_order') ->sortBy('sort_order')
->first(); ->first();
$gateway_fields = collect($first_gateway->driver()->getClientRequiredFields() ?? [])->map(function ($field) { $mappings = ClientRegisterService::mappings();
return [
'key' => $field['name'],
'required' => true,
'visible' => true,
'label' => str_replace(['client_', 'contact_'], '', $field['name'])
];
})->toArray();
$gateway_fields = collect($gateway_fields) collect($first_gateway->driver()->getClientRequiredFields() ?? [])
->filter(function ($field) use ($registration_keys, $mappings) { ->each(function ($field) use ($mappings) {
$mapping = $mappings[$field['key']] ?? null; $mapping = $mappings[$field['name']] ?? null;
if ($mapping === null) { if ($mapping === null) {
return true; return;
} }
return !in_array($mapping, $registration_keys); $i = collect($this->register_fields)->search(fn ($field) => $field['key'] == $mapping);
if ($i !== false) {
$this->register_fields[$i]['visible'] = true;
$this->register_fields[$i]['required'] = true;
$this->additional_fields[] = $this->register_fields[$i];
} else {
$field = [
'key' => $mapping,
'required' => true,
'visible' => true,
];
$this->register_fields[] = $field;
$this->additional_fields[] = $field;
}
}) })
->toArray(); ->toArray();
$this->fields = array_merge($this->fields, $gateway_fields);
return $this->state['register_form'] = true; return $this->state['register_form'] = true;
} }