Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions app/Http/Controllers/Api/DepartmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ public function index(Request $request) : JsonResponse | array
$allowed_columns = ['id', 'name', 'image', 'users_count', 'notes'];

$departments = Department::select(
'departments.id',
'departments.name',
'departments.phone',
'departments.fax',
'departments.location_id',
'departments.company_id',
'departments.manager_id',
'departments.created_at',
'departments.updated_at',
'departments.image',
'departments.notes',
)->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
[
'departments.id',
'departments.name',
'departments.phone',
'departments.fax',
'departments.location_id',
'departments.company_id',
'departments.manager_id',
'departments.created_at',
'departments.updated_at',
'departments.image',
'departments.notes'
])->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Transformers/DepartmentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function transformDepartment(Department $department = null)
'id' => (int) $department->location->id,
'name' => e($department->location->name),
] : null,
'users_count' => e($department->users_count),
'users_count' => (int) ($department->users_count),
'notes' => Helper::parseEscapedMarkedownInline($department->notes),
'created_at' => Helper::getFormattedDateObject($department->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($department->updated_at, 'datetime'),
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Department.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Department extends SnipeModel
];

protected $rules = [
'name' => 'required|max:255|is_unique_department',
'name' => 'required|max:255|is_unique_across_company_and_location:departments,name',
'location_id' => 'numeric|nullable|exists:locations,id',
'company_id' => 'numeric|nullable|exists:companies,id',
'manager_id' => 'numeric|nullable|exists:users,id',
Expand Down
53 changes: 24 additions & 29 deletions app/Providers/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,41 +283,36 @@ public function boot()
}
});

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

if (
array_key_exists('location_id', $data) && $data['location_id'] !== null &&
array_key_exists('company_id', $data) && $data['company_id'] !== null
) {
//for updating existing departments
if(array_key_exists('id', $data) && $data['id'] !== null){
$count = Department::where('name', $data['name'])
->where('location_id', $data['location_id'])
->where('company_id', $data['company_id'])
->whereNotNull('company_id')
->whereNotNull('location_id')
->where('id', '!=', $data['id'])
->count('name');

return $count < 1;
}else // for entering in new departments
{
$count = Department::where('name', $data['name'])
->where('location_id', $data['location_id'])
->where('company_id', $data['company_id'])
->whereNotNull('company_id')
->whereNotNull('location_id')
->count('name');
$count = DB::table($table)->select($attribute)
->where($attribute, $value)
->whereNull('deleted_at');

return $count < 1;
if (array_key_exists('id', $data) && $data['id'] !== null) {
$count = $count->where('id', '!=', $data['id']);
}
}
else {
return true;
}

if (array_key_exists('location_id', $data) && $data['location_id'] !== null) {
$count = $count->where('location_id', $data['location_id']);
}

if (array_key_exists('company_id', $data) && $data['company_id'] !== null) {
$count = $count->where('company_id', $data['company_id']);
}

$count = $count->count('name');
return $count < 1;

});


Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) {
return !is_array($value);
});
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en-US/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
'ulid' => 'The :attribute field must be a valid ULID.',
'uuid' => 'The :attribute field must be a valid UUID.',
'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.',

'is_unique_across_company_and_location' => 'The :attribute must be unique within the selected company and location.',

/*
|--------------------------------------------------------------------------
Expand Down
Loading