Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
RegisterController | |
0.00% |
0 / 44 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validator | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
validateEmailUniqueness | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
register | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | namespace App\Http\Controllers\v1\UserAuth; |
4 | |
5 | use App\Traits\CompanyAuth; |
6 | use Illuminate\Http\Request; |
7 | use App\Http\Models\Auth\User; |
8 | use App\Events\User\Registered; |
9 | use Illuminate\Http\JsonResponse; |
10 | use Illuminate\Support\Facades\Hash; |
11 | use Illuminate\Support\Facades\Validator; |
12 | use App\Http\Controllers\v1\Auth\RegisterController as BaseRegisterController; |
13 | use App\Http\Services\EmailVerificationService; |
14 | |
15 | class RegisterController extends BaseRegisterController |
16 | { |
17 | use CompanyAuth; |
18 | |
19 | public function __construct( |
20 | private readonly EmailVerificationService $emailVerificationService |
21 | ) {} |
22 | |
23 | /** |
24 | * Validate request data |
25 | */ |
26 | protected function validator(array $data): \Illuminate\Contracts\Validation\Validator |
27 | { |
28 | return Validator::make($data, [ |
29 | 'first_name' => ['required', 'string', 'max:255'], |
30 | 'last_name' => ['required', 'string', 'max:255'], |
31 | 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], |
32 | 'password' => ['required', 'string', 'min:8', 'confirmed'], |
33 | 'created_by' => ['nullable', 'in:admin'], |
34 | 'referral_key' => ['nullable', 'string'], |
35 | ]); |
36 | } |
37 | |
38 | /** |
39 | * Validate email uniqueness |
40 | */ |
41 | protected function validateEmailUniqueness(array $data): \Illuminate\Contracts\Validation\Validator |
42 | { |
43 | return Validator::make($data, [ |
44 | 'email' => ['unique:users'], |
45 | ]); |
46 | } |
47 | |
48 | /** |
49 | * Create user |
50 | */ |
51 | protected function create(array $data): User |
52 | { |
53 | $referral_key = uniqid(); |
54 | |
55 | return User::create([ |
56 | 'first_name' => $data['first_name'], |
57 | 'last_name' => $data['last_name'], |
58 | 'email' => $data['email'], |
59 | 'password' => Hash::make($data['password']), |
60 | 'signup_source' => 'email', |
61 | 'hubspot_id' => null, |
62 | 'onboarding' => 'pending', |
63 | 'refered_by' => (isset($data['referral_key'])) ? $data['referral_key'] : '', |
64 | 'referral_key' => $referral_key, |
65 | 'onboardingv2_presented' => true |
66 | ]); |
67 | } |
68 | |
69 | /** |
70 | * Register user |
71 | * |
72 | * @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory |
73 | */ |
74 | public function register(Request $request): JsonResponse |
75 | { |
76 | $this->validator($request->all())->validate(); |
77 | |
78 | $data = $request->all(); |
79 | |
80 | $data['onboardingv2_presented'] = true; |
81 | |
82 | $user = $this->create($data); |
83 | |
84 | $about = $this->handleAdminInvitation($user); |
85 | |
86 | Registered::dispatch($user, $data); |
87 | |
88 | $this->emailVerificationService->generateToken($user->email); |
89 | |
90 | // Log in users who were not created by the CreateUsers command script |
91 | if ( |
92 | empty($data['created_by']) || |
93 | (filled($data['created_by']) && $data['created_by'] !== 'admin') |
94 | ) { |
95 | $request = Request::create(config('app.url') . '/romeo/api/v1/user/auth/login', 'POST', [ |
96 | 'email' => $request->email, |
97 | 'password' => $request->password, |
98 | ]); |
99 | |
100 | $response = app()->handle($request); |
101 | $data = json_decode($response->getContent(), true); |
102 | |
103 | if ($about && $about['company_poc']) { |
104 | $data['result']['is_company_poc'] = $about['company_poc']; |
105 | } |
106 | |
107 | return response()->json($data['result']); |
108 | } |
109 | |
110 | return response()->json($user->id); |
111 | } |
112 | } |