From 951f03094a27f94f9120e05648bdbe911cb2c4a9 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 19 Sep 2024 17:01:17 +0100 Subject: [PATCH] Added created_by to kits Signed-off-by: snipe --- .../Api/PredefinedKitsController.php | 22 ++++++++++++++++--- .../Kits/PredefinedKitsController.php | 1 + .../PredefinedKitsTransformer.php | 7 ++++++ app/Models/PredefinedKit.php | 12 ++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/PredefinedKitsController.php b/app/Http/Controllers/Api/PredefinedKitsController.php index 26ccb50354..7b9e8f3ac9 100644 --- a/app/Http/Controllers/Api/PredefinedKitsController.php +++ b/app/Http/Controllers/Api/PredefinedKitsController.php @@ -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(); diff --git a/app/Http/Controllers/Kits/PredefinedKitsController.php b/app/Http/Controllers/Kits/PredefinedKitsController.php index 187f5aad14..54f7514510 100644 --- a/app/Http/Controllers/Kits/PredefinedKitsController.php +++ b/app/Http/Controllers/Kits/PredefinedKitsController.php @@ -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()); diff --git a/app/Http/Transformers/PredefinedKitsTransformer.php b/app/Http/Transformers/PredefinedKitsTransformer.php index a5d37e5c72..b5de12fc08 100644 --- a/app/Http/Transformers/PredefinedKitsTransformer.php +++ b/app/Http/Transformers/PredefinedKitsTransformer.php @@ -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'] = [ diff --git a/app/Models/PredefinedKit.php b/app/Models/PredefinedKit.php index f9464e12c7..36790a1fc7 100644 --- a/app/Models/PredefinedKit.php +++ b/app/Models/PredefinedKit.php @@ -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); + } }