Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientManagementInvitedUserResource
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 toArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildUser
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Http\Resources;
4
5use Illuminate\Http\Request;
6use MongoDB\BSON\ObjectId;
7use App\Traits\SubscriptionTrait;
8use App\Http\Models\Admin\AdminUserInvitation;
9use App\Http\Models\Plans;
10use Illuminate\Http\Resources\Json\JsonResource;
11
12class ClientManagementInvitedUserResource extends JsonResource
13{
14    use SubscriptionTrait;
15
16    /**
17     * Transform the resource into an array.
18     *
19     * @return array<string, mixed>
20     */
21    public function toArray(Request $request): array
22    {
23        return $this->buildUser($this);
24    }
25
26    private function buildUser($model)
27    {
28        if ($model->status === "Invited" && $invitation = AdminUserInvitation::firstWhere('email', $model->email)) {
29            $model = $invitation;
30        }
31
32        $group_name = $model->company_groups->parent ?? $model->company_groups->name ?? "Not Assigned";
33        $subgroup_name = $model->company_groups->name ?? "Not Assigned";
34
35        $statusDate = match (true) {
36            !is_null($model->created_at) => $model->created_at->toFormattedDateString(),
37            default => null,
38        };
39
40        $planId = new ObjectId($model->plan_id);
41        $plan = Plans::where('_id', $planId)
42            ->whereNull('pricing_version')
43            ->firstOrFail();
44
45        $plan_name = $plan ? $plan->title : "Freemium";
46        $role = role($model->role_name);
47        $extension = "No";
48        $status = "Active";
49
50        return [
51            'id' => $model->id,
52            'first_name' => $model->first_name,
53            'last_name' => $model->last_name,
54            'name' => $model->first_name . " " . $model->last_name,
55            'email' => $model->email,
56            'company' => $model->company?->name ?? "Not Assigned",
57            'company_slug' => $model->company?->slug ?? null,
58            'company_has_groups' => false,
59            'extension' => $extension,
60            'role' => $role,
61            'group' => $group_name,
62            'subgroup' => $subgroup_name,
63            'licenseType' => $plan_name,
64            'status' => $status,
65            'invitation_link' => $model->getInvitationLinkForAdminPortal(),
66            'created_at' => $model->created_at ?? $model->updated_at,
67            'statusDate' => $statusDate,
68        ];
69    }
70}