2
2
3
3
declare (strict_types=1 );
4
4
5
- namespace Kanvas \Connectors \Esim \Webhooks ;
5
+ namespace Kanvas \Connectors \WooCommerce \Webhooks ;
6
6
7
7
use Kanvas \Auth \DataTransferObject \RegisterInput ;
8
8
use Kanvas \Auth \Actions \CreateUserAction ;
@@ -29,10 +29,11 @@ public function execute(): array
29
29
];
30
30
}
31
31
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 {
36
37
$ user = $ this ->createNewUser ($ userData );
37
38
38
39
return [
@@ -49,25 +50,45 @@ public function execute(): array
49
50
}
50
51
}
51
52
52
- private function handleExistingUser ( Users $ user , array $ userData ): array
53
+ private function checkUserExists ( string $ email ): bool
53
54
{
54
55
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
+ }
63
63
} catch (ModelNotFoundException ) {
64
- $ this ->registerExistingUserInApp ($ user , $ userData );
64
+ return false ;
65
+ }
66
+ }
65
67
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 ;
71
92
}
72
93
}
73
94
@@ -91,7 +112,7 @@ private function registerExistingUserInApp(Users $user, array $userData): void
91
112
'firstname ' => $ user ->firstname ,
92
113
'lastname ' => $ user ->lastname ,
93
114
'displayname ' => $ user ->displayname ,
94
- 'password ' => $ userData ['password ' ] ?? null ,
115
+ 'password ' => $ userData ['password ' ],
95
116
'phone_number ' => $ user ->phone_number ,
96
117
'cell_phone_number ' => $ user ->cell_phone_number ,
97
118
'custom_fields ' => $ userData ['custom_fields ' ] ?? [],
0 commit comments