From b30bbe174019d5634189b1b1498a211513aa4403 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 11 Jun 2021 19:41:20 -0700 Subject: [PATCH] Fixed more factories Signed-off-by: snipe --- database/factories/ActionlogFactory.php | 210 +++++++++--------------- database/factories/AssetFactory.php | 6 +- database/factories/ComponentFactory.php | 41 ++--- database/factories/UserFactory.php | 16 +- database/seeders/ActionlogSeeder.php | 6 +- 5 files changed, 95 insertions(+), 184 deletions(-) diff --git a/database/factories/ActionlogFactory.php b/database/factories/ActionlogFactory.php index 952a3d42b4..005e975de4 100644 --- a/database/factories/ActionlogFactory.php +++ b/database/factories/ActionlogFactory.php @@ -9,140 +9,14 @@ use App\Models\Location; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -$factory->defineAs(App\Models\Actionlog::class, 'asset-upload', function ($faker) { - $asset = \App\Models\Asset::factory()->create(); - - return [ - 'item_type' => get_class($asset), - 'item_id' => 1, - 'user_id' => 1, - 'filename' => $faker->word, - 'action_type' => 'uploaded', - ]; -}); - -$factory->defineAs(Actionlog::class, 'asset-checkout-user', function (Faker\Generator $faker) { - $target = User::inRandomOrder()->first(); - $item = 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( - [ - 'assigned_to' => $target->id, - 'assigned_type' => App\Models\User::class, - 'assigned_to' => $target->location_id, - ] - ); - - return [ - 'created_at' => $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, - 'target_id' => $target->id, - 'target_type' => get_class($target), - - ]; -}); - -$factory->defineAs(Actionlog::class, 'asset-checkout-location', function (Faker\Generator $faker) { - $target = Location::inRandomOrder()->first(); - $item = 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( - [ - 'assigned_to' => $target->id, - 'assigned_type' => App\Models\Location::class, - 'assigned_to' => $target->id, - ] - ); - - return [ - 'created_at' => $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, - 'target_id' => $target->id, - 'target_type' => get_class($target), - ]; -}); - -// This doesn't work - we need to assign a seat - -$factory->defineAs(Actionlog::class, 'license-checkout-asset', function (Faker\Generator $faker) { - $target = Asset::inRandomOrder()->RTD()->first(); - $item = License::inRandomOrder()->first(); - $user_id = rand(1, 2); // keep it simple - make it one of the two superadmins - - return [ - 'user_id' => $user_id, - 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => get_class($item), - 'target_id' => $target->id, - 'target_type' => get_class($target), - 'created_at' => $faker->dateTime(), - 'note' => $faker->sentence, - ]; -}); - -$factory->defineAs(Actionlog::class, 'accessory-checkout', function (Faker\Generator $faker) { - $target = Asset::inRandomOrder()->RTD()->first(); - $item = Accessory::inRandomOrder()->first(); - $user_id = rand(1, 2); // keep it simple - make it one of the two superadmins - - return [ - 'user_id' => $user_id, - 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => get_class($item), - 'target_id' => $target->id, - 'target_type' => get_class($target), - 'created_at' => $faker->dateTime(), - 'note' => $faker->sentence, - ]; -}); - -$factory->defineAs(Actionlog::class, 'consumable-checkout', function (Faker\Generator $faker) { - $company = \App\Models\Company::factory()->create(); - $user = \App\Models\User::factory()->create(['company_id' => $company->id]); - $target = \App\Models\User::factory()->create(['company_id' => $company->id]); - $item = \App\Models\Consumable::factory()->create(['company_id' => $company->id]); - - return [ - 'user_id' => $user->id, - 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => get_class($item), - 'target_id' => $target->id, - 'target_type' => get_class($target), - 'created_at' => $faker->dateTime(), - 'note' => $faker->sentence, - 'company_id' => $company->id, - ]; -}); - -$factory->defineAs(Actionlog::class, 'component-checkout', function (Faker\Generator $faker) { - $company = \App\Models\Company::factory()->create(); - $user = \App\Models\User::factory()->create(['company_id' => $company->id]); - $target = \App\Models\User::factory()->create(['company_id' => $company->id]); - $item = \App\Models\Component::factory()->create(['company_id' => $company->id]); - - return [ - 'user_id' => $user->id, - 'action_type' => 'checkout', - 'item_id' => $item->id, - 'item_type' => get_class($item), - 'target_id' => $target->id, - 'target_type' => get_class($target), - 'created_at' => $faker->dateTime(), - 'note' => $faker->sentence, - 'company_id' => $company->id, - ]; -}); +/* +|-------------------------------------------------------------------------- +| Action Log Factories +|-------------------------------------------------------------------------- +| +| This simulates checkin/checkout/etc activities +| +*/ class ActionlogFactory extends Factory { @@ -151,7 +25,7 @@ class ActionlogFactory extends Factory * * @var string */ - protected $model = Actionlog::class; + protected $model = \App\Models\Actionlog::class; /** * Define the model's default state. @@ -160,8 +34,72 @@ class ActionlogFactory extends Factory */ public function definition() { + $asset = \App\Models\Asset::factory()->create(); return [ - 'note' => 'Sample checkout from DB seeder!', + 'item_type' => get_class($asset), + 'item_id' => 1, + 'user_id' => 1, + 'filename' => $this->faker->word, + 'action_type' => 'uploaded', ]; } + + + public function assetCheckoutToUser() + { + return $this->state(function () { + $target = \App\Models\User::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 = Asset::where('id', $item->id) + ->update( + [ + 'assigned_to' => $target->id, + 'assigned_type' => \App\Models\User::class, + 'assigned_to' => $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, + 'target_id' => $target->id, + 'target_type' => get_class($target), + + ]; + }); + } + + + 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( + [ + 'assigned_to' => $target->id, + 'assigned_type' => \App\Models\Location::class, + 'assigned_to' => $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, + 'target_id' => $target->id, + 'target_type' => get_class($target), + ]; + }); + } + + } diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php index f4a5b73807..b4831e43cb 100644 --- a/database/factories/AssetFactory.php +++ b/database/factories/AssetFactory.php @@ -242,7 +242,7 @@ class AssetFactory extends Factory return [ 'model_id' => 1, 'assigned_to' => \App\Models\User::factory()->create()->id, - 'assigned_type' => App\Models\User::class, + 'assigned_type' => \App\Models\User::class, ]; }); } @@ -253,7 +253,7 @@ class AssetFactory extends Factory return [ 'model_id' => 1, 'assigned_to' => \App\Models\Location::factory()->create()->id, - 'assigned_type' => App\Models\Location::class, + 'assigned_type' => \App\Models\Location::class, ]; }); } @@ -264,7 +264,7 @@ class AssetFactory extends Factory return [ 'model_id' => 1, 'assigned_to' => \App\Models\Asset::factory()->create()->id, - 'assigned_type' => App\Models\Asset::class, + 'assigned_type' => \App\Models\Asset::class, ]; }); } diff --git a/database/factories/ComponentFactory.php b/database/factories/ComponentFactory.php index 78ea4572c0..bd971162ab 100644 --- a/database/factories/ComponentFactory.php +++ b/database/factories/ComponentFactory.php @@ -30,12 +30,20 @@ class ComponentFactory extends Factory public function definition() { return [ - 'user_id' => 1, + 'name' => $this->faker->text(20), + 'category_id' => function () { + return \App\Models\Category::factory()->create()->id; + }, + 'location_id' => 1, + 'serial' => $this->faker->uuid, + 'qty' => $this->faker->numberBetween(3, 10), 'order_number' => $this->faker->numberBetween(1000000, 50000000), - 'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()), - 'purchase_cost' => $this->faker->randomFloat(2, 1, 50), - 'qty' => $this->faker->numberBetween(5, 10), + 'purchase_date' => $this->faker->dateTime(), + 'purchase_cost' => $this->faker->randomFloat(2), 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), + 'company_id' => function () { + return \App\Models\Company::factory()->create()->id; + }, ]; } @@ -89,28 +97,5 @@ class ComponentFactory extends Factory }); } - /** - * Define the model's default state. - * - * @return array - */ - public function definition() - { - return [ - 'name' => $this->faker->text(20), - 'category_id' => function () { - return \App\Models\Category::factory()->create()->id; - }, - 'location_id' => 1, - 'serial' => $this->faker->uuid, - 'qty' => $this->faker->numberBetween(3, 10), - 'order_number' => $this->faker->numberBetween(1000000, 50000000), - 'purchase_date' => $this->faker->dateTime(), - 'purchase_cost' => $this->faker->randomFloat(2), - 'min_amt' => $this->faker->numberBetween($min = 1, $max = 2), - 'company_id' => function () { - return \App\Models\Company::factory()->create()->id; - }, - ]; - } + } diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 63fb8ffa7f..86681f2598 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -5,21 +5,6 @@ namespace Database\Factories; use App\Models\Company; use Illuminate\Database\Eloquent\Factories\Factory; -$password = bcrypt('password'); - -// USER GLOBAL PERMISSION STATES - -// USER ASSET PERMISSION STATES - -// USER ACCESSORY PERMISSION STATES - -// USER CONSUMABLE PERMISSION STATES - -// USER LICENSE PERMISSION STATES - -// USER COMPONENTS PERMISSION STATES - -// USER USER PERMISSION STATES class UserFactory extends Factory { @@ -37,6 +22,7 @@ class UserFactory extends Factory */ public function definition() { + $password = bcrypt('password'); return [ 'activated' => 1, 'address' => $this->faker->address, diff --git a/database/seeders/ActionlogSeeder.php b/database/seeders/ActionlogSeeder.php index 8603d63a89..06eb27bdeb 100644 --- a/database/seeders/ActionlogSeeder.php +++ b/database/seeders/ActionlogSeeder.php @@ -10,7 +10,9 @@ class ActionlogSeeder extends Seeder public function run() { Actionlog::truncate(); - Actionlog::factory()->count('asset-checkout-user', 300)->create(); - Actionlog::factory()->count('asset-checkout-location', 100)->create(); + Actionlog::factory()->count(300)->assetCheckoutToUser()->create(); + Actionlog::factory()->count(100)->assetCheckoutToLocation()->create(); + + } }