Skip to content

Commit 54500b8

Browse files
committed
feedback fix
1 parent 2659c8b commit 54500b8

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

src/Domains/Connectors/WooCommerce/Webhooks/SyncExternalSimlimitesUserWebhookJob.php

+42-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Kanvas\Connectors\Esim\Webhooks;
5+
namespace Kanvas\Connectors\WooCommerce\Webhooks;
66

77
use Kanvas\Auth\DataTransferObject\RegisterInput;
88
use Kanvas\Auth\Actions\CreateUserAction;
@@ -29,10 +29,11 @@ public function execute(): array
2929
];
3030
}
3131

32-
try {
33-
$user = Users::getByEmail($userData['email']);
34-
return $this->handleExistingUser($user, $userData);
35-
} catch (ModelNotFoundException) {
32+
$userExists = $this->checkUserExists($userData['email']);
33+
34+
if ($userExists) {
35+
return $this->handleExistingUser($userData);
36+
} else {
3637
$user = $this->createNewUser($userData);
3738

3839
return [
@@ -49,25 +50,45 @@ public function execute(): array
4950
}
5051
}
5152

52-
private function handleExistingUser(Users $user, array $userData): array
53+
private function checkUserExists(string $email): bool
5354
{
5455
try {
55-
UsersRepository::belongsToThisApp($user, $this->receiver->app);
56-
$this->updateExistingUser($user, $userData);
57-
58-
return [
59-
'message' => 'User already exists in this app, data updated',
60-
'user_id' => $user->getId(),
61-
'status' => 'success'
62-
];
56+
$user = Users::getByEmail($email);
57+
try {
58+
UsersRepository::belongsToThisApp($user, $this->receiver->app);
59+
return true;
60+
} catch (ModelNotFoundException) {
61+
return true;
62+
}
6363
} catch (ModelNotFoundException) {
64-
$this->registerExistingUserInApp($user, $userData);
64+
return false;
65+
}
66+
}
6567

66-
return [
67-
'message' => 'User exists but was added to this app',
68-
'user_id' => $user->getId(),
69-
'status' => 'success'
70-
];
68+
private function handleExistingUser(array $userData): array
69+
{
70+
try {
71+
$user = Users::getByEmail($userData['email']);
72+
try {
73+
UsersRepository::belongsToThisApp($user, $this->receiver->app);
74+
$this->updateExistingUser($user, $userData);
75+
76+
return [
77+
'message' => 'User already exists in this app, data updated',
78+
'user_id' => $user->getId(),
79+
'status' => 'success'
80+
];
81+
} catch (ModelNotFoundException) {
82+
$this->registerExistingUserInApp($user, $userData);
83+
84+
return [
85+
'message' => 'User exists but was added to this app',
86+
'user_id' => $user->getId(),
87+
'status' => 'success'
88+
];
89+
}
90+
} catch (ModelNotFoundException $e) {
91+
throw $e;
7192
}
7293
}
7394

@@ -91,7 +112,7 @@ private function registerExistingUserInApp(Users $user, array $userData): void
91112
'firstname' => $user->firstname,
92113
'lastname' => $user->lastname,
93114
'displayname' => $user->displayname,
94-
'password' => $userData['password'] ?? null,
115+
'password' => $userData['password'],
95116
'phone_number' => $user->phone_number,
96117
'cell_phone_number' => $user->cell_phone_number,
97118
'custom_fields' => $userData['custom_fields'] ?? [],

0 commit comments

Comments
 (0)