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

Implement Checkout API for Consumables #8258

Merged
merged 2 commits into from
Jul 31, 2020
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
54 changes: 53 additions & 1 deletion app/Http/Controllers/Api/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\User;
use App\Http\Transformers\ConsumablesTransformer;
use App\Helpers\Helper;

Expand Down Expand Up @@ -157,7 +158,7 @@ public function destroy($id)
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.delete.success')));
}

/**
/**
* Returns a JSON response containing details on the users associated with this consumable.
*
* @author [A. Gianotto] [<[email protected]>]
Expand Down Expand Up @@ -196,4 +197,55 @@ function ($query) {
$data = array('total' => $consumableCount, 'rows' => $rows);
return $data;
}

/**
* Checkout a consumable
*
* @author [A. Gutierrez] [<[email protected]>]
* @param int $id
* @since [v4.9.5]
* @return JsonResponse
*/
public function checkout(Request $request, $id)
{
// Check if the consumable exists
if (is_null($consumable = Consumable::find($id))) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.does_not_exist')));
}

$this->authorize('checkout', $consumable);

if ($consumable->qty > 0) {

// Check if the user exists
$assigned_to = $request->input('assigned_to');
if (is_null($user = User::find($assigned_to))) {
// Return error message
return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found'));
}

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

$consumable->users()->attach($consumable->id, [
'consumable_id' => $consumable->id,
'user_id' => $user->id,
'assigned_to' => $assigned_to
]);

// Log checkout event
$logaction = $consumable->logCheckout(e($request->input('note')), $user);
$data['log_id'] = $logaction->id;
$data['eula'] = $consumable->getEula();
$data['first_name'] = $user->first_name;
$data['item_name'] = $consumable->name;
$data['checkout_date'] = $logaction->created_at;
$data['note'] = $logaction->note;
$data['require_acceptance'] = $consumable->requireAcceptance();

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
}

return response()->json(Helper::formatStandardApiResponse('error', null, 'No consumables remaining'));
}
}
23 changes: 16 additions & 7 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@


/*--- Consumables API ---*/

Route::resource('consumables', 'ConsumablesController',
[
'names' =>
Expand All @@ -204,12 +203,22 @@
'parameters' => ['consumable' => 'consumable_id']
]
); // Consumables resource
Route::get('consumables/view/{id}/users',
[
'as' => 'api.consumables.showUsers',
'uses' => 'ConsumablesController@getDataView'
]
);

Route::group(['prefix' => 'consumables'], function () {
Route::get('view/{id}/users',
[
'as' => 'api.consumables.showUsers',
'uses' => 'ConsumablesController@getDataView'
]
);

Route::post('{consumable}/checkout',
[
'as' => 'api.consumables.checkout',
'uses' => 'ConsumablesController@checkout'
]
);
});

/*--- Depreciations API ---*/

Expand Down