Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminCenterUserResource
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 2
812
0.00% covered (danger)
0.00%
0 / 1
 setPlan
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 1
756
1<?php
2
3namespace App\Http\Resources;
4
5use App\Http\Models\Admin\AdminUserInvitation;
6use App\Http\Models\Plans;
7use App\Traits\AccountCenter\Reporting\ChartTrait;
8use Carbon\Carbon;
9use Illuminate\Http\Request;
10use Illuminate\Http\Resources\Json\JsonResource;
11
12class AdminCenterUserResource extends JsonResource
13{
14    use ChartTrait;
15
16    public $plan;
17
18    public function setPlan($plan)
19    {
20        $this->plan = $plan;
21
22        return $this;
23    }
24
25    /**
26     * Transform the resource into an array.
27     *
28     * @return array<string, mixed>
29     */
30    public function toArray(Request $request): array
31    {
32        $group_name = $this->company_group ? $this->company_group->name : 'Not Assigned';
33        $subgroup_name = $this->company_group ? $this->company_group->name : 'Not Assigned';
34
35        $group_id = null;
36        $group_subgroup_id = null;
37
38        if ($this->company_group) {
39            $group_id = $this->company_group->id;
40        }
41
42        if ($this->company_group && $this->company_group->parent_id) {
43            $group_name = $this->company_group->parent->name;
44            $group_id = $this->company_group->parent->id;
45            $group_subgroup_id = $this->company_group->id;
46        }
47
48        if ($this->company_group && ! $this->company_group->parent_id) {
49            $subgroup_name = 'Not Assigned';
50        }
51
52        $statusDate = $this->created_at->toFormattedDateString();
53        if ($this->activation_date) {
54            $statusDate = Carbon::parse($this->activation_date)->toFormattedDateString();
55        }
56
57        $status = filled($this->deleted_at) ? 'Deactivated' : 'Active';
58
59        if ($this->status && $this->status != 'Deactivated' && empty($this->deleted_at)) {
60            $status = $this->status;
61        }
62
63        if ($status == 'Deactivated') {
64            $deactivatedAt = $model->deactivated_at ?? $this->deleted_at;
65            $statusDate = $deactivatedAt ? Carbon::parse($deactivatedAt)->toFormattedDateString() : null;
66        }
67
68        $user_plan = 'Not Assigned';
69        if ($this->subscription('main') && $this->subscription('main')->valid() && $this->subscription('main')->plan) {
70            $user_plan = $this->subscription('main')->plan->title;
71        } elseif ($this->subscription('invitation')) {
72            $identifier = $this->subscription('invitation')->identifier;
73            $user_plan = $this->parseSubscriptionPlanName($identifier);
74        } elseif ($this->status == 'Invited') {
75            $invitation = AdminUserInvitation::where('email', $this->email)->first();
76            if (filled($invitation?->plan_id)) {
77                $plan = Plans::find($invitation->plan_id);
78                $identifier = $plan?->identifier ?? 'freemium';
79                $user_plan = $this->parseSubscriptionPlanName($identifier);
80            }
81        }
82
83        $user_properties = [
84            'id' => $this->id,
85            'first_name' => $this->first_name,
86            'last_name' => $this->last_name,
87            'email' => $this->email,
88            'role' => role($this->role),
89            'group' => $group_name,
90            'sub_group' => $subgroup_name,
91            'group_subgroup' => $subgroup_name != 'Not Assigned' ? $subgroup_name : $group_name,
92            'licenseType' => $this->plan ? $this->plan->title : $user_plan,
93            'status' => $status,
94            'created_at' => $this->created_at,
95            'user_id' => $this->id,
96            'group_id' => $group_id,
97            'subgroup_id' => $group_subgroup_id,
98            'statusDate' => $statusDate,
99        ];
100
101        if ($user_properties['status'] == 'Invited') {
102            $user_properties['invitation_link'] = $this->getInvitationLinkForAdminPortal();
103            $invitation = AdminUserInvitation::where('email', $this->email)->first();
104            if ($invitation) {
105                $user_properties['statusDate'] = $invitation?->updated_at?->toDateString();
106
107                if (filled($invitation->role_name)) {
108                    $user_properties['role'] = $invitation->role_name;
109                }
110            }
111        }
112        if ($this->sales_pro_team_manager) {
113            $user_properties['groups_admin'] = $this->sales_pro_team_manager?->company_group_ids;
114        }
115
116        return $user_properties;
117    }
118}