Skip to content

Commit

Permalink
Add SolarHomeSystem seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
dmohns committed Oct 24, 2024
1 parent 4b4585b commit 88e0cb2
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/backend/app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
namespace App\Models;

use App\Models\Base\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Asset extends BaseModel
{
use HasFactory;

public const RELATION_NAME = 'appliance';
protected $table = 'assets';

public function assetType(): HasOne
public function assetType(): BelongsTo
{
return $this->hasOne(AssetType::class, 'id', 'asset_type_id');
return $this->belongsTo(AssetType::class);
}

public function agentAssignedAppliance(): HasMany
Expand Down
3 changes: 3 additions & 0 deletions src/backend/app/Models/SolarHomeSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace App\Models;

use App\Models\Base\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class SolarHomeSystem extends BaseModel
{
use HasFactory;

public const RELATION_NAME = 'solar_home_system';
protected $table = 'solar_home_systems';

Expand Down
24 changes: 24 additions & 0 deletions src/backend/database/factories/AssetFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\Asset;
use Illuminate\Database\Eloquent\Factories\Factory;

class AssetFactory extends Factory
{
protected $model = Asset::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->word,
'price' => $this->faker->randomDigitNotNull() * 100000,
];
}
}
1 change: 0 additions & 1 deletion src/backend/database/factories/AssetTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function definition()
{
return [
'name' => $this->faker->word,
'price' => $this->faker->randomFloat(2, 0, 100),
];
}
}
14 changes: 14 additions & 0 deletions src/backend/database/factories/ManufacturerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public function isMeterManufacturer()
});
}

/**
* Indicate that the manufacturer is for SHS devices.
*
* @return Factory
*/
public function isShsManufacturer()
{
return $this->state(function (array $attributes) {
return [
'type' => 'shs',
];
});
}

public function definition(): array
{
$inflector = InflectorFactory::create()->build();
Expand Down
20 changes: 20 additions & 0 deletions src/backend/database/factories/SolarHomeSystemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Factories;

use App\Models\SolarHomeSystem;
use Illuminate\Database\Eloquent\Factories\Factory;

class SolarHomeSystemFactory extends Factory
{
protected $model = SolarHomeSystem::class;

public function definition(): array
{
return [
// 'asset_id' => 1,
'serial_number' => $this->faker->unique()->uuid,
'manufacturer_id' => 1,
];
}
}
1 change: 1 addition & 0 deletions src/backend/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function run()
ClusterSeeder::class,
CustomerSeeder::class,
MeterSeeder::class,
SolarHomeSystemSeeder::class,
TicketSeeder::class,
AgentSeeder::class,
]);
Expand Down
80 changes: 80 additions & 0 deletions src/backend/database/seeders/SolarHomeSystemSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Database\Seeders;

use App\Models\Asset;
use App\Models\AssetType;
use App\Models\Device;
use App\Models\Manufacturer;
use App\Models\SolarHomeSystem;
use Illuminate\Database\Seeder;
use MPM\DatabaseProxy\DatabaseProxyManagerService;

class SolarHomeSystemSeeder extends Seeder
{
public function __construct(
private DatabaseProxyManagerService $databaseProxyManagerService,
) {
$this->databaseProxyManagerService->buildDatabaseConnectionDummyCompany();
}

/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Manufacturer
// For now, we just adding some dummy Manufacturers.
// Later, this should probably be synced with the manufacturers
// for which we have plugins in the Demo setup.
$manufacturers = Manufacturer::factory()
->count(2)
->isShsManufacturer()
->create();

// Get the SHS asset type
$assetType = AssetType::where('name', 'Solar Home System')->first();

// Create our appliances, i.e. sales deals (?)
$appliances = Asset::factory()
->count(5)
->for($assetType)
->sequence(
// Thank you ChatGPT for generating these names... 🤖
[
'name' => 'SunPower Home 3000',
'price' => 500000,
],
[
'name' => 'SunPower Home 1000',
'price' => 100000,
],
['name' => 'EcoBright Solar Kit'],
['name' => 'HelioVolt Pro Series'],
['name' => 'SolaraMax Home Energy'],
)
->create();

// Create not-yet-sold SHS such that we can "sell" them in the Demo
for ($i = 1; $i <= 10; ++$i) {
$solarHomeSystem = SolarHomeSystem::factory()
->for(Asset::all()->random(), 'appliance')
->for(Manufacturer::where('type', 'shs')->get()->random())
->create();

$device = Device::factory()
->for($solarHomeSystem, 'device')
->create([
'device_serial' => $solarHomeSystem->serial_number,
]);
}

// Create already-sold SHS
$sold_solar_home_systems = null;

// TBD: assign appliances to Agents
// This is required to use the Agent app.
}
}

0 comments on commit 88e0cb2

Please sign in to comment.