From 1441cf9f4f135502bbb4e44604cc8ef69818c72d Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 12 Apr 2022 21:04:57 +0100 Subject: [PATCH] Ports #10494 to master Signed-off-by: snipe --- .../Controllers/Api/LicensesController.php | 2 +- .../Licenses/LicensesController.php | 2 +- app/Models/License.php | 26 ++++++++++++++----- database/factories/LicenseFactory.php | 8 +++++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index 2371f4bdac..fcdfea5a13 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -26,7 +26,7 @@ class LicensesController extends Controller public function index(Request $request) { $this->authorize('view', License::class); - $licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'freeSeats', 'supplier','category')->withCount('freeSeats as free_seats_count')); + $licenses = Company::scopeCompanyables(License::with('company', 'manufacturer', 'supplier','category')->withCount('freeSeats as free_seats_count')); if ($request->filled('company_id')) { diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php index c13eb8d9e0..dc7a34c371 100755 --- a/app/Http/Controllers/Licenses/LicensesController.php +++ b/app/Http/Controllers/Licenses/LicensesController.php @@ -235,7 +235,7 @@ class LicensesController extends Controller public function show($licenseId = null) { - $license = License::with('assignedusers', 'licenseSeats.user', 'licenseSeats.asset')->find($licenseId); + $license = License::with('assignedusers')->find($licenseId); if ($license) { $this->authorize('view', $license); diff --git a/app/Models/License.php b/app/Models/License.php index 37a0e8d411..fa2b3b6eac 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -45,7 +45,7 @@ class License extends Depreciable protected $rules = array( 'name' => 'required|string|min:3|max:255', - 'seats' => 'required|min:1|max:999|integer', + 'seats' => 'required|min:1|max:9999|integer', 'license_email' => 'email|nullable|max:120', 'license_name' => 'string|nullable|max:100', 'notes' => 'string|nullable', @@ -173,13 +173,25 @@ class License extends Depreciable $logAction->logaction('delete seats'); return true; } - // Else we're adding seats. - DB::transaction(function () use ($license, $oldSeats, $newSeats) { - for ($i = $oldSeats; $i < $newSeats; $i++) { - $license->licenseSeatsRelation()->save(new LicenseSeat, ['user_id' => Auth::id()]); - } + //Create enough seats for the change. + $licenseInsert = []; + for ($i = $oldSeats; $i < $newSeats; $i++) { + $licenseInsert[] = [ + 'user_id' => Auth::id(), + 'license_id' => $license->id, + 'created_at' => now(), + 'updated_at' => now() + ]; + } + //Chunk and use DB transactions to prevent timeouts. + + collect($licenseInsert)->chunk(1000)->each(function ($chunk) { + DB::transaction(function () use ($chunk) { + LicenseSeat::insert($chunk->toArray()); + }); }); - // On initail create, we shouldn't log the addition of seats. + + // On initial create, we shouldn't log the addition of seats. if ($license->id) { //Log the addition of license to the log. $logAction = new Actionlog(); diff --git a/database/factories/LicenseFactory.php b/database/factories/LicenseFactory.php index b4b440f45c..96b77bc14b 100644 --- a/database/factories/LicenseFactory.php +++ b/database/factories/LicenseFactory.php @@ -1,4 +1,8 @@ define(App\Models\License::class, function (Faker\Generator $faker) { return [ 'user_id' => 1, - 'license_name' => $faker->name, + 'name' => $this->faker->name, 'license_email' => $faker->safeEmail, 'serial' => $faker->uuid, 'notes' => 'Created by DB seeder', + 'seats' => $this->faker->numberBetween(1, 10), 'purchase_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get()), 'order_number' => $faker->numberBetween(1000000, 50000000), 'expiration_date' => $faker->dateTimeBetween('now', '+3 years', date_default_timezone_get())->format('Y-m-d H:i:s'), 'reassignable' => $faker->boolean(), 'termination_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get())->format('Y-m-d H:i:s'), 'supplier_id' => $faker->numberBetween(1,5), + 'category_id' => Category::where('category_type', '=', 'license')->inRandomOrder()->first()->id, ]; });