Skip to content

Commit 0f2236b

Browse files
[CUR-1718] Resolved issue with elasticsearch response for non public projects (ActiveLearningStudio#474)
* WORK IN PROGRESS * Updated the search logic to consider private, protected, global and public visibility. * Refactored "OrganizationVisibilityTypeSeeder" to update display_name.
1 parent 3d0f815 commit 0f2236b

File tree

9 files changed

+187
-72
lines changed

9 files changed

+187
-72
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ LRS_JOB_ROW_LIMIT=50
9090

9191
PUBLIC_ORGANIZATION_VISIBILITY_TYPE_ID=4
9292
GLOBAL_ORGANIZATION_VISIBILITY_TYPE_ID=3
93+
PROTECTED_ORGANIZATION_VISIBILITY_TYPE_ID=2
9394
PRIVATE_ORGANIZATION_VISIBILITY_TYPE_ID=1
9495
INDEXING_APPROVED=3
9596
INDEXING_OPTIONS=null,1,2,3

app/Http/Controllers/Api/V1/SearchController.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,16 @@ public function advance(SearchRequest $searchRequest)
118118
$this->authorize('advanceSearch', $organization);
119119

120120
$data['organizationIds'] = [$data['organization_id']];
121+
$data['orgObj'] = $organization;
121122

122-
if (!auth()->user()->hasPermissionTo('organization:view', $organization)) {
123-
$data['organizationVisibilityTypeIds'] = [null, config('constants.public-organization-visibility-type-id')];
124-
$data['indexing'] = [config('constants.indexing-approved')];
125-
} elseif (isset($data['query'])) {
126-
$data['organizationVisibilityTypeIds'] = [null, config('constants.public-organization-visibility-type-id')];
123+
if ($data['searchType'] === 'showcase_projects') {
127124
$data['indexing'] = [config('constants.indexing-approved')];
125+
} elseif ($data['searchType'] === 'org_projects') {
126+
if (!auth()->user()->hasPermissionTo('organization:view', $organization)) {
127+
$data['searchType'] = 'org_projects_non_admin';
128+
} else {
129+
$data['searchType'] = 'org_projects_admin';
130+
}
128131
}
129132

130133
$results = $this->activityRepository->advanceSearchForm($data);

app/Http/Requests/V1/SearchRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function authorize()
2424
public function rules()
2525
{
2626
return [
27+
'searchType' => 'required|in:my_projects,showcase_projects,org_projects',
2728
'query' => 'string|max:255',
2829
'organization_id' => 'required|integer|exists:App\Models\Organization,id',
2930
'negativeQuery' => 'string|max:255',

app/Models/QueryBuilders/SearchFormQueryBuilder.php

+92-51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
final class SearchFormQueryBuilder implements QueryBuilderInterface
99
{
10+
/**
11+
* @var string
12+
*/
13+
private $organizationParentChildrenIds;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $searchType;
19+
1020
/**
1121
* @var string
1222
*/
@@ -72,6 +82,18 @@ final class SearchFormQueryBuilder implements QueryBuilderInterface
7282
*/
7383
private $indexing;
7484

85+
public function organizationParentChildrenIds(array $organizationParentChildrenIds): self
86+
{
87+
$this->organizationParentChildrenIds = $organizationParentChildrenIds;
88+
return $this;
89+
}
90+
91+
public function searchType(string $searchType): self
92+
{
93+
$this->searchType = $searchType;
94+
return $this;
95+
}
96+
7597
public function query(string $query): self
7698
{
7799
$this->query = $query;
@@ -183,47 +205,89 @@ public function buildQuery(): array
183205
];
184206
}
185207

208+
if (!empty($this->searchType)) {
209+
if (
210+
$this->searchType === 'my_projects'
211+
|| $this->searchType === 'org_projects_admin'
212+
|| $this->searchType === 'org_projects_non_admin'
213+
) {
214+
if (!empty($this->organizationIds)) {
215+
$queries[] = [
216+
'terms' => [
217+
'organization_id' => $this->organizationIds
218+
]
219+
];
220+
}
186221

187-
if (empty($this->organizationIds) && !empty($this->organizationVisibilityTypeIds)) {
188-
if (in_array(null, $this->organizationVisibilityTypeIds, true)) {
189-
$queries[] = [
190-
'bool' => [
191-
'should' => [
192-
[
193-
'terms' => [
194-
'organization_visibility_type_id' => array_values(array_filter($this->organizationVisibilityTypeIds))
195-
]
196-
],
197-
[
198-
'bool' => [
199-
'must_not' => [
200-
'exists' => [
201-
'field' => 'organization_visibility_type_id'
202-
]
222+
if ($this->searchType === 'org_projects_non_admin') {
223+
$queries[] = [
224+
'bool' => [
225+
'must_not' => [
226+
[
227+
'terms' => [
228+
'organization_visibility_type_id' => [config('constants.private-organization-visibility-type-id')]
203229
]
204230
]
205231
]
206232
]
233+
];
234+
}
235+
} elseif ($this->searchType === 'showcase_projects') {
236+
// Get all public items
237+
$organizationIdsShouldQueries[] = [
238+
'terms' => [
239+
'organization_visibility_type_id' => [config('constants.public-organization-visibility-type-id')]
207240
]
208241
];
209-
} else {
210-
$queries[] = [
242+
243+
// Get all global items
244+
$globalOrganizationIdsQueries[] = [
211245
'terms' => [
212-
'organization_visibility_type_id' => $this->organizationVisibilityTypeIds
246+
'organization_id' => $this->organizationParentChildrenIds
213247
]
214248
];
215-
}
216-
} elseif (!empty($this->organizationIds)) {
217249

218-
$organizationIdsQueries[] = [
219-
'terms' => [
220-
'organization_id' => $this->organizationIds
221-
]
222-
];
250+
$globalOrganizationIdsQueries[] = [
251+
'terms' => [
252+
'organization_visibility_type_id' => [config('constants.global-organization-visibility-type-id')]
253+
]
254+
];
255+
256+
$organizationIdsShouldQueries[] = [
257+
'bool' => [
258+
'must' => $globalOrganizationIdsQueries
259+
]
260+
];
261+
262+
// Get all protected items
263+
$protectedOrganizationIdsQueries[] = [
264+
'terms' => [
265+
'organization_id' => $this->organizationIds
266+
]
267+
];
268+
269+
$protectedOrganizationIdsQueries[] = [
270+
'terms' => [
271+
'organization_visibility_type_id' => [config('constants.protected-organization-visibility-type-id')]
272+
]
273+
];
274+
275+
$organizationIdsShouldQueries[] = [
276+
'bool' => [
277+
'must' => $protectedOrganizationIdsQueries
278+
]
279+
];
223280

281+
$queries[] = [
282+
'bool' => [
283+
'should' => $organizationIdsShouldQueries
284+
]
285+
];
286+
}
287+
} else {
224288
if (!empty($this->organizationVisibilityTypeIds)) {
225289
if (in_array(null, $this->organizationVisibilityTypeIds, true)) {
226-
$organizationIdsQueries[] = [
290+
$queries[] = [
227291
'bool' => [
228292
'should' => [
229293
[
@@ -244,36 +308,13 @@ public function buildQuery(): array
244308
]
245309
];
246310
} else {
247-
$organizationIdsQueries[] = [
311+
$queries[] = [
248312
'terms' => [
249313
'organization_visibility_type_id' => $this->organizationVisibilityTypeIds
250314
]
251315
];
252316
}
253-
254-
$globalPublicVisibilityTypeIds = [config('constants.global-organization-visibility-type-id'), config('constants.public-organization-visibility-type-id')];
255-
$commonVisibilityTypeIds = array_values(array_intersect($this->organizationVisibilityTypeIds, $globalPublicVisibilityTypeIds));
256-
257-
if (!empty($commonVisibilityTypeIds)) {
258-
$organizationIdsShouldQueries[] = [
259-
'terms' => [
260-
'organization_visibility_type_id' => $commonVisibilityTypeIds
261-
]
262-
];
263-
}
264317
}
265-
266-
$organizationIdsShouldQueries[] = [
267-
'bool' => [
268-
'must' => $organizationIdsQueries
269-
]
270-
];
271-
272-
$queries[] = [
273-
'bool' => [
274-
'should' => $organizationIdsShouldQueries
275-
]
276-
];
277318
}
278319

279320
if (!empty($this->subjectIds)) {

app/Repositories/Activity/ActivityRepository.php

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Illuminate\Support\Collection;
1919
use Illuminate\Support\Facades\Storage;
2020
use Illuminate\Support\Facades\Log;
21+
use App\Repositories\Organization\OrganizationRepositoryInterface;
2122

2223
class ActivityRepository extends BaseRepository implements ActivityRepositoryInterface
2324
{
@@ -129,6 +130,12 @@ public function advanceSearchForm($data)
129130

130131
$counts = [];
131132
$projectIds = [];
133+
$organizationParentChildrenIds = [];
134+
135+
if (isset($data['searchType']) && $data['searchType'] === 'showcase_projects') {
136+
$organization = $data['orgObj'];
137+
$organizationParentChildrenIds = resolve(OrganizationRepositoryInterface::class)->getParentChildrenOrganizationIds($organization);
138+
}
132139

133140
if (isset($data['userIds']) && !empty($data['userIds'])) {
134141
$userIds = $data['userIds'];
@@ -159,6 +166,8 @@ public function advanceSearchForm($data)
159166
}
160167

161168
$searchResultQuery = $this->model->searchForm()
169+
->searchType(Arr::get($data, 'searchType', 0))
170+
->organizationParentChildrenIds($organizationParentChildrenIds)
162171
->query(Arr::get($data, 'query', 0))
163172
->join(Project::class, Playlist::class)
164173
->aggregate('count_by_index', [

app/Repositories/Organization/OrganizationRepository.php

+37
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,43 @@ public function getSuborganizationIds($organization, $organizationIds = [])
9191
return $organizationIds;
9292
}
9393

94+
/**
95+
* Get ids for parent organizations
96+
*
97+
* @param Organization $organization
98+
* @param array $organizationIds
99+
* @return array $ids
100+
*/
101+
public function getParentOrganizationIds($organization, $organizationIds = [])
102+
{
103+
$organizationIds[] = $organization->id;
104+
105+
if ($organization->parent) {
106+
foreach ($organization->parent as $parent) {
107+
$organizationIds = $this->getParentOrganizationIds($parent, $organizationIds);
108+
}
109+
}
110+
111+
return $organizationIds;
112+
}
113+
114+
/**
115+
* Get ids for parent organizations
116+
*
117+
* @param Organization $organization
118+
* @param array $organizationIds
119+
* @return array $ids
120+
*/
121+
public function getParentChildrenOrganizationIds($organization)
122+
{
123+
$parentIds = $this->getParentOrganizationIds($organization);
124+
$childrenIds = $this->getSuborganizationIds($organization);
125+
126+
$parentChildrenOrganizationIds = $parentIds + $childrenIds;
127+
128+
return $parentChildrenOrganizationIds;
129+
}
130+
94131
/**
95132
* To create a suborganization
96133
*

app/Repositories/Organization/OrganizationRepositoryInterface.php

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ public function fetchSuborganizations($data, $organization);
2626
*/
2727
public function getSuborganizationIds($organization, $organizationIds = []);
2828

29+
/**
30+
* Get ids for parent organizations
31+
*
32+
* @param Organization $organization
33+
* @param array $organizationIds
34+
* @return array $ids
35+
*/
36+
public function getParentOrganizationIds($organization, $organizationIds = []);
37+
38+
/**
39+
* Get ids for parent organizations
40+
*
41+
* @param Organization $organization
42+
* @param array $organizationIds
43+
* @return array $ids
44+
*/
45+
public function getParentChildrenOrganizationIds($organization);
46+
2947
/**
3048
* To create a suborganization
3149
*

config/constants.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
],
1010
'public-organization-visibility-type-id' => env('PUBLIC_ORGANIZATION_VISIBILITY_TYPE_ID', 4),
1111
'global-organization-visibility-type-id' => env('GLOBAL_ORGANIZATION_VISIBILITY_TYPE_ID', 3),
12+
'protected-organization-visibility-type-id' => env('PROTECTED_ORGANIZATION_VISIBILITY_TYPE_ID', 2),
1213
'private-organization-visibility-type-id' => env('PRIVATE_ORGANIZATION_VISIBILITY_TYPE_ID', 1),
1314
'indexing-approved' => env('INDEXING_APPROVED', 3),
1415
'indexing-options' => env('INDEXING_OPTIONS', 'null,1,2,3'),

database/seeds/OrganizationVisibilityTypeSeeder.php

+20-16
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ class OrganizationVisibilityTypeSeeder extends Seeder
1212
*/
1313
public function run()
1414
{
15-
DB::table('organization_visibility_types')->insert([
16-
'name' => 'private',
17-
'display_name' => 'Private'
18-
]);
15+
DB::table('organization_visibility_types')
16+
->updateOrInsert(
17+
['name' => 'private'],
18+
['display_name' => 'Private']
19+
);
1920

20-
DB::table('organization_visibility_types')->insert([
21-
'name' => 'protected',
22-
'display_name' => 'Protected'
23-
]);
21+
DB::table('organization_visibility_types')
22+
->updateOrInsert(
23+
['name' => 'protected'],
24+
['display_name' => 'My Org']
25+
);
2426

25-
DB::table('organization_visibility_types')->insert([
26-
'name' => 'global',
27-
'display_name' => 'Global'
28-
]);
27+
DB::table('organization_visibility_types')
28+
->updateOrInsert(
29+
['name' => 'global'],
30+
['display_name' => 'Parent/Child Org']
31+
);
2932

30-
DB::table('organization_visibility_types')->insert([
31-
'name' => 'public',
32-
'display_name' => 'Public'
33-
]);
33+
DB::table('organization_visibility_types')
34+
->updateOrInsert(
35+
['name' => 'public'],
36+
['display_name' => 'Instance Wide']
37+
);
3438
}
3539
}

0 commit comments

Comments
 (0)