Make a few more factories a bit more pure

This commit is contained in:
Marcus Moore 2023-03-16 18:29:51 -07:00
parent 605f214597
commit 3849bb838d
No known key found for this signature in database
6 changed files with 76 additions and 204 deletions

View file

@ -40,9 +40,7 @@ class AccessoryFactory extends Factory
$this->faker->randomElement(['Bluetooth', 'Wired']),
$this->faker->randomElement(['Keyboard', 'Wired'])
),
'user_id' => function () {
return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin();
},
'user_id' => User::factory()->firstAdmin(),
'category_id' => Category::factory(),
'model_number' => $this->faker->numberBetween(1000000, 50000000),
'location_id' => Location::factory(),

View file

@ -36,9 +36,7 @@ class ActionlogFactory extends Factory
return [
'item_id' => Asset::factory(),
'item_type' => Asset::class,
'user_id' => function () {
return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin();
},
'user_id' => User::factory()->firstAdmin(),
'action_type' => 'uploaded',
];
}

View file

@ -46,9 +46,7 @@ class AssetFactory extends Factory
'status_id' => function () {
return Statuslabel::where('name', 'Ready to Deploy')->first() ?? Statuslabel::factory()->rtd()->create(['name' => 'Ready to Deploy']);
},
'user_id' => function () {
return User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin();
},
'user_id' => User::factory()->firstAdmin(),
'asset_tag' => $this->faker->unixTime('now'),
'notes' => 'Created by DB seeder',
'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'),

View file

@ -5,6 +5,7 @@ namespace Database\Seeders;
use App\Models\Accessory;
use App\Models\Location;
use App\Models\Supplier;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@ -29,23 +30,33 @@ class AccessorySeeder extends Seeder
$supplierIds = Supplier::all()->pluck('id');
Accessory::factory()->count(1)->appleUsbKeyboard()->create([
$admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create();
Accessory::factory()->appleUsbKeyboard()->create([
'location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
'user_id' => $admin->id,
]);
Accessory::factory()->count(1)->appleBtKeyboard()->create([
Accessory::factory()->appleBtKeyboard()->create([
'location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
'user_id' => $admin->id,
]);
Accessory::factory()->count(1)->appleMouse()->create([
Accessory::factory()->appleMouse()->create([
'location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
'user_id' => $admin->id,
]);
Accessory::factory()->count(1)->microsoftMouse()->create([
Accessory::factory()->microsoftMouse()->create([
'location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
'user_id' => $admin->id,
]);
$src = public_path('/img/demo/accessories/');
$dst = 'accessories'.'/';
$del_files = Storage::files($dst);

View file

@ -5,6 +5,7 @@ namespace Database\Seeders;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Location;
use App\Models\User;
use Illuminate\Database\Seeder;
class ActionlogSeeder extends Seeder
@ -21,14 +22,16 @@ class ActionlogSeeder extends Seeder
$this->call(LocationSeeder::class);
}
$admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create();
Actionlog::factory()
->count(300)
->assetCheckoutToUser()
->create();
->create(['user_id' => $admin->id]);
Actionlog::factory()
->count(100)
->assetCheckoutToLocation()
->create();
->create(['user_id' => $admin->id]);
}
}

View file

@ -5,6 +5,7 @@ namespace Database\Seeders;
use App\Models\Asset;
use App\Models\Location;
use App\Models\Supplier;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
@ -13,201 +14,41 @@ use Illuminate\Support\Facades\Storage;
class AssetSeeder extends Seeder
{
private $admin;
private $locationIds;
private $supplierIds;
public function run()
{
Asset::truncate();
if (! Location::count()) {
$this->call(LocationSeeder::class);
}
$this->ensureLocationsSeeded();
$this->ensureSuppliersSeeded();
$locationIds = Location::all()->pluck('id');
$this->admin = User::where('permissions->superuser', '1')->first() ?? User::factory()->firstAdmin()->create();
$this->locationIds = Location::all()->pluck('id');
$this->supplierIds = Supplier::all()->pluck('id');
if (! Supplier::count()) {
$this->call(SupplierSeeder::class);
}
$supplierIds = Supplier::all()->pluck('id');
Asset::factory()
->count(1000)
->laptopMbp()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(50)
->laptopMbpPending()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(50)
->laptopMbpArchived()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(50)
->laptopAir()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(5)
->laptopSurface()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(5)
->laptopXps()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(5)
->laptopSpectre()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(5)
->laptopZenbook()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(3)
->laptopYoga()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(30)
->desktopMacpro()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(30)
->desktopLenovoI5()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(30)
->desktopOptiplex()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(5)
->confPolycom()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(2)
->confPolycomcx()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(12)
->tabletIpad()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(4)
->tabletTab3()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(27)
->phoneIphone11()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(40)
->phoneIphone12()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(10)
->ultrafine()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()
->count(10)
->ultrasharp()
->state(new Sequence(fn($sequence) => [
'rtd_location_id' => $locationIds->random(),
'supplier_id' => $supplierIds->random(),
]))
->create();
Asset::factory()->count(1000)->laptopMbp()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(50)->laptopMbpPending()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(50)->laptopMbpArchived()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(50)->laptopAir()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(5)->laptopSurface()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(5)->laptopXps()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(5)->laptopSpectre()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(5)->laptopZenbook()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(3)->laptopYoga()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(30)->desktopMacpro()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(30)->desktopLenovoI5()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(30)->desktopOptiplex()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(5)->confPolycom()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(2)->confPolycomcx()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(12)->tabletIpad()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(4)->tabletTab3()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(27)->phoneIphone11()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(40)->phoneIphone12()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(10)->ultrafine()->state(new Sequence($this->getState()))->create();
Asset::factory()->count(10)->ultrasharp()->state(new Sequence($this->getState()))->create();
$del_files = Storage::files('assets');
foreach ($del_files as $del_file) { // iterate files
@ -221,4 +62,27 @@ class AssetSeeder extends Seeder
DB::table('checkout_requests')->truncate();
}
private function ensureLocationsSeeded()
{
if (! Location::count()) {
$this->call(LocationSeeder::class);
}
}
private function ensureSuppliersSeeded()
{
if (! Supplier::count()) {
$this->call(SupplierSeeder::class);
}
}
private function getState()
{
return fn($sequence) => [
'rtd_location_id' => $this->locationIds->random(),
'supplier_id' => $this->supplierIds->random(),
'user_id' => $this->admin->id,
];
}
}