Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 109 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| UserController | |
0.00% |
0 / 109 |
|
0.00% |
0 / 8 |
552 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| users | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
56 | |||
| user_exists | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| userDetails | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| add_by_email | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
| reactivateIndividualUsers | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| pushItToHubspot | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| recalculateUserInfo | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2\Company; |
| 4 | |
| 5 | use App\Enums\HttpStatusCode; |
| 6 | use App\Http\Controllers\Controller; |
| 7 | use App\Http\Models\Auth\User; |
| 8 | use App\Http\Models\FlyCutUsage; |
| 9 | use App\Http\Models\FlyMsgUserDailyUsage; |
| 10 | use App\Http\Models\Subscription; |
| 11 | use App\Http\Models\UserInfo; |
| 12 | use App\Http\Requests\CompanyUserExistsFormRequest; |
| 13 | use App\Http\Requests\CreateCompanyUserByEmailRequest; |
| 14 | use App\Http\Requests\ReactivateIndividualUsersRequest; |
| 15 | use App\Http\Resources\ClientManagementUserResource; |
| 16 | use App\Http\Resources\ClientManagementUserV2Resource; |
| 17 | use App\Http\Resources\UserDetailsResource; |
| 18 | use App\Http\Services\Admin\Companies\CompanyUsersService; |
| 19 | use App\Http\Services\Admin\Users\AdminUsersService; |
| 20 | use App\Http\Services\Admin\Users\IndividualUsersService; |
| 21 | use App\Http\Services\HubspotServiceV2; |
| 22 | use App\Services\UserInfo\FlyMsgUserDailyUsageService; |
| 23 | use App\Services\UserInfo\UserInfoService; |
| 24 | use Illuminate\Http\JsonResponse; |
| 25 | use Illuminate\Http\Request; |
| 26 | use Illuminate\Support\Facades\Event; |
| 27 | use Illuminate\Support\Facades\Log; |
| 28 | |
| 29 | class UserController extends Controller |
| 30 | { |
| 31 | public function __construct( |
| 32 | private CompanyUsersService $companyUsersService, |
| 33 | private IndividualUsersService $individualUsersService, |
| 34 | private readonly UserInfoService $userInfoService, |
| 35 | private readonly FlyMsgUserDailyUsageService $flyMsgUserDailyUsageService, |
| 36 | private readonly AdminUsersService $adminUsersService, |
| 37 | ) {} |
| 38 | |
| 39 | |
| 40 | public function users(Request $request): JsonResponse |
| 41 | { |
| 42 | $deactivated = $request->has('deactivated') && $request->deactivated == "true"; |
| 43 | $perPage = intval(request('perPage', 0)); |
| 44 | $page = intval(request('page', 1)); |
| 45 | $filter = request('filter', ''); |
| 46 | $categories = request('categories', ''); |
| 47 | $license_types = request('selectedSubscriptionTypes', ''); |
| 48 | $account_status_list = request('selectedAccountStatus', ''); |
| 49 | $extension_list = request('selectedExtensions', ''); |
| 50 | $sortBy = request('sort_by', ''); |
| 51 | $sortOrder = request('sort_order', ''); |
| 52 | |
| 53 | if ($categories) { |
| 54 | $categories = explode(',', urldecode($categories)); |
| 55 | } else { |
| 56 | $categories = []; |
| 57 | } |
| 58 | |
| 59 | if ($license_types) { |
| 60 | $license_types = explode(',', urldecode($license_types)); |
| 61 | } else { |
| 62 | $license_types = []; |
| 63 | } |
| 64 | |
| 65 | $account_status = [$account_status_list]; |
| 66 | |
| 67 | if (strpos($account_status_list, ',') !== false) { |
| 68 | $account_status = explode(",", $account_status_list); |
| 69 | } |
| 70 | |
| 71 | if ($extension_list === '' || $extension_list === null) { |
| 72 | $extensions = []; // Set empty array if no value is provided |
| 73 | } else { |
| 74 | // Convert to an array if it's a comma-separated string |
| 75 | $extensions = explode(',', $extension_list); |
| 76 | |
| 77 | // Convert values to integers |
| 78 | $extensions = array_map('intval', $extensions); |
| 79 | } |
| 80 | |
| 81 | $userData = $this->adminUsersService->getUsersPaginated($filter, $deactivated, $perPage, $page, $categories, $sortBy, $sortOrder, $license_types, $account_status, $extensions); |
| 82 | |
| 83 | return response()->json( |
| 84 | data: [ |
| 85 | 'success' => true, |
| 86 | 'data' => [ |
| 87 | 'items' => ClientManagementUserV2Resource::collection($userData['users']), |
| 88 | 'total' => $userData['total'], |
| 89 | 'total_pages' => $userData['total_pages'], |
| 90 | 'current_page' => $userData['current_page'], |
| 91 | ], |
| 92 | ], |
| 93 | status: HttpStatusCode::OK->value |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | public function user_exists(CompanyUserExistsFormRequest $request) |
| 98 | { |
| 99 | $company_id = $request->company_id; |
| 100 | $data = $request->validated(); |
| 101 | $emails = $data['emails']; |
| 102 | |
| 103 | $exists = $this->companyUsersService->userEmailsExists($company_id, $emails); |
| 104 | |
| 105 | return response()->json($exists, 200); |
| 106 | } |
| 107 | |
| 108 | public function userDetails(User $user) |
| 109 | { |
| 110 | $userInfo = UserInfo::where('email', $user->email)->first(); |
| 111 | |
| 112 | $data = [ |
| 113 | 'user' => filled($userInfo) ? new UserDetailsResource($userInfo) : [] |
| 114 | ]; |
| 115 | |
| 116 | return response()->json($data); |
| 117 | } |
| 118 | |
| 119 | public function add_by_email(CreateCompanyUserByEmailRequest $request) |
| 120 | { |
| 121 | $validated_data = $request->validated(); |
| 122 | |
| 123 | $admin = auth()->user(); |
| 124 | $companyId = $request->company_id; |
| 125 | |
| 126 | $result = $this->companyUsersService->addByEmail($validated_data, $companyId, $admin); |
| 127 | |
| 128 | return response()->json( |
| 129 | data: [ |
| 130 | 'success' => true, |
| 131 | 'message' => "{$result['invitation_sent_count']} users invited successfully", |
| 132 | 'users' => $result['users'], |
| 133 | ], |
| 134 | status: 200 |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | public function reactivateIndividualUsers(ReactivateIndividualUsersRequest $request): JsonResponse |
| 139 | { |
| 140 | $validatedData = $request->validated(); |
| 141 | $userIds = array_column($validatedData['users'], 'id'); |
| 142 | |
| 143 | $result = $this->individualUsersService->reactivateUsers($userIds, auth()->user()); |
| 144 | |
| 145 | return response()->json($result, 200); |
| 146 | } |
| 147 | |
| 148 | public function pushItToHubspot(User $user) |
| 149 | { |
| 150 | $userInfoDispatcher = UserInfo::getEventDispatcher(); |
| 151 | UserInfo::unsetEventDispatcher(); |
| 152 | $userDispatcher = User::getEventDispatcher(); |
| 153 | User::unsetEventDispatcher(); |
| 154 | $dailyDispatcher = FlyMsgUserDailyUsage::getEventDispatcher(); |
| 155 | FlyMsgUserDailyUsage::unsetEventDispatcher(); |
| 156 | |
| 157 | $this->userInfoService->pushItToHubspot($user->id, false); |
| 158 | |
| 159 | if ($userInfoDispatcher) { |
| 160 | UserInfo::setEventDispatcher($userInfoDispatcher); |
| 161 | } |
| 162 | if ($userDispatcher) { |
| 163 | User::setEventDispatcher($userDispatcher); |
| 164 | } |
| 165 | if ($dailyDispatcher) { |
| 166 | FlyMsgUserDailyUsage::setEventDispatcher($dailyDispatcher); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | public function recalculateUserInfo(User $user) |
| 171 | { |
| 172 | ini_set('memory_limit', '-1'); |
| 173 | ini_set('max_execution_time', '1800'); |
| 174 | $userInfoDispatcher = UserInfo::getEventDispatcher(); |
| 175 | UserInfo::unsetEventDispatcher(); |
| 176 | $userDispatcher = User::getEventDispatcher(); |
| 177 | User::unsetEventDispatcher(); |
| 178 | $subscriptionDispatcher = Subscription::getEventDispatcher(); |
| 179 | Subscription::unsetEventDispatcher(); |
| 180 | $dailyDispatcher = FlyMsgUserDailyUsage::getEventDispatcher(); |
| 181 | FlyMsgUserDailyUsage::unsetEventDispatcher(); |
| 182 | $flyCutDispatcher = FlyCutUsage::getEventDispatcher(); |
| 183 | FlyCutUsage::unsetEventDispatcher(); |
| 184 | |
| 185 | $this->userInfoService->recalculateFlyCutUsage($user->id); |
| 186 | |
| 187 | $userInfo = $this->userInfoService->processUser($user); |
| 188 | $this->flyMsgUserDailyUsageService->processUserUsage($user); |
| 189 | $this->flyMsgUserDailyUsageService->processUserSummaryUsage($user); |
| 190 | $user->user_info_id = $userInfo->id; |
| 191 | $user->save(); |
| 192 | $this->userInfoService->pushItToHubspot($user->id, false); |
| 193 | |
| 194 | if ($userInfoDispatcher) { |
| 195 | UserInfo::setEventDispatcher($userInfoDispatcher); |
| 196 | } |
| 197 | if ($subscriptionDispatcher) { |
| 198 | Subscription::setEventDispatcher($subscriptionDispatcher); |
| 199 | } |
| 200 | if ($userDispatcher) { |
| 201 | User::setEventDispatcher($userDispatcher); |
| 202 | } |
| 203 | if ($dailyDispatcher) { |
| 204 | FlyMsgUserDailyUsage::setEventDispatcher($dailyDispatcher); |
| 205 | } |
| 206 | if ($flyCutDispatcher) { |
| 207 | FlyCutUsage::setEventDispatcher($flyCutDispatcher); |
| 208 | } |
| 209 | |
| 210 | return response()->json([ |
| 211 | 'success' => true, |
| 212 | 'data' => UserInfo::where('user_id', $user->id)->first() |
| 213 | ], 200); |
| 214 | } |
| 215 | } |