Added created_by to kits

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-09-19 17:01:17 +01:00
parent 354b00ef15
commit 951f03094a
4 changed files with 39 additions and 3 deletions

View file

@ -23,7 +23,6 @@ class PredefinedKitsController extends Controller
public function index(Request $request) : JsonResponse | array
{
$this->authorize('view', PredefinedKit::class);
$allowed_columns = ['id', 'name'];
$kits = PredefinedKit::query();
@ -36,8 +35,25 @@ class PredefinedKitsController extends Controller
$limit = app('api_limit_value');
$order = $request->input('order') === 'desc' ? 'desc' : 'asc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'name';
$kits->orderBy($sort, $order);
switch ($request->input('sort')) {
case 'created_by':
$kits = $kits->OrderByCreatedBy($order);
break;
default:
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
// These must match a column on the consumables table directly.
$allowed_columns = [
'id',
'name',
'created_at',
'updated_at',
];
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
$kits = $kits->orderBy($sort, $order);
break;
}
$total = $kits->count();
$kits = $kits->skip($offset)->take($limit)->get();

View file

@ -55,6 +55,7 @@ class PredefinedKitsController extends Controller
// Create a new Predefined Kit
$kit = new PredefinedKit;
$kit->name = $request->input('name');
$kit->created_by = auth()->id();
if (! $kit->save()) {
return redirect()->back()->withInput()->withErrors($kit->getErrors());

View file

@ -2,6 +2,7 @@
namespace App\Http\Transformers;
use App\Helpers\Helper;
use App\Models\PredefinedKit;
use App\Models\SnipeModel;
use Illuminate\Support\Facades\Gate;
@ -30,6 +31,12 @@ class PredefinedKitsTransformer
$array = [
'id' => (int) $kit->id,
'name' => e($kit->name),
'created_by' => ($kit->adminuser) ? [
'id' => (int) $kit->adminuser->id,
'name'=> e($kit->adminuser->present()->fullName()),
] : null,
'created_at' => Helper::getFormattedDateObject($kit->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($kit->updated_at, 'datetime'),
];
$permissions_array['available_actions'] = [

View file

@ -135,6 +135,13 @@ class PredefinedKit extends SnipeModel
*/
protected $searchableRelations = [];
public function adminuser()
{
return $this->belongsTo(\App\Models\User::class, 'created_by');
}
/**
* Establishes the kits -> models relationship
* @return \Illuminate\Database\Eloquent\Relations\Relation
@ -181,4 +188,9 @@ class PredefinedKit extends SnipeModel
* BEGIN QUERY SCOPES
* -----------------------------------------------
**/
public function scopeOrderByCreatedBy($query, $order)
{
return $query->leftJoin('users as admin_sort', 'kits.created_by', '=', 'admin_sort.id')->select('kits.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
}