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