A free open source IT asset/license management system
Licenses API: Fixed n+1 query
snipe2f749be466b1
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.phpindex fcc55cb355..2d5ef2d838 100644--- a/app/Http/Controllers/Api/LicensesController.php+++ b/app/Http/Controllers/Api/LicensesController.php@@ -34,7 +34,11 @@ class LicensesController extends Controller{$this->authorize('view', License::class);- $licenses = License::with('company', 'manufacturer', 'supplier', 'category', 'adminuser', 'licenseSeatsRelation', 'assignedCount')->withCount('freeSeats as free_seats_count');+ $licenses = License::with('company', 'manufacturer', 'supplier', 'category', 'adminuser', 'licenseSeatsRelation', 'assignedCount')+ ->withCount([+ 'freeSeats as free_seats_count',+ 'licenseseats as unreassignable_seats_count' => fn ($q) => $q->where('unreassignable_seat', true),+ ]);$settings = Setting::getSettings();if ($request->input('status') == 'inactive') {@@ -114,8 +118,10 @@ class LicensesController extends Controller$licenses->onlyTrashed();}+ $total = $licenses->count();+// Make sure the offset and limit are actually integers and do not exceed system limits- $offset = ($request->input('offset') > $licenses->count()) ? $licenses->count() : app('api_offset_value');+ $offset = ($request->input('offset') > $total) ? $total : app('api_offset_value');$limit = app('api_limit_value');$order = $request->input('order') === 'asc' ? 'asc' : 'desc';@@ -142,32 +148,30 @@ class LicensesController extends Controllerdefault:$allowed_columns =[+ 'category',+ 'company',+ 'depreciation_id',+ 'expiration_date',+ 'free_seats_count','id',+ 'license_email',+ 'license_name',+ 'min_amt','name',- 'purchase_cost',- 'expiration_date',- 'purchase_order',- 'order_number','notes',+ 'order_number',+ 'purchase_cost','purchase_date',- 'serial',- 'company',- 'category',- 'license_name',- 'license_email',- 'free_seats_count',+ 'purchase_order','seats',+ 'serial','termination_date',- 'depreciation_id',- 'min_amt',];$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';$licenses = $licenses->orderBy($sort, $order);break;}- $total = $licenses->count();-$licenses = $licenses->skip($offset)->take($limit)->get();return (new LicensesTransformer)->transformLicenses($licenses, $total);@@ -206,7 +210,10 @@ class LicensesController extends Controllerpublic function show($id): JsonResponse|array{$this->authorize('view', License::class);- $license = License::withCount('freeSeats as free_seats_count')->findOrFail($id);+ $license = License::withCount([+ 'freeSeats as free_seats_count',+ 'licenseseats as unreassignable_seats_count' => fn ($q) => $q->where('unreassignable_seat', true),+ ])->findOrFail($id);$license = $license->load('assignedusers', 'licenseSeats.user', 'licenseSeats.asset');return (new LicensesTransformer)->transformLicense($license);diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.phpindex 133715c3bb..a4ba954cce 100644--- a/app/Http/Transformers/LicensesTransformer.php+++ b/app/Http/Transformers/LicensesTransformer.php@@ -21,6 +21,8 @@ class LicensesTransformerpublic function transformLicense(License $license){+ $unreassignable = $license->reassignable ? 0 : (int) ($license->unreassignable_seats_count ?? License::unReassignableCount($license));+$array = ['id' => (int) $license->id,'name' => e($license->name),@@ -42,7 +44,7 @@ class LicensesTransformer'purchase_cost_numeric' => $license->purchase_cost,'notes' => Helper::parseEscapedMarkedownInline($license->notes),'seats' => (int) $license->seats,- 'free_seats_count' => (int) $license->free_seats_count - License::unReassignableCount($license),+ 'free_seats_count' => (int) $license->free_seats_count - $unreassignable,'remaining' => (int) $license->free_seats_count,'percent_remaining' => round($license->percentRemaining()),'min_amt' => ($license->min_amt) ? (int) ($license->min_amt) : null,@@ -76,7 +78,7 @@ class LicensesTransformer'clone' => Gate::allows('create', License::class),'update' => Gate::allows('update', License::class),'delete' => $license->isDeletable(),- 'user_can_checkout' => (bool) (($license->free_seats_count - License::unReassignableCount($license)) > 0),+ 'user_can_checkout' => (bool) (($license->free_seats_count - $unreassignable) > 0),'bulk_selectable' => ['delete' => $license->isDeletable(),],