Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| AdminGroupsService | |
0.00% |
0 / 65 |
|
0.00% |
0 / 7 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleteGroup | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| updateGroup | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| createGroup | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| updateInstancyGroup | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| createInstancyGroup | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services\Admin\Groups; |
| 4 | |
| 5 | use App\Actions\AccountCenter\Reporting\AccountCenterReporting; |
| 6 | use App\Http\Models\Admin\Company; |
| 7 | use App\Http\Models\Admin\CompanyGroup; |
| 8 | use App\Http\Repositories\InstancyRepository; |
| 9 | use App\Http\Services\Admin\Users\AdminUsersService; |
| 10 | use Illuminate\Http\Response; |
| 11 | use stdClass; |
| 12 | |
| 13 | class AdminGroupsService extends AccountCenterReporting |
| 14 | { |
| 15 | public function __construct( |
| 16 | private InstancyRepository $instancyRepository, |
| 17 | private AdminUsersService $adminUsersService, |
| 18 | ) { } |
| 19 | |
| 20 | public function getGroup(string $id) { |
| 21 | return CompanyGroup::find($id); |
| 22 | } |
| 23 | |
| 24 | public function deleteGroup(CompanyGroup $group) { |
| 25 | if ($group->users()->count() > 0) { |
| 26 | $group->users()->update(['company_group_id' => null]); |
| 27 | $user_ids = $group->users()->pluck('_id')->toArray(); |
| 28 | $this->adminUsersService->moveUsersToGroup($user_ids, null); |
| 29 | } |
| 30 | |
| 31 | // Of a top parent group is being deleted, delete subgroups too? |
| 32 | if ($group->subgroups()->count() > 0) { |
| 33 | foreach ($group->subgroups as $subgroup) { |
| 34 | $subgroup->users()->update(['company_group_id' => null]); |
| 35 | |
| 36 | $user_ids = $subgroup->users()->pluck('_id')->toArray(); |
| 37 | $this->adminUsersService->moveUsersToGroup($user_ids, null); |
| 38 | |
| 39 | $subgroup->delete(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | $group->delete(); |
| 44 | |
| 45 | // $this->instancyRepository->deleteGroup($group->instancy_id); instancy's api is not working |
| 46 | |
| 47 | return [ |
| 48 | 'success' => true, |
| 49 | 'message' => "Group deleted successfully", |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | public function updateGroup(CompanyGroup $group, string $name, string $company_id) { |
| 54 | $groupExistsInCompany = CompanyGroup::where("name", $name) |
| 55 | ->where("company_id", $company_id) |
| 56 | ->whereNot("_id", $group->id) |
| 57 | ->exists(); |
| 58 | |
| 59 | if ($groupExistsInCompany) { |
| 60 | return [ |
| 61 | "success" => false, |
| 62 | "message" => "This group already exists in the company. Choose another" |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | $group->name = $name; |
| 67 | $group->save(); |
| 68 | |
| 69 | $this->updateInstancyGroup($group, $company_id, $group->parent); |
| 70 | |
| 71 | return [ |
| 72 | "success" => true, |
| 73 | "message" => "Group updated successfully" |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | public function createGroup(string $name, string $company_id, ?CompanyGroup $parent = null) { |
| 78 | $group = CompanyGroup::where("name", $name) |
| 79 | ->where("company_id", $company_id) |
| 80 | ->first(); |
| 81 | |
| 82 | if ($group) { |
| 83 | return response()->json([ |
| 84 | "success" => "false", |
| 85 | "message" => "This group already exists in the company. Choose another" |
| 86 | ], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 87 | } |
| 88 | |
| 89 | $group = new CompanyGroup([ |
| 90 | 'name' => $name |
| 91 | ]); |
| 92 | $group->instancy_id = $this->createInstancyGroup($name, $company_id, $parent); |
| 93 | if ($parent) { |
| 94 | $group->parent_id = $parent->id; |
| 95 | } |
| 96 | $group->company_id = $company_id; |
| 97 | $group->save(); |
| 98 | |
| 99 | return $group; |
| 100 | } |
| 101 | |
| 102 | private function updateInstancyGroup(CompanyGroup $group, string $company_id, ?CompanyGroup $parent = null) { |
| 103 | $newInstancyGroup = new stdClass(); |
| 104 | $newInstancyGroup->groupId = $group->instancy_id; |
| 105 | $newInstancyGroup->name = $group->name; |
| 106 | |
| 107 | if ($parent) { |
| 108 | $newInstancyGroup->parentId = $parent->instancy_id; |
| 109 | } else { |
| 110 | $company = Company::find($company_id); |
| 111 | $newInstancyGroup->parentId = $company->instancy_id; |
| 112 | } |
| 113 | |
| 114 | return $this->instancyRepository->updateGroup($newInstancyGroup); |
| 115 | } |
| 116 | |
| 117 | private function createInstancyGroup(string $name, string $company_id, ?CompanyGroup $parent = null) { |
| 118 | $newInstancyGroup = new stdClass(); |
| 119 | $newInstancyGroup->name = $name; |
| 120 | |
| 121 | if ($parent) { |
| 122 | $newInstancyGroup->parentId = $parent->instancy_id; |
| 123 | } else { |
| 124 | $company = Company::find($company_id); |
| 125 | $newInstancyGroup->parentId = $company->instancy_id; |
| 126 | } |
| 127 | |
| 128 | return $this->instancyRepository->createGroup($newInstancyGroup); |
| 129 | } |
| 130 | } |