Skip to content

Commit dc0d963

Browse files
authored
Merge pull request #112 from imbus/SIT-190-bugfix-failed-to-apply
added a method to explicit return the name of the models
2 parents cc9d999 + 19b5236 commit dc0d963

File tree

2 files changed

+47
-58
lines changed

2 files changed

+47
-58
lines changed

app/Http/Controllers/Api/PredefinedFilterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function index(Request $request) : JsonResponse | array
6161

6262
public function show(int $id)
6363
{
64-
$filter = $this->service->getFilterWithOptionalPermissionsById($id);
64+
$filter = $this->service->getFilterWithIdAndNameValues($id);
6565

6666
if (!$filter) {
6767
return response()->json(['message' => trans('admin/predefinedFilters/message.does_not_exist')], 404);

app/Services/PredefinedFilterService.php

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Throwable;
1515
use App\Models\PredefinedFilter;
1616
use App\Services\FilterService\FilterService;
17+
use App\Services\PredefinedFilterPermissionService;
1718
use Illuminate\Http\Request;
1819
use Illuminate\Pagination\LengthAwarePaginator;
1920
use Illuminate\Support\Collection;
@@ -60,94 +61,82 @@ public function getFilterWithOptionalPermissionsById(int $id, bool $include_pred
6061
$permissions = $this->predefinedFilterPermissionService->getPermissionsByPredefinedFilterId($id);
6162
$predefinedFilter['permissions'] = $permissions;
6263
}
63-
64+
65+
return $predefinedFilter;
66+
}
67+
68+
public function getFilterWithIdAndNameValues(int $id)
69+
{
70+
$predefinedFilter = $this->getFilterWithOptionalPermissionsById($id);
71+
6472
if (!$predefinedFilter){
6573
return null;
6674
}
6775

76+
$fieldsToLookup = [
77+
'company',
78+
'model',
79+
'category',
80+
'status_label',
81+
'location',
82+
'rtd_location',
83+
'manufacturer',
84+
'supplier'
85+
];
86+
6887
$filters = $predefinedFilter->filter_data;
69-
88+
7089
foreach ($filters as &$filter) {
90+
91+
$model = null;
7192

72-
93+
if (isset($filter['field']) && !in_array($filter['field'], $fieldsToLookup)){
94+
continue;
95+
}
96+
7397
if (!empty($filter['value']) && is_array($filter['value']) && is_int($filter['value'][0])) {
98+
7499
$values =[];
100+
75101
foreach ($filter['value'] as $valueId){
76102
switch ($filter['field']) {
77103
case 'company':
78-
$company = Company::find($valueId);
79-
if ($company) {
80-
$values[] = [
81-
'id' => $company->id,
82-
'name' => $company->name,
83-
];
84-
}
104+
$model = Company::find($valueId);
85105
break;
86106
case 'model':
87107
$model = AssetModel::find($valueId);
88-
if ($model) {
89-
$values[] = [
90-
'id' => $model->id,
91-
'name' => $model->name,
92-
];
93-
}
94108
break;
95109
case 'category':
96-
$category = Category::find($valueId);
97-
if ($category) {
98-
$values[] = [
99-
'id' => $category->id,
100-
'name' => $category->name,
101-
];
102-
}
110+
$model = Category::find($valueId);
103111
break;
104-
case 'statuslabel':
105-
$status = Statuslabel::find($valueId);
106-
if ($status) {
107-
$values[] = [
108-
'id' => $status->id,
109-
'name' => $status->name,
110-
];
111-
}
112+
case 'status_label':
113+
$model = Statuslabel::find($valueId);
112114
break;
113115
case 'location':
114-
case 'default_location':
115-
$location = Location::find($valueId);
116-
if ($location) {
117-
$values[] = [
118-
'id' => $location->id,
119-
'name' => $location->name,
120-
];
121-
}
116+
case 'rtd_location':
117+
$model = Location::find($valueId);
122118
break;
123119
case 'manufacturer':
124-
$manufacturer = Manufacturer::find($valueId);
125-
if ($manufacturer) {
126-
$values[] = [
127-
'id' => $manufacturer->id,
128-
'name' => $manufacturer->name,
129-
];
130-
}
120+
$model = Manufacturer::find($valueId);
131121
break;
132122
case 'supplier':
133-
$supplier = Supplier::find($valueId);
134-
if ($supplier) {
135-
$values[] = [
136-
'id' => $supplier->id,
137-
'name' => $supplier->name,
138-
];
139-
}
123+
$model = Supplier::find($valueId);
140124
break;
141125
default:
142126
break;
143127
}
128+
if ($model){
129+
$values[] = [
130+
'id' => $model->id,
131+
'name' => $model->name
132+
];
133+
}
134+
$filter['value'] = $values;
144135
}
145-
146-
$filter['value'] = $values;
147-
$predefinedFilter->filter_data = $filters;
148-
136+
149137
}
150138
}
139+
$predefinedFilter->filter_data = $filters;
151140
return $predefinedFilter;
152141
}
153142

0 commit comments

Comments
 (0)