Skip to content

Commit b7a6706

Browse files
authored
Merge pull request #18150 from grokability/#18148-dept-api-request-user-count
Fixed #18148 and #17451 - return int for user_count, fixed validation
2 parents e906d25 + ddb031f commit b7a6706

File tree

5 files changed

+42
-46
lines changed

5 files changed

+42
-46
lines changed

app/Http/Controllers/Api/DepartmentsController.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ public function index(Request $request) : JsonResponse | array
2727
$allowed_columns = ['id', 'name', 'image', 'users_count', 'notes'];
2828

2929
$departments = Department::select(
30-
'departments.id',
31-
'departments.name',
32-
'departments.phone',
33-
'departments.fax',
34-
'departments.location_id',
35-
'departments.company_id',
36-
'departments.manager_id',
37-
'departments.created_at',
38-
'departments.updated_at',
39-
'departments.image',
40-
'departments.notes',
41-
)->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
30+
[
31+
'departments.id',
32+
'departments.name',
33+
'departments.phone',
34+
'departments.fax',
35+
'departments.location_id',
36+
'departments.company_id',
37+
'departments.manager_id',
38+
'departments.created_at',
39+
'departments.updated_at',
40+
'departments.image',
41+
'departments.notes'
42+
])->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
4243

4344
if ($request->filled('search')) {
4445
$departments = $departments->TextSearch($request->input('search'));
@@ -105,7 +106,7 @@ public function store(StoreDepartmentRequest $request): JsonResponse
105106
$department->manager_id = ($request->filled('manager_id') ? $request->input('manager_id') : null);
106107

107108
if ($department->save()) {
108-
return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/departments/message.create.success')));
109+
return response()->json(Helper::formatStandardApiResponse('success', (new DepartmentsTransformer)->transformDepartment($department), trans('admin/departments/message.create.success')));
109110
}
110111
return response()->json(Helper::formatStandardApiResponse('error', null, $department->getErrors()));
111112

@@ -121,7 +122,7 @@ public function store(StoreDepartmentRequest $request): JsonResponse
121122
public function show($id) : array
122123
{
123124
$this->authorize('view', Department::class);
124-
$department = Department::findOrFail($id);
125+
$department = Department::withCount('users as users_count')->findOrFail($id);
125126
return (new DepartmentsTransformer)->transformDepartment($department);
126127
}
127128

app/Http/Transformers/DepartmentsTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function transformDepartment(Department $department = null)
4343
'id' => (int) $department->location->id,
4444
'name' => e($department->location->name),
4545
] : null,
46-
'users_count' => e($department->users_count),
46+
'users_count' => (int) ($department->users_count),
4747
'notes' => Helper::parseEscapedMarkedownInline($department->notes),
4848
'created_at' => Helper::getFormattedDateObject($department->created_at, 'datetime'),
4949
'updated_at' => Helper::getFormattedDateObject($department->updated_at, 'datetime'),

app/Models/Department.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Department extends SnipeModel
3131
];
3232

3333
protected $rules = [
34-
'name' => 'required|max:255|is_unique_department',
34+
'name' => 'required|max:255|is_unique_across_company_and_location:departments,name',
3535
'location_id' => 'numeric|nullable|exists:locations,id',
3636
'company_id' => 'numeric|nullable|exists:companies,id',
3737
'manager_id' => 'numeric|nullable|exists:users,id',

app/Providers/ValidationServiceProvider.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -283,41 +283,36 @@ public function boot()
283283
}
284284
});
285285

286-
Validator::extend('is_unique_department', function ($attribute, $value, $parameters, $validator) {
286+
/**
287+
* Check that the 'name' field is unique in the table while within both company_id and location_id
288+
* This is only used by Departments right now, but could be used elsewhere in the future.
289+
*/
290+
Validator::extend('is_unique_across_company_and_location', function ($attribute, $value, $parameters, $validator) {
287291
$data = $validator->getData();
292+
$table = array_get($parameters, 0);
288293

289-
if (
290-
array_key_exists('location_id', $data) && $data['location_id'] !== null &&
291-
array_key_exists('company_id', $data) && $data['company_id'] !== null
292-
) {
293-
//for updating existing departments
294-
if(array_key_exists('id', $data) && $data['id'] !== null){
295-
$count = Department::where('name', $data['name'])
296-
->where('location_id', $data['location_id'])
297-
->where('company_id', $data['company_id'])
298-
->whereNotNull('company_id')
299-
->whereNotNull('location_id')
300-
->where('id', '!=', $data['id'])
301-
->count('name');
302-
303-
return $count < 1;
304-
}else // for entering in new departments
305-
{
306-
$count = Department::where('name', $data['name'])
307-
->where('location_id', $data['location_id'])
308-
->where('company_id', $data['company_id'])
309-
->whereNotNull('company_id')
310-
->whereNotNull('location_id')
311-
->count('name');
294+
$count = DB::table($table)->select($attribute)
295+
->where($attribute, $value)
296+
->whereNull('deleted_at');
312297

313-
return $count < 1;
298+
if (array_key_exists('id', $data) && $data['id'] !== null) {
299+
$count = $count->where('id', '!=', $data['id']);
314300
}
315-
}
316-
else {
317-
return true;
318-
}
301+
302+
if (array_key_exists('location_id', $data) && $data['location_id'] !== null) {
303+
$count = $count->where('location_id', $data['location_id']);
304+
}
305+
306+
if (array_key_exists('company_id', $data) && $data['company_id'] !== null) {
307+
$count = $count->where('company_id', $data['company_id']);
308+
}
309+
310+
$count = $count->count('name');
311+
return $count < 1;
312+
319313
});
320314

315+
321316
Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) {
322317
return !is_array($value);
323318
});

resources/lang/en-US/validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
'ulid' => 'The :attribute field must be a valid ULID.',
175175
'uuid' => 'The :attribute field must be a valid UUID.',
176176
'fmcs_location' => 'Full multiple company support and location scoping is enabled in the Admin Settings, and the selected location and selected company are not compatible.',
177-
177+
'is_unique_across_company_and_location' => 'The :attribute must be unique within the selected company and location.',
178178

179179
/*
180180
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)