Skip to content

Commit 7583b16

Browse files
committed
fix 500 error, add try/catch, refactor store method
1 parent 5ef3183 commit 7583b16

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"repository": "https://github.com/snipe/snipe-it",
1010
"logo": "https://pbs.twimg.com/profile_images/976748875733020672/K-HnZCCK_400x400.jpg",
1111
"success_url": "/setup",
12-
"env": {
12+
"": {
1313
"APP_ENV": {
1414
"description": "Laravel environment mode. Unless developing the application, this should be production.",
1515
"value": "production"

app/Http/Controllers/Consumables/ConsumableCheckoutController.php

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Http\Request;
1111
use \Illuminate\Contracts\View\View;
1212
use \Illuminate\Http\RedirectResponse;
13+
use Illuminate\Support\Facades\DB;
1314

1415
class ConsumableCheckoutController extends Controller
1516
{
@@ -63,8 +64,9 @@ public function create($id) : View | RedirectResponse
6364
*/
6465
public function store(Request $request, $consumableId)
6566
{
66-
if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
67-
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
67+
if (is_null($consumable = Consumable::find($consumableId))) {
68+
return redirect()->route('consumables.index')
69+
->with('error', trans('admin/consumables/message.not_found'));
6870
}
6971

7072
$this->authorize('checkout', $consumable);
@@ -74,10 +76,16 @@ public function store(Request $request, $consumableId)
7476
if (!isset($quantity) || !ctype_digit((string)$quantity) || $quantity <= 0) {
7577
$quantity = 1;
7678
}
79+
// attaching large amounts of checkouts can exhaust memory.
80+
if($quantity > 10000){
81+
return redirect()->back()
82+
->with('error', trans('admin/consumables/message.checkout.large_quantity_error', ['requested' => $quantity, 'remaining' => $consumable->numRemaining() ]));
83+
}
7784

7885
// Make sure there is at least one available to checkout
79-
if ($consumable->numRemaining() <= 0 || $quantity > $consumable->numRemaining()) {
80-
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable', ['requested' => $quantity, 'remaining' => $consumable->numRemaining() ]));
86+
if ($consumable->numRemaining() <= 0 || $quantity > $consumable->numRemaining() ){
87+
return redirect()->route('consumables.index')
88+
->with('error', trans('admin/consumables/message.checkout.unavailable', ['requested' => $quantity, 'remaining' => $consumable->numRemaining() ]));
8189
}
8290

8391
$admin_user = auth()->user();
@@ -86,26 +94,35 @@ public function store(Request $request, $consumableId)
8694
// Check if the user exists
8795
if (is_null($user = User::find($assigned_to))) {
8896
// Redirect to the consumable management page with error
89-
return redirect()->route('consumables.checkout.show', $consumable)->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))->withInput();
97+
return redirect()->route('consumables.checkout.show', $consumable)
98+
->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))
99+
->withInput();
90100
}
91-
92-
// Update the consumable data
93-
$consumable->assigned_to = e($request->input('assigned_to'));
94-
95-
for ($i = 0; $i < $quantity; $i++){
96-
$consumable->users()->attach($consumable->id, [
97-
'consumable_id' => $consumable->id,
98-
'created_by' => $admin_user->id,
99-
'assigned_to' => e($request->input('assigned_to')),
100-
'note' => $request->input('note'),
101-
]);
101+
$now = now();
102+
103+
try {
104+
$data = [
105+
'consumable_id' => $consumable->id,
106+
'created_by' => $admin_user->id,
107+
'assigned_to' => $assigned_to,
108+
'note' => $request->input('note') ?: null,
109+
'created_at' => $now,
110+
'updated_at' => $now,
111+
];
112+
113+
// Update the consumable data
114+
$attachData = array_fill(0,$quantity, $data);
115+
116+
DB::transaction(function () use ($consumable, $attachData, $user, $request) {
117+
$consumable->users()->attach($attachData);
118+
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));
119+
});
120+
}catch(\Exception $e){
121+
report ($e);
122+
return redirect()->back()->with('error', trans('admin/consumables/message.checkout.checkout_error'));
102123
}
103124

104-
$consumable->checkout_qty = $quantity;
105-
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));
106-
107-
$request->request->add(['checkout_to_type' => 'user']);
108-
$request->request->add(['assigned_user' => $user->id]);
125+
$request->request->add(['checkout_to_type' => 'user', 'assigned_user' => $user->id]);
109126

110127
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
111128

resources/lang/en-US/admin/consumables/message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'invalid_category_type' => 'The category must be a consumable category.',
66
'does_not_exist' => 'Consumable does not exist.',
77

8+
89
'create' => array(
910
'error' => 'Consumable was not created, please try again.',
1011
'success' => 'Consumable created successfully.'
@@ -26,6 +27,8 @@
2627
'success' => 'Consumable checked out successfully.',
2728
'user_does_not_exist' => 'That user is invalid. Please try again.',
2829
'unavailable' => 'There are not enough consumables for this checkout. Please check the quantity left. ',
30+
'checkout_error' => 'Something went wrong with your Checkout',
31+
'large_quantity_error' => '10,000 is the max quantity per checkout.',
2932
),
3033

3134
'checkin' => array(

0 commit comments

Comments
 (0)