mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 13:44:06 -08:00
Improve actionlog factory and seeder
This commit is contained in:
parent
daf6e9fa4b
commit
a50ddb4a5d
|
@ -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,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue