Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UserDetailsReportResource | |
0.00% |
0 / 75 |
|
0.00% |
0 / 3 |
210 | |
0.00% |
0 / 1 |
| toArray | |
0.00% |
0 / 70 |
|
0.00% |
0 / 1 |
156 | |||
| getVisibleColumns | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| checkFrontendKeysAgainstBackendKeys | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Resources; |
| 4 | |
| 5 | use App\Exceptions\ExpectedException; |
| 6 | use App\Helpers\FlyMSGLogger; |
| 7 | use Illuminate\Http\Request; |
| 8 | use Illuminate\Http\Resources\Json\JsonResource; |
| 9 | use App\Http\Models\Admin\UserDetailsColumnSetting; |
| 10 | use PHPUnit\Event\Code\Throwable; |
| 11 | |
| 12 | class UserDetailsReportResource extends JsonResource |
| 13 | { |
| 14 | /** |
| 15 | * Transform the resource into an array. |
| 16 | * |
| 17 | * @param Request $request |
| 18 | * @return array<string, mixed> |
| 19 | */ |
| 20 | public function toArray($request): array |
| 21 | { |
| 22 | // Fetch visible columns for the user |
| 23 | $visibleColumns = $this->getVisibleColumns(); |
| 24 | |
| 25 | $user = $this->user; |
| 26 | if (!$user) { |
| 27 | FlyMSGLogger::logError("UserDetailsReportResource", new ExpectedException("User not found for HS property with ID $this->id")); |
| 28 | $user = $this; |
| 29 | } |
| 30 | $status = $user->status; |
| 31 | |
| 32 | if ($user->deleted_at) { |
| 33 | $status = "Deactivated"; |
| 34 | } |
| 35 | |
| 36 | if ($user->status == "Invited") { |
| 37 | $status = "Not Active"; |
| 38 | } |
| 39 | |
| 40 | if ($user->status == "Active") { |
| 41 | $status = "Active"; |
| 42 | } |
| 43 | |
| 44 | $group_subgroup_id = null; |
| 45 | $group_id = $user->company_group->id ?? null; |
| 46 | |
| 47 | if ($user->company_group && $user->company_group->parent_id) { |
| 48 | $group_id = $user->company_group->parent->id; |
| 49 | $group_subgroup_id = $user->company_group->id; |
| 50 | } |
| 51 | |
| 52 | // Define all available columns |
| 53 | $allColumns = [ |
| 54 | "first_name" => $this->firstname, |
| 55 | "last_name" => $this->lastname, |
| 56 | "email" => $this->email, |
| 57 | "license_type" => $this->subscription_type, |
| 58 | "flymsg_account_creation_date" => $this->account_creation_date, |
| 59 | "extension_installed_on_any_browser" => ($this->flymsg_chrome_extension_installed == "Yes" || $this->flymsg_edge_extension_installed == "Yes") ? "Yes" : "No", |
| 60 | "last_login_to_flymsg" => $this->last_login, |
| 61 | "total_no_of_characters_typed_by_flymsg_by_user" => $this->total___of_characters_typed_by_flymsg_by_user, |
| 62 | "total_no_of_characters_typed_summarized_monthly_by_flymsg_by_user" => $this->total___of_characters_typed_monthly_by_flymsg_by_user, |
| 63 | "is_the_user_signed_into_flymsg_extension" => $this->signed_into_flymsg_extension, |
| 64 | "last_login_to_flylearning" => $this->last_login_to_flylearning, |
| 65 | "total_time_saved_by_flymsg_by_user" => $this->total_time_saved_by_flymsg_by_user, |
| 66 | "total_cost_savings_by_flymsg_by_user" => $this->total_cost_savings_by_flymsg_by_user, |
| 67 | "last_date_user_used_a_flycut" => $this->last_date_user_used_a_flycut, |
| 68 | "flycuts_created_count" => $this->number_of_flycuts_created_count, |
| 69 | "flycuts_created_last_date" => $this->number_of_flycuts_created_last_date, |
| 70 | "flyplates_added_to_flycuts_count" => $this->number_of_flyplates_in_flycuts_count, |
| 71 | "flyplates_added_to_flycuts_last_date" => $this->number_of_flyplates_in_flycuts_last_date, |
| 72 | "total_no_of_times_flyengage_ai_used_count" => $this->total___of_times_flyengage_used__count_, |
| 73 | "total_no_of_characters_typed_by_flyengage" => $this->total_no_of_characters_typed_by_flyengage, |
| 74 | "total_no_of_times_sentence_rewrite_ai_used_count" => $this->total___of_times_sentence_rewrite_used__count_, |
| 75 | "total_no_of_characters_typed_by_sentence_rewrite" => $this->total_no_of_characters_typed_by_sentence_rewrite, |
| 76 | "total_no_of_times_paragraph_rewrite_ai_used_count" => $this->total___of_times_paragraph_rewrite_used__count_, |
| 77 | "total_no_of_characters_typed_by_paragraph_rewrite" => $this->total_no_of_characters_typed_by_paragraph_rewrite, |
| 78 | "total_no_of_times_flyposts_used_count" => $this->total___of_times_flyposts_used__count_, |
| 79 | "total_no_of_characters_typed_by_flyposts" => $this->total_no_of_characters_typed_by_flyposts, |
| 80 | "which_browser_has_an_extension_been_installed_on" => $this->which_browser_has_an_extension_been_installed_on, |
| 81 | "flymsg_chrome_extension_installed" => $this->flymsg_chrome_extension_installed, |
| 82 | "flymsg_chrome_extension_installed_date" => $this->flymsg_chrome_extension_installed__date_, |
| 83 | "flymsg_chrome_extension_uninstalled" => $this->flymsg_chrome_extension_uninstalled, |
| 84 | "flymsg_chrome_extension_uninstalled_date" => $this->flymsg_chrome_extension_uninstalled__date_, |
| 85 | "flymsg_edge_extension_installed_date" => $this->flymsg_edge_extension_installed__date_, |
| 86 | "flymsg_edge_extension_installed" => $this->flymsg_edge_extension_installed, |
| 87 | "flymsg_edge_extension_uninstalled" => $this->flymsg_edge_extension_uninstalled, |
| 88 | "flymsg_edge_extension_uninstalled__date_" => $this->flymsg_edge_extension_uninstalled__date_, |
| 89 | "other_reason_to_sign_out_free_form_field" => $this->other_reason_to_sign_out_free_form_field, |
| 90 | "flymsg_extension_version_for_edge" => $this->flymsg_extension_version_for_edge, |
| 91 | "flymsg_extension_version_for_chrome" => $this->flymsg_extension_version_for_chrome, |
| 92 | "clicked_help_request_help_from_menu_count" => $this->clicked_settings_count, |
| 93 | "typed_words_per_minute" => $this->typed_words_per_minute, |
| 94 | "user_id" => $user->id, |
| 95 | "group_id" => $group_id, |
| 96 | "subgroup_id" => $group_subgroup_id, |
| 97 | "status" => $status ?? "Active", |
| 98 | ]; |
| 99 | |
| 100 | if (empty($visibleColumns)) { |
| 101 | return $allColumns; |
| 102 | } |
| 103 | |
| 104 | // Initialize an empty array for filtered columns |
| 105 | $filteredColumns = []; |
| 106 | |
| 107 | // Loop through visible columns and add them to filteredColumns array |
| 108 | foreach ($visibleColumns as $column) { |
| 109 | if (array_key_exists($column, $allColumns)) { |
| 110 | $filteredColumns[$column] = $allColumns[$column]; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return $filteredColumns; |
| 115 | } |
| 116 | |
| 117 | private function getVisibleColumns() |
| 118 | { |
| 119 | return UserDetailsColumnSetting::where("user_id", auth()->user()->id) |
| 120 | ->where("visible", true) |
| 121 | ->pluck("column") |
| 122 | ->toArray(); |
| 123 | } |
| 124 | |
| 125 | private function checkFrontendKeysAgainstBackendKeys($key) |
| 126 | { |
| 127 | return array_key_exists($key, $this->getVisibleColumns()); |
| 128 | } |
| 129 | } |