From 86b9fdbffee602e6cc58f2ac7c989ab168d666e2 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 7 Oct 2017 04:25:35 -0700 Subject: [PATCH] License and actionlog seeder tweaks --- database/factories/ActionLogFactory.php | 98 ++++++++++++++++--------- database/factories/LicenseFactory.php | 71 ++++++++++++++++++ database/seeds/LicenseSeeder.php | 7 +- 3 files changed, 137 insertions(+), 39 deletions(-) create mode 100644 database/factories/LicenseFactory.php diff --git a/database/factories/ActionLogFactory.php b/database/factories/ActionLogFactory.php index 4d54940648..a42185a243 100644 --- a/database/factories/ActionLogFactory.php +++ b/database/factories/ActionLogFactory.php @@ -1,76 +1,102 @@ defineAs(App\Models\Actionlog::class, 'asset-upload', function ($faker) { - $asset = factory(App\Models\Asset::class)->create(); + +$factory->define(Actionlog::class, function (Faker\Generator $faker) { return [ - 'item_type' => get_class($asset), - 'item_id' => $asset->id, - 'user_id' => function () { - return factory(App\Models\User::class)->create()->id; - }, - 'filename' => $faker->word, - 'action_type' => 'uploaded' + + 'note' => 'Sample checkout from DB seeder!', ]; }); -$factory->defineAs(Actionlog::class, 'asset-checkout', function (Faker\Generator $faker) { - $company = factory(App\Models\Company::class)->create(); - $user = factory(App\Models\User::class)->create(['company_id' => $company->id]); - $target = factory(App\Models\User::class)->create(['company_id' => $company->id]); - do { - $item = factory(App\Models\Asset::class)->create(['company_id' => $company->id]); - } while (!$item->isValid()); -// dd($item); + + +$factory->defineAs(Actionlog::class, 'asset-checkout-user', function (Faker\Generator $faker) { + $target = User::inRandomOrder()->first(); + $item = Asset::inRandomOrder()->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 + ] + ); + return [ - 'user_id' => $user->id, + '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), - 'created_at' => $faker->dateTime(), - 'note' => $faker->sentence, - 'company_id' => $company->id + + ]; }); -$factory->defineAs(Actionlog::class, 'license-checkout-asset', function (Faker\Generator $faker) { - $company = factory(App\Models\Company::class)->create(); - $user = factory(App\Models\User::class)->create(['company_id' => $company->id]); - $target = factory(App\Models\Asset::class)->create(['company_id' => $company->id]); - $item = factory(App\Models\License::class)->create(['company_id' => $company->id]); +$factory->defineAs(Actionlog::class, 'asset-checkout-location', function (Faker\Generator $faker) { + $target = Location::inRandomOrder()->first(); + $item = Asset::inRandomOrder()->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 + ] + ); return [ - 'user_id' => $user->id, + '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()->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, - 'company_id' => $company->id + 'note' => $faker->sentence ]; }); + $factory->defineAs(Actionlog::class, 'accessory-checkout', function (Faker\Generator $faker) { - $company = factory(App\Models\Company::class)->create(); - $user = factory(App\Models\User::class)->create(['company_id' => $company->id]); - $target = factory(App\Models\User::class)->create(['company_id' => $company->id]); - $item = factory(App\Models\Accessory::class)->create(['company_id' => $company->id]); + $target = Asset::inRandomOrder()->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, + '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 + 'note' => $faker->sentence ]; }); diff --git a/database/factories/LicenseFactory.php b/database/factories/LicenseFactory.php new file mode 100644 index 0000000000..7547bf7fe6 --- /dev/null +++ b/database/factories/LicenseFactory.php @@ -0,0 +1,71 @@ +define(App\Models\License::class, function (Faker\Generator $faker) { + + + + $data = [ + 'user_id' => 1, + 'location_id' => rand(1,5), + 'serial' => $faker->uuid, + 'notes' => 'Created by DB seeder', + 'purchase_date' => $faker->dateTimeBetween('-1 years','now', date_default_timezone_get()), + 'order_number' => $faker->numberBetween(1000000, 50000000), + ]; + + for ($x = 0; $x < $data['seats']; $x++) { + $seat = new LicenseSeat; + $seat->license_id = 1; + $seat->save(); + } + + return $data; +}); + +// 1 +$factory->state(App\Models\License::class, 'photoshop', function ($faker) { + return [ + 'name' => 'Photoshop', + 'manufacturer_id' => 9, + 'seats' => 10, + ]; +}); + +// 2 +$factory->state(App\Models\License::class, 'acrobat', function ($faker) { + return [ + 'name' => 'Acrobat', + 'manufacturer_id' => 9, + 'seats' => 10, + ]; +}); + +// 3 +$factory->state(App\Models\License::class, 'indesign', function ($faker) { + return [ + 'name' => 'InDesign', + 'manufacturer_id' => 9, + 'seats' => 5, + ]; +}); + + +// 4 +$factory->state(App\Models\License::class, 'office', function ($faker) { + return [ + 'name' => 'Office', + 'manufacturer_id' => 2, + 'seats' => 20, + ]; +}); + + diff --git a/database/seeds/LicenseSeeder.php b/database/seeds/LicenseSeeder.php index eebe7a1f65..75ab8d3b6e 100644 --- a/database/seeds/LicenseSeeder.php +++ b/database/seeds/LicenseSeeder.php @@ -8,9 +8,10 @@ class LicenseSeeder extends Seeder public function run() { License::truncate(); - factory(License::class, 10)->create(); - LicenseSeat::truncate(); - factory(LicenseSeat::class, 10)->create(); + factory(License::class, 1)->states('photoshop')->create(); + factory(License::class, 1)->states('acrobat')->create(); + factory(License::class, 1)->states('indesign')->create(); + factory(License::class, 1)->states('office')->create(); } }