Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 115 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
UsersImport | |
0.00% |
0 / 115 |
|
0.00% |
0 / 6 |
380 | |
0.00% |
0 / 1 |
chunkSize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
shouldSaveToDB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
saveToDB | |
0.00% |
0 / 75 |
|
0.00% |
0 / 1 |
72 | |||
createInstancyAccount | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
updateSubcription | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
sendWelcomeEmail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Imports; |
4 | |
5 | use Carbon\Carbon; |
6 | use App\Http\Models\Plans; |
7 | use Illuminate\Support\Str; |
8 | use App\Helpers\FlyMSGLogger; |
9 | use App\Http\Models\Auth\Role; |
10 | use App\Http\Models\Auth\User; |
11 | use App\Events\User\Registered; |
12 | use App\Http\Models\Auth\UserRole; |
13 | use App\Exceptions\ExpectedException; |
14 | use App\Http\Models\Admin\Company; |
15 | use App\Http\Models\Admin\CompanyGroup; |
16 | use App\Http\Models\SalesProTeamManager; |
17 | use App\Http\Services\InstancyServiceV2; |
18 | use MongoDB\BSON\UTCDateTime; |
19 | |
20 | class UsersImport extends AbstractImportable |
21 | { |
22 | /** |
23 | * Set the chunk size for better performance with large datasets. |
24 | */ |
25 | public function chunkSize(): int |
26 | { |
27 | return 1000; // Example chunk size, adjust as necessary |
28 | } |
29 | |
30 | public function shouldSaveToDB(): bool |
31 | { |
32 | return true; |
33 | } |
34 | |
35 | public function saveToDB(array $row) |
36 | { |
37 | $admin = auth()->user(); |
38 | |
39 | $first_name = $row[0] ?? null; |
40 | $last_name = $row[1] ?? null; |
41 | $email = $row[2] ?? null; |
42 | $group_name = $row[3] ?? null; |
43 | $manager_first_name = $row[4] ?? null; |
44 | $manager_last_name = $row[5] ?? null; |
45 | $manager_email = $row[6] ?? null; |
46 | $plan_name = $row[7] ?? null; |
47 | $role_1 = $row[8] ?? null; |
48 | $role_2 = $row[9] ?? null; |
49 | $role_3 = $row[10] ?? null; |
50 | $role_4 = $row[11] ?? null; |
51 | |
52 | |
53 | $plan = match ($plan_name) { |
54 | "Sales Pro" => Plans::whereTitle("FlyMSG.io Sales Pro - YR Plan")->first(), |
55 | "Starter" => Plans::whereTitle("Starter Yearly")->first(), |
56 | "Growth" => Plans::whereTitle("Growth Yearly")->first(), |
57 | "Sales Pro Teams - ENT" => Plans::whereTitle("Pro Plan Teams - ENT")->first(), |
58 | "Sales Pro Teams - SMB" => Plans::whereTitle("Pro Plan Teams - SMB")->first(), |
59 | default => null, |
60 | }; |
61 | |
62 | $role_name = role($role_1); |
63 | $role = Role::where("name", $role_name)->first(); |
64 | if (!$role) { |
65 | throw new ExpectedException("Role not found: " . $role_name . " " . $role_1 . " " . $role_2 . " " . $role_3 . " " . $role_4); |
66 | } |
67 | |
68 | $group = CompanyGroup::where("name", $group_name)->first(); |
69 | if (!$group) { |
70 | $group = new CompanyGroup([ |
71 | 'name' => $group_name |
72 | ]); |
73 | $group->company_id = $admin->company_id; |
74 | $group->save(); |
75 | } |
76 | |
77 | $manager = SalesProTeamManager::where("email", $manager_email)->first(); |
78 | if (!$manager) { |
79 | $manager = new SalesProTeamManager([ |
80 | 'first_name' => $manager_first_name, |
81 | 'last_name' => $manager_last_name, |
82 | 'email' => $manager_email |
83 | ]); |
84 | $manager->company_id = $admin->company_id; |
85 | $manager->company_group_id = $group->id; |
86 | $manager->save(); |
87 | } |
88 | |
89 | $user = new User([ |
90 | 'first_name' => $first_name, |
91 | 'last_name' => $last_name, |
92 | 'email' => $email, |
93 | 'onboardingv2_presented' => true |
94 | ]); |
95 | |
96 | $password = Str::password(16); |
97 | |
98 | $user->password = bcrypt($password); |
99 | $user->company_group_id = $group->id; |
100 | $user->company_id = $admin->company_id; |
101 | $user->sales_pro_team_manager_id = $manager->id; |
102 | $user->imported_by = $admin->id; |
103 | $user->email_verified_at = new UTCDateTime(now()->getTimestamp() * 1000); |
104 | $user->save(); |
105 | |
106 | // Revoke all current roles |
107 | UserRole::where("user_id", $user->id)->delete(); |
108 | // Assign new role |
109 | UserRole::create([ |
110 | 'user_id' => $user->id, |
111 | 'role_id' => $role->id, |
112 | ]); |
113 | |
114 | $data = [ |
115 | "email" => $user->email, |
116 | "first_name" => $user->first_name, |
117 | "last_name" => $user->last_name, |
118 | ]; |
119 | |
120 | Registered::dispatch($user, $data); |
121 | |
122 | try { |
123 | $this->updateSubcription($user, $plan, $plan_name); |
124 | } catch (\Throwable $th) { |
125 | FlyMSGLogger::logError("UsersImport : Failed to update user subscription", $th); |
126 | // Send this to devs |
127 | } |
128 | if (Str::contains($plan_name, "Pro") || Str::contains($plan_name, "pro")) { |
129 | try { |
130 | $this->createInstancyAccount($user->email); |
131 | } catch (\Throwable $th) { |
132 | FlyMSGLogger::logError("UsersImport : Failed to create the users instancy account", $th); |
133 | // Send this to devs |
134 | } |
135 | } |
136 | |
137 | $this->sendWelcomeEmail($user, $password); |
138 | } |
139 | |
140 | private function createInstancyAccount($email) |
141 | { |
142 | $instancyService = new InstancyServiceV2; |
143 | |
144 | $user = User::firstWhere('email', $email); |
145 | $group = CompanyGroup::find($user->company_group_id); |
146 | $company = Company::find($user->company_id); |
147 | |
148 | $groupId = false; |
149 | if ($company) { |
150 | $groupId = $company->instancy_id; |
151 | } elseif ($group) { |
152 | $groupId = $group->instancy_id; |
153 | } |
154 | |
155 | $instancyService->createInstancyUser($email, $groupId); |
156 | } |
157 | |
158 | private function updateSubcription($user, $plan, $user_plan_name) |
159 | { |
160 | try { |
161 | if ($user->subscribed('main')) { |
162 | $user->subscription('main')->cancel(); |
163 | } else { |
164 | $subscription = $user->subscriptions()->latest()->first(); |
165 | if ($subscription) { |
166 | $subscription->update([ |
167 | 'stripe_status' => 'canceled', |
168 | 'ends_at' => Carbon::now()->toDateTimeString(), |
169 | ]); |
170 | } |
171 | } |
172 | } catch (\Throwable $th) { |
173 | FlyMSGLogger::logError("UsersImport", $th); |
174 | $subscription = $user->subscriptions() |
175 | ->latest() |
176 | ->first(); |
177 | if ($subscription) { |
178 | $subscription->update([ |
179 | 'stripe_status' => 'canceled', |
180 | 'ends_at' => Carbon::now()->toDateTimeString(), |
181 | ]); |
182 | } |
183 | } |
184 | |
185 | // Only sales pro team users |
186 | $end_date_c = Carbon::now()->addMonths(12)->endOfDay(); |
187 | |
188 | $user->subscriptions()->create([ |
189 | 'name' => 'main', |
190 | 'stripe_status' => 'active', |
191 | 'stripe_plan' => $plan->stripe_id, |
192 | 'quantity' => "1", |
193 | 'ends_at' => $end_date_c->toDateTimeString(), |
194 | 'starts_at' => Carbon::now()->startOfDay()->toDateTimeString(), |
195 | ]); |
196 | } |
197 | |
198 | |
199 | private function sendWelcomeEmail() {} |
200 | } |