Skip to content

Commit 5f8914c

Browse files
committed
refactor: streamline person data handling and organization creation
1 parent d118219 commit 5f8914c

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

packages/Webkul/Admin/src/Http/Controllers/Contact/Persons/PersonController.php

+2-25
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function store(AttributeForm $request): RedirectResponse|JsonResponse
5454
{
5555
Event::dispatch('contacts.person.create.before');
5656

57-
$person = $this->personRepository->create($this->sanitizeRequestedPersonData($request->all()));
57+
$person = $this->personRepository->create($request->all());
5858

5959
Event::dispatch('contacts.person.create.after', $person);
6060

@@ -97,7 +97,7 @@ public function update(AttributeForm $request, int $id): RedirectResponse|JsonRe
9797
{
9898
Event::dispatch('contacts.person.update.before', $id);
9999

100-
$person = $this->personRepository->update($this->sanitizeRequestedPersonData($request->all()), $id);
100+
$person = $this->personRepository->update($request->all(), $id);
101101

102102
Event::dispatch('contacts.person.update.after', $person);
103103

@@ -174,27 +174,4 @@ public function massDestroy(MassDestroyRequest $massDestroyRequest): JsonRespons
174174
'message' => trans('admin::app.contacts.persons.index.delete-success'),
175175
]);
176176
}
177-
178-
/**
179-
* Sanitize requested person data and return the clean array.
180-
*/
181-
private function sanitizeRequestedPersonData(array $data): array
182-
{
183-
if (
184-
array_key_exists('organization_id', $data)
185-
&& empty($data['organization_id'])
186-
) {
187-
$data['organization_id'] = null;
188-
}
189-
190-
$data['unique_id'] = $data['user_id'].'|'.$data['organization_id'].'|'.$data['emails'][0]['value'];
191-
192-
if (isset($data['contact_numbers'])) {
193-
$data['contact_numbers'] = collect($data['contact_numbers'])->filter(fn ($number) => ! is_null($number['value']))->toArray();
194-
195-
$data['unique_id'] .= '|'.$data['contact_numbers'][0]['value'];
196-
}
197-
198-
return $data;
199-
}
200177
}

packages/Webkul/Contact/src/Repositories/PersonRepository.php

+59-12
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,18 @@ public function model()
5555
*/
5656
public function create(array $data)
5757
{
58-
if (isset($data['organization_name'])) {
58+
$data = $this->sanitizeRequestedPersonData($data);
59+
60+
if (
61+
isset($data['organization_name'])
62+
&& ! empty($data['organization_name'])
63+
) {
5964
$organization = self::createOrganization($data);
6065

6166
$data['organization_id'] = $organization->id;
6267

68+
$data['user_id'] = $organization->user_id;
69+
6370
unset($data['organization_name']);
6471
}
6572

@@ -83,9 +90,14 @@ public function create(array $data)
8390
*/
8491
public function update(array $data, $id, $attributes = [])
8592
{
93+
$data = $this->sanitizeRequestedPersonData($data);
94+
8695
$data['user_id'] = empty($data['user_id']) ? null : $data['user_id'];
8796

88-
if (isset($data['organization_name'])) {
97+
if (
98+
isset($data['organization_name'])
99+
&& ! empty($data['organization_name'])
100+
) {
89101
$organization = self::createOrganization($data);
90102

91103
$data['organization_id'] = $organization->id;
@@ -136,20 +148,55 @@ public function getCustomerCount($startDate, $endDate)
136148
->count();
137149
}
138150

151+
/**
152+
* Sanitize requested person data and return the clean array.
153+
*/
154+
private function sanitizeRequestedPersonData(array $data): array
155+
{
156+
if (
157+
array_key_exists('organization_id', $data)
158+
&& empty($data['organization_id'])
159+
) {
160+
$data['organization_id'] = null;
161+
}
162+
163+
$uniqueIdParts = array_filter([
164+
$data['user_id'] ?? null,
165+
$data['organization_id'] ?? null,
166+
$data['emails'][0]['value'] ?? null,
167+
]);
168+
169+
$data['unique_id'] = implode('|', $uniqueIdParts);
170+
171+
if (isset($data['contact_numbers'])) {
172+
$data['contact_numbers'] = collect($data['contact_numbers'])->filter(fn ($number) => ! is_null($number['value']))->toArray();
173+
174+
$data['unique_id'] .= '|'.$data['contact_numbers'][0]['value'];
175+
}
176+
177+
return $data;
178+
}
179+
139180
/**
140181
* Create a organization.
141182
*/
142183
public function createOrganization(array $data)
143184
{
144-
$userId = empty($data['user_id']) ? null : $data['user_id'];
145-
146-
return $this->organizationRepository->create(
147-
array_merge($data, [
148-
'name' => $data['organization_name'],
149-
'entity_type' => 'organization',
150-
'address' => [],
151-
'user_id' => $userId,
152-
])
153-
);
185+
$organization = $this->organizationRepository->findOneWhere([
186+
'name' => $data['organization_name'],
187+
]);
188+
189+
return $organization ?: $this->organizationRepository->create([
190+
'name' => $data['organization_name'],
191+
'entity_type' => 'organization',
192+
'address' => [
193+
"address" => "",
194+
"country" => "",
195+
"state" => "",
196+
"city" => "",
197+
"postcode" => ""
198+
],
199+
'user_id' => 1,
200+
]);
154201
}
155202
}

0 commit comments

Comments
 (0)