From 0abb993600df315b946af12ab64d1fc675bf6ef1 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 6 Apr 2023 22:02:57 -0700 Subject: [PATCH 1/6] General cleanup and standardization for component API Signed-off-by: snipe --- .../Controllers/Api/ComponentsController.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 473fbf1482a8..7dc168726fbd 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -12,6 +12,7 @@ use App\Events\CheckoutableCheckedIn; use App\Events\ComponentCheckedIn; use App\Models\Asset; +use Illuminate\Support\Facades\Validator; class ComponentsController extends Controller { @@ -225,12 +226,27 @@ public function getAssets(Request $request, $id) public function checkout(Request $request, $componentId) { // Check if the component exists - if (is_null($component = Component::find($componentId))) { + if (!$component = Component::find($componentId)) { return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.does_not_exist'))); } $this->authorize('checkout', $component); + $validator = Validator::make($request->all(), [ + 'asset_id' => 'required|exists:assets,id', + 'assigned_qty' => "required|numeric|min:1|digits_between:1,".$component->numRemaining(), + ]); + + if ($validator->fails()) { + return response()->json(Helper::formatStandardApiResponse('error', $validator->errors())); + + } + + // Make sure there is at least one available to checkout + if ($component->numRemaining() <= $request->get('assigned_qty')) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.checkout.unavailable', ['remaining' => $component->numRemaining(), 'requested' => $request->get('assigned_qty')]))); + + } if ($component->numRemaining() >= $request->get('assigned_qty')) { @@ -255,7 +271,7 @@ public function checkout(Request $request, $componentId) return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkout.success'))); } - return response()->json(Helper::formatStandardApiResponse('error', null, 'Not enough components remaining: '.$component->numRemaining().' remaining, '.$request->get('assigned_qty').' requested.')); + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.checkout.unavailable', ['remaining' => $component->numRemaining(), 'requested' => $request->get('assigned_qty')]))); } /** From 43ad0f58b5abdc26cd4807bf03821c05de2deb32 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 6 Apr 2023 22:03:15 -0700 Subject: [PATCH 2/6] SLightly cleaner layout - still needs work Signed-off-by: snipe --- resources/views/components/checkout.blade.php | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/resources/views/components/checkout.blade.php b/resources/views/components/checkout.blade.php index e27670658c80..a104a81e1d7f 100644 --- a/resources/views/components/checkout.blade.php +++ b/resources/views/components/checkout.blade.php @@ -10,7 +10,7 @@ @section('content')
-
+
{{ csrf_field() }} @@ -25,30 +25,26 @@ @endif
- @if ($component->name) - -
- -
-

{{ $component->name }}

-
-
- @endif - @include ('partials.forms.edit.asset-select', ['translated_name' => trans('general.select_asset'), 'fieldname' => 'asset_id'])
- -
- - {!! $errors->first('assigned_qty', '
') !!} + +
+
+ @if ($errors->first('assigned_qty')) +
+ {!! $errors->first('assigned_qty', '') !!} +
+ @endif
+ -
+
From 358b0548fa0e786ccf7b18aa0a05c1c46463be74 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 6 Apr 2023 22:03:26 -0700 Subject: [PATCH 3/6] Added string for translations Signed-off-by: snipe --- resources/lang/en/admin/components/message.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/lang/en/admin/components/message.php b/resources/lang/en/admin/components/message.php index 1d13970f23ab..0a7dd8d9540e 100644 --- a/resources/lang/en/admin/components/message.php +++ b/resources/lang/en/admin/components/message.php @@ -23,7 +23,8 @@ 'checkout' => array( 'error' => 'Component was not checked out, please try again', 'success' => 'Component checked out successfully.', - 'user_does_not_exist' => 'That user is invalid. Please try again.' + 'user_does_not_exist' => 'That user is invalid. Please try again.', + 'unavailable' => 'Not enough components remaining: :remaining remaining, :requested requested ', ), 'checkin' => array( From 4c88c751ef2dd1f9546c5f3d42c229d93d3c705e Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 6 Apr 2023 22:03:40 -0700 Subject: [PATCH 4/6] Code cleanup, better validation Signed-off-by: snipe --- .../ComponentCheckoutController.php | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php index b2be07c66180..998ec996c9bb 100644 --- a/app/Http/Controllers/Components/ComponentCheckoutController.php +++ b/app/Http/Controllers/Components/ComponentCheckoutController.php @@ -33,6 +33,11 @@ public function create($componentId) } $this->authorize('checkout', $component); + // Make sure there is at least one available to checkout + if ($component->numRemaining() <= 0){ + return redirect()->route('components.index')->with('error', trans('admin/components/message.checkout.unavailable')); + } + return view('components/checkout', compact('component')); } @@ -50,7 +55,7 @@ public function create($componentId) public function store(Request $request, $componentId) { // Check if the component exists - if (is_null($component = Component::find($componentId))) { + if (!$component = Component::find($componentId)) { // Redirect to the component management page with error return redirect()->route('components.index')->with('error', trans('admin/components/message.not_found')); } @@ -58,9 +63,15 @@ public function store(Request $request, $componentId) $this->authorize('checkout', $component); $max_to_checkout = $component->numRemaining(); + + // Make sure there is at least one available to checkout + if ($max_to_checkout <= $request->get('assigned_qty')) { + return redirect()->back()->withInput()->with('error', trans('admin/components/message.checkout.unavailable', ['remaining' => $max_to_checkout, 'requested' => $request->get('assigned_qty')])); + } + $validator = Validator::make($request->all(), [ - 'asset_id' => 'required', - 'assigned_qty' => "required|numeric|between:1,$max_to_checkout", + 'asset_id' => 'required|exists:assets,id', + 'assigned_qty' => "required|numeric|min:1|digits_between:1,$max_to_checkout", ]); if ($validator->fails()) { @@ -70,13 +81,10 @@ public function store(Request $request, $componentId) } $admin_user = Auth::user(); - $asset_id = e($request->input('asset_id')); + $asset_id = $request->input('asset_id'); // Check if the user exists - if (is_null($asset = Asset::find($asset_id))) { - // Redirect to the component management page with error - return redirect()->route('components.index')->with('error', trans('admin/components/message.asset_does_not_exist')); - } + $asset = Asset::find($asset_id); // Update the component data $component->asset_id = $asset_id; From cb76815e8b63527bef8cce3f2e8324c31d2da54c Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 7 Apr 2023 09:42:31 -0700 Subject: [PATCH 5/6] =?UTF-8?q?Removed=20if=20check=20on=20assets,=20since?= =?UTF-8?q?=20that=E2=80=99s=20already=20handled=20via=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: snipe --- app/Http/Controllers/Api/ComponentsController.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 7dc168726fbd..bf281d6b2b44 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -245,16 +245,11 @@ public function checkout(Request $request, $componentId) // Make sure there is at least one available to checkout if ($component->numRemaining() <= $request->get('assigned_qty')) { return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.checkout.unavailable', ['remaining' => $component->numRemaining(), 'requested' => $request->get('assigned_qty')]))); - } if ($component->numRemaining() >= $request->get('assigned_qty')) { - if (!$asset = Asset::find($request->input('assigned_to'))) { - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist'))); - } - - // Update the accessory data + $asset = Asset::find($request->input('assigned_to')); $component->assigned_to = $request->input('assigned_to'); $component->assets()->attach($component->id, [ From baf267e2e95239ee5a8d9af085a8c5149c75a245 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 7 Apr 2023 09:45:16 -0700 Subject: [PATCH 6/6] Small cleanups Signed-off-by: snipe --- .../Components/ComponentCheckoutController.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php index 998ec996c9bb..c3d391c1fd4a 100644 --- a/app/Http/Controllers/Components/ComponentCheckoutController.php +++ b/app/Http/Controllers/Components/ComponentCheckoutController.php @@ -80,21 +80,17 @@ public function store(Request $request, $componentId) ->withInput(); } - $admin_user = Auth::user(); - $asset_id = $request->input('asset_id'); - // Check if the user exists - $asset = Asset::find($asset_id); + $asset = Asset::find($request->input('asset_id')); // Update the component data - $component->asset_id = $asset_id; - + $component->asset_id = $request->input('asset_id'); $component->assets()->attach($component->id, [ 'component_id' => $component->id, - 'user_id' => $admin_user->id, + 'user_id' => Auth::user(), 'created_at' => date('Y-m-d H:i:s'), 'assigned_qty' => $request->input('assigned_qty'), - 'asset_id' => $asset_id, + 'asset_id' => $request->input('asset_id'), 'note' => $request->input('note'), ]);