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\Admin\AdminUserInvitation;
7use App\Http\Models\Admin\CompanyLicenses;
8use App\Http\Models\Auth\Role;
9use App\Http\Models\Auth\User;
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
66                        continue;
67                    }
68
69                    $companyLicense = CompanyLicenses::where('company_id', $userInvitation->company_id)->active()->first();
70
71                    if (! $companyLicense) {
72                        // Failed to assign plan. The company doesn't have any active license.";
73                        continue;
74                    }
75
76                    $licenseRemaining = match ($plan->identifier) {
77                        Plans::STARTER_YEARLY_IDENTIFIER => $companyLicense->total_starter_license_remaining,
78                        Plans::GROWTH_YEARLY_IDENTIFIER => $companyLicense->total_growth_license_remaining,
79                        Plans::PROFESSIONAL_YEARLY_IDENTIFIER => $companyLicense->total_sales_pro_license_remaining,
80                        Plans::ProPlanTeamsSMB => $companyLicense->total_sales_pro_teams_license_remaining,
81                        default => 0,
82                    };
83
84                    if ($licenseRemaining < 1) {
85                        // Failed to assign plan. The company doesn't have sufficient licenses for selected plan.";
86                        continue;
87                    }
88
89                    $userSub = $user->subscription('main');
90                    if ($userSub && $userSub->plan->identifier != Plans::FREEMIUM_IDENTIFIER) {
91                        $userSub->markAsCancelledOnlyInDB();
92                    }
93
94                    if ($plan->identifier != Plans::FREEMIUM_IDENTIFIER) {
95                        $user->subscriptions()->create([
96                            'name' => 'main',
97                            'stripe_status' => 'active',
98                            'stripe_plan' => $plan->stripe_id,
99                            'quantity' => '1',
100                            'ends_at' => $companyLicense->contract_end_date,
101                            'starts_at' => now()->toDateTimeString(),
102                        ]);
103                    }
104                }
105            }
106        }
107
108        return $usersInvitation;
109    }
110}