From a50ddb4a5d10a2931ec20ab246874856730e7fd4 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 16 Mar 2023 15:25:23 -0700 Subject: [PATCH] Improve actionlog factory and seeder --- database/factories/ActionlogFactory.php | 49 ++++++++++--------------- database/seeders/ActionlogSeeder.php | 22 ++++++++++- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/database/factories/ActionlogFactory.php b/database/factories/ActionlogFactory.php index ea4f4bfc7a..6fdd53c65e 100644 --- a/database/factories/ActionlogFactory.php +++ b/database/factories/ActionlogFactory.php @@ -4,7 +4,6 @@ namespace Database\Factories; use App\Models\Actionlog; use App\Models\Asset; -use App\Models\Company; use App\Models\Location; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; @@ -25,7 +24,7 @@ class ActionlogFactory extends Factory * * @var string */ - protected $model = \App\Models\Actionlog::class; + protected $model = Actionlog::class; /** * Define the model's default state. @@ -34,10 +33,9 @@ class ActionlogFactory extends Factory */ public function definition() { - $asset = \App\Models\Asset::factory()->create(); return [ - 'item_type' => get_class($asset), - 'item_id' => 1, + 'item_id' => Asset::factory(), + 'item_type' => Asset::class, 'user_id' => function () { return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin(); }, @@ -45,62 +43,53 @@ class ActionlogFactory extends Factory ]; } - public function assetCheckoutToUser() { return $this->state(function () { - $target = \App\Models\User::inRandomOrder()->first(); - $item = \App\Models\Asset::RTD()->inRandomOrder()->first(); - $user_id = rand(1, 2); // keep it simple - make it one of the two superadmins - $asset = Asset::where('id', $item->id) - ->update( + $target = User::inRandomOrder()->first(); + $asset = Asset::RTD()->inRandomOrder()->first(); + + $asset->update( [ 'assigned_to' => $target->id, - 'assigned_type' => \App\Models\User::class, + 'assigned_type' => User::class, 'location_id' => $target->location_id, ] ); return [ 'created_at' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()), - 'user_id' => $user_id, 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => \App\Models\Asset::class, + 'item_id' => $asset->id, + 'item_type' => Asset::class, 'target_id' => $target->id, - 'target_type' => get_class($target), - + 'target_type' => User::class, ]; }); } - public function assetCheckoutToLocation() { return $this->state(function () { - $target = \App\Models\Location::inRandomOrder()->first(); - $item = \App\Models\Asset::inRandomOrder()->RTD()->first(); - $user_id = rand(1, 2); // keep it simple - make it one of the two superadmins - $asset = \App\Models\Asset::where('id', $item->id) - ->update( + $target = Location::inRandomOrder()->first(); + $asset = Asset::inRandomOrder()->RTD()->first(); + + $asset->update( [ 'assigned_to' => $target->id, - 'assigned_type' => \App\Models\Location::class, + 'assigned_type' => Location::class, 'location_id' => $target->id, ] ); return [ 'created_at' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()), - 'user_id' => $user_id, 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => \App\Models\Asset::class, + 'item_id' => $asset->id, + 'item_type' => Asset::class, 'target_id' => $target->id, - 'target_type' => get_class($target), + 'target_type' => Location::class, ]; }); } - - } diff --git a/database/seeders/ActionlogSeeder.php b/database/seeders/ActionlogSeeder.php index 6ff0645d20..b680b87b35 100644 --- a/database/seeders/ActionlogSeeder.php +++ b/database/seeders/ActionlogSeeder.php @@ -3,6 +3,8 @@ namespace Database\Seeders; use App\Models\Actionlog; +use App\Models\Asset; +use App\Models\Location; use Illuminate\Database\Seeder; class ActionlogSeeder extends Seeder @@ -10,7 +12,23 @@ class ActionlogSeeder extends Seeder public function run() { Actionlog::truncate(); - Actionlog::factory()->count(300)->assetCheckoutToUser()->create(); - Actionlog::factory()->count(100)->assetCheckoutToLocation()->create(); + + if (! Asset::count()) { + $this->call(AssetSeeder::class); + } + + if (! Location::count()) { + $this->call(LocationSeeder::class); + } + + Actionlog::factory() + ->count(300) + ->assetCheckoutToUser() + ->create(); + + Actionlog::factory() + ->count(100) + ->assetCheckoutToLocation() + ->create(); } }