Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientManagementInvitedUserService
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 1
380
0.00% covered (danger)
0.00%
0 / 1
 moveToInvitedUsers
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 1
380
1<?php
2
3namespace App\Http\Services;
4
5use App\Enums\HttpStatusCode;
6use App\Http\Models\Auth\User;
7use App\Http\Models\Admin\AdminUserInvitation;
8use App\Http\Models\Admin\CompanyLicenses;
9use App\Http\Models\Auth\Role;
10use App\Http\Models\Plans;
11
12class ClientManagementInvitedUserService
13{
14    public function moveToInvitedUsers($validatedData)
15    {
16        $companyId = $validatedData['company_id'];
17        $roleName = $validatedData['role_name'];
18        $plan = Plans::whereNull('pricing_version')->firstWhere('identifier', $validatedData['plan_identifier']);
19        $groupId = $validatedData['group_id'] ?? null;
20        $subgroupId = $validatedData['subgroup_id'] ?? null;
21
22        $usersInvitation = AdminUserInvitation::whereIn('_id', $validatedData['user_ids'])->get();
23
24        if ($usersInvitation->isEmpty()) {
25            return response()->json([
26                'success' => false,
27                'message' => "No invited users found with the provided IDs.",
28            ], HttpStatusCode::NOT_FOUND->value);
29        }
30
31        if ($usersInvitation) {
32            foreach ($usersInvitation as $userInvitation) {
33
34                $userInvitation->company_id = $companyId;
35                $userInvitation->role_name = $roleName;
36                $userInvitation->plan_id = $plan->id;
37
38                if (filled($groupId)) {
39                    $userInvitation->company_group_id = $groupId;
40                    $userInvitation->company_subgroup_id = null;
41                }
42
43                if (filled($subgroupId)) {
44                    $userInvitation->company_subgroup_id = $subgroupId;
45                }
46
47                $userInvitation->save();
48
49                $user = User::where('email', $userInvitation->email)->first();
50
51                if ($user) {
52
53                    $user->removeAllRoles();
54                    $group_ids = [];
55                    if (($roleName == Role::GROUP_ADMIN || $roleName = Role::REPORTING_ADMIN) && ($groupId || $subgroupId)) {
56                        $group_ids = [$groupId] ?? [$subgroupId] ?? [];
57                    }
58                    $user->assignRole($roleName, $group_ids);
59
60                    if ($plan->identifier === Plans::FREEMIUM_IDENTIFIER) {
61                        $userSub = $user->subscription("main");
62                        if ($userSub && $userSub->plan->identifier != Plans::FREEMIUM_IDENTIFIER) {
63                            $userSub->markAsCancelledOnlyInDB();
64                        }
65                        continue;
66                    }
67
68                    $companyLicense = CompanyLicenses::where('company_id', $userInvitation->company_id)->active()->first();
69
70                    if (!$companyLicense) {
71                        // Failed to assign plan. The company doesn't have any active license.";
72                        continue;
73                    }
74
75                    $licenseRemaining = match ($plan->identifier) {
76                        Plans::STARTER_YEARLY_IDENTIFIER => $companyLicense->total_starter_license_remaining,
77                        Plans::GROWTH_YEARLY_IDENTIFIER => $companyLicense->total_growth_license_remaining,
78                        Plans::PROFESSIONAL_YEARLY_IDENTIFIER => $companyLicense->total_sales_pro_license_remaining,
79                        Plans::ProPlanTeamsSMB => $companyLicense->total_sales_pro_teams_license_remaining,
80                        default => 0,
81                    };
82
83                    if ($licenseRemaining < 1) {
84                        // Failed to assign plan. The company doesn't have sufficient licenses for selected plan.";
85                        continue;
86                    }
87
88                    $userSub = $user->subscription("main");
89                    if ($userSub && $userSub->plan->identifier != Plans::FREEMIUM_IDENTIFIER) {
90                        $userSub->markAsCancelledOnlyInDB();
91                    }
92
93                    if ($plan->identifier != Plans::FREEMIUM_IDENTIFIER) {
94                        $user->subscriptions()->create([
95                            'name' => 'main',
96                            'stripe_status' => 'active',
97                            'stripe_plan' => $plan->stripe_id,
98                            'quantity' => "1",
99                            'ends_at' => $companyLicense->contract_end_date,
100                            'starts_at' => now()->toDateTimeString()
101                        ]);
102                    }
103                }
104            }
105        }
106
107
108        return $usersInvitation;
109    }
110}