Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes consumables' checkout 500 error and memory exhaustion #15785

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
59 changes: 38 additions & 21 deletions app/Http/Controllers/Consumables/ConsumableCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Http\Request;
use \Illuminate\Contracts\View\View;
use \Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;

class ConsumableCheckoutController extends Controller
{
Expand Down Expand Up @@ -63,8 +64,9 @@ public function create($id) : View | RedirectResponse
*/
public function store(Request $request, $consumableId)
{
if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
if (is_null($consumable = Consumable::find($consumableId))) {
return redirect()->route('consumables.index')
->with('error', trans('admin/consumables/message.not_found'));
}

$this->authorize('checkout', $consumable);
Expand All @@ -74,10 +76,16 @@ public function store(Request $request, $consumableId)
if (!isset($quantity) || !ctype_digit((string)$quantity) || $quantity <= 0) {
$quantity = 1;
}
// attaching large amounts of checkouts can exhaust memory.
if($quantity > 10000){
return redirect()->back()
->with('error', trans('admin/consumables/message.checkout.large_quantity_error', ['max_amount' => 10000]));
}

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

$admin_user = auth()->user();
Expand All @@ -86,26 +94,35 @@ public function store(Request $request, $consumableId)
// Check if the user exists
if (is_null($user = User::find($assigned_to))) {
// Redirect to the consumable management page with error
return redirect()->route('consumables.checkout.show', $consumable)->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))->withInput();
return redirect()->route('consumables.checkout.show', $consumable)
->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))
->withInput();
}

// Update the consumable data
$consumable->assigned_to = e($request->input('assigned_to'));

for ($i = 0; $i < $quantity; $i++){
$consumable->users()->attach($consumable->id, [
'consumable_id' => $consumable->id,
'created_by' => $admin_user->id,
'assigned_to' => e($request->input('assigned_to')),
'note' => $request->input('note'),
]);
$now = now();

try {
$data = [
'consumable_id' => $consumable->id,
'created_by' => $admin_user->id,
'assigned_to' => $assigned_to,
'note' => $request->input('note') ?: null,
'created_at' => $now,
'updated_at' => $now,
];

// Update the consumable data
$attachData = array_fill(0,$quantity, $data);

DB::transaction(function () use ($consumable, $attachData, $user, $request) {
$consumable->users()->attach($attachData);
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));
});
}catch(\Exception $e){
report ($e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only thing that we might not want to include. I'll leave it up to @snipe.

return redirect()->back()->with('error', trans('admin/consumables/message.checkout.checkout_error'));
}

$consumable->checkout_qty = $quantity;
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));

$request->request->add(['checkout_to_type' => 'user']);
$request->request->add(['assigned_user' => $user->id]);
$request->request->add(['checkout_to_type' => 'user', 'assigned_user' => $user->id]);

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

Expand Down
3 changes: 3 additions & 0 deletions resources/lang/en-US/admin/consumables/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'invalid_category_type' => 'The category must be a consumable category.',
'does_not_exist' => 'Consumable does not exist.',


'create' => array(
'error' => 'Consumable was not created, please try again.',
'success' => 'Consumable created successfully.'
Expand All @@ -26,6 +27,8 @@
'success' => 'Consumable checked out successfully.',
'user_does_not_exist' => 'That user is invalid. Please try again.',
'unavailable' => 'There are not enough consumables for this checkout. Please check the quantity left. ',
'checkout_error' => 'Something went wrong with your Checkout',
'large_quantity_error' => ':max_amount is the max quantity per checkout.',
),

'checkin' => array(
Expand Down
Loading