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