Skip to content

Commit

Permalink
fix 500 error, add try/catch, refactor store method
Browse files Browse the repository at this point in the history
  • Loading branch information
Godmartinz committed Nov 7, 2024
1 parent 5ef3183 commit 7583b16
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"repository": "https://github.com/snipe/snipe-it",
"logo": "https://pbs.twimg.com/profile_images/976748875733020672/K-HnZCCK_400x400.jpg",
"success_url": "/setup",
"env": {
"": {
"APP_ENV": {
"description": "Laravel environment mode. Unless developing the application, this should be production.",
"value": "production"
Expand Down
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', ['requested' => $quantity, 'remaining' => $consumable->numRemaining() ]));
}

// 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);
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' => '10,000 is the max quantity per checkout.',
),

'checkin' => array(
Expand Down

0 comments on commit 7583b16

Please sign in to comment.