Skip to content

Commit

Permalink
Convert dump to seeder (#464)
Browse files Browse the repository at this point in the history
* Convert sql dump to Seeder (Sms, SubConnectionType)

This change replaces the old approach of using sql dump to seed the db,
instead uses Seperate Seeders.

Created seeders; SmsSeeder and SubConnectionTypeSeeder

* fix laravel lint

* Create Seeder for agent_balance_history

* Create seeder for targets and sub_targets
  • Loading branch information
beesaferoot authored Jan 21, 2025
1 parent 29948ed commit 4f9d8e9
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@

class CreateDummyCompanyWithData extends Command {
public const SQL_DUMMY_DATA_FILE_NAMES = [
'dummy_agent_data.sql',
'dummy_data.sql',
'dummy_plugin_data.sql',
'dummy_sms_data.sql',
'dummy_ticket_data.sql',
'dummy_transaction_data.sql',
];
public const DUMMY_COMPANY_DATA = [
'name' => 'Dummy Company',
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/AgentBalanceHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

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

class AgentBalanceHistory extends BaseModel {
use HasFactory;
protected $guarded = [];

public function agent(): void {
Expand Down
3 changes: 3 additions & 0 deletions src/backend/app/Models/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand All @@ -21,6 +22,8 @@
* @property int $sender_id
*/
class Sms extends BaseModel {
use HasFactory;

public function trigger(): MorphTo {
return $this->morphTo();
}
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/SmsAndroidSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use App\Exceptions\SmsAndroidSettingNotExistingException;
use App\Models\Base\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;

/**
* @property int $id
*/
class SmsAndroidSetting extends BaseModel {
use HasFactory;
protected $table = 'sms_android_settings';

public static function getResponsible() {
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/SmsBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Models;

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

class SmsBody extends BaseModel {
use HasFactory;
protected $table = 'sms_bodies';
}
2 changes: 2 additions & 0 deletions src/backend/app/Models/SmsResendInformationKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Models;

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

class SmsResendInformationKey extends BaseModel {
use HasFactory;
protected $table = 'sms_resend_information_keys';
}
2 changes: 2 additions & 0 deletions src/backend/app/Models/SmsVariableDefaultValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Models;

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

class SmsVariableDefaultValue extends BaseModel {
use HasFactory;
protected $table = 'sms_variable_default_values';
}
3 changes: 3 additions & 0 deletions src/backend/app/Models/SubConnectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use App\Models\Base\BaseModel;
use App\Models\Meter\MeterParameter;
use App\Models\Meter\MeterTariff;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class SubConnectionType extends BaseModel {
use HasFactory;

public function connectionType(): BelongsTo {
return $this->belongsTo(ConnectionType::class);
}
Expand Down
3 changes: 3 additions & 0 deletions src/backend/app/Models/SubTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

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

/**
Expand All @@ -15,6 +16,8 @@
* @property int new_connections
*/
class SubTarget extends BaseModel {
use HasFactory;

/**
* @return BelongsTo
*/
Expand Down
3 changes: 3 additions & 0 deletions src/backend/app/Models/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Base\BaseModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
Expand All @@ -18,6 +19,8 @@
* @property int $city_id
*/
class Target extends BaseModel {
use HasFactory;

public function city(): BelongsTo {
return $this->belongsTo(City::class);
}
Expand Down
29 changes: 29 additions & 0 deletions src/backend/database/factories/SmsAndroidSettingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Database\Factories;

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

class SmsAndroidSettingFactory extends Factory {
protected $model = SmsAndroidSetting::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'url' => $this->faker->url, // Generates a random URL
'token' => $this->faker->regexify('[A-Za-z0-9_-]{64}'), // Mimics a token format
'key' => $this->faker->regexify('[A-Za-z0-9:_-]{72}'), // Mimics a key format
'callback' => sprintf(
'https://cloud.micropowermanager.com/api/sms-android-callback/%s/confirm/11',
$this->faker->uuid
), // Generates a formatted callback URL with a UUID
'created_at' => now(), // Current timestamp
'updated_at' => now(), // Current timestamp
];
}
}
36 changes: 36 additions & 0 deletions src/backend/database/factories/SmsBodyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Database\Factories;

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

class SmsBodyFactory extends Factory {
protected $model = SmsBody::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'reference' => 'test ref',
'title' => 'Test title',
'body' => 'test body',
'place_holder' => 'test placeholder',
'variables' => 'variable',
'created_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
'updated_at' => $this->faker->dateTimeBetween($this->faker->dateTimeThisYear(), 'now'),
];
}

/**
* Define the specific data for seeding.
*
* @return static
*/
public function withCustomData($data) {
return $this->state($data);
}
}
69 changes: 69 additions & 0 deletions src/backend/database/factories/SmsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Database\Factories;

use App\Models\Sms;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class SmsFactory extends Factory {
protected $model = Sms::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
// Generate realistic Tanzanian names
$firstNames = ['Ambidwile', 'Samiha', 'Tatu', 'Sharifa', 'Manica', 'Seghen', 'Bamba',
'Bora', 'Hadiya', 'Fahima', 'Asiya', 'Asha', 'Adla', 'Hiba', 'Malika'];
$lastNames = ['Ngabile', 'Furaha', 'Amaziah', 'Sanaa', 'Asili', 'Adhra', 'Fadhili',
'Buyu', 'Sakina', 'Tumo', 'Ndweleifwa', 'Buyu', 'Amaziah', 'Saidi'];

// Generate realistic meter numbers
$meterNumber = '47000'.$this->faker->numberBetween(290000, 520000);

// Generate realistic transaction amounts (between 1000 and 20000)
$amount = $this->faker->numberBetween(1000, 20000);

// Calculate units based on amount (approximately amount/2)
$units = round($amount / 2, 1);

// Calculate VAT (1% of amount)
$vat = round($amount / 100, 2);

// Generate random token (32 characters)
$token = Str::random(32);

// Format Tanzanian phone number
$phone = '+255'.$this->faker->numberBetween(710000000, 789999999);

// Construct message body
$fullName = $this->faker->firstName().' '.$this->faker->lastName();
$body = sprintf(
'Dear %s, we received your transaction %d.Meter: %s, %s Unit %s .Transaction amount is %d, \n VAT for energy : %s \n VAT for the other staffs : 0 . Your Company etc.',
$fullName,
$amount,
$meterNumber,
$token,
$units,
$amount,
$vat
);

return [
'receiver' => $phone,
'trigger_type' => 0,
'trigger_id' => $this->faker->unique()->numberBetween(1, 10000),
'body' => $body,
'status' => 0,
'uuid' => $this->faker->uuid(),
'sender_id' => null,
'direction' => 1,
'gateway_id' => 1,
'created_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
'updated_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SmsResendInformationKeyFactory extends Factory {

public function definition() {
return [
'id' => $this->faker->numberBetween(1, 10),
'key' => 'Resend',
];
}
Expand Down
41 changes: 41 additions & 0 deletions src/backend/database/factories/SmsVariableDefaultValueFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Database\Factories;

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

class SmsVariableDefaultValueFactory extends Factory {
protected $model = SmsVariableDefaultValue::class;

protected $variables = [
'name' => ['Herbert', 'John', 'Maria', 'Sarah', 'Michael'],
'surname' => ['Kale', 'Smith', 'Johnson', 'Brown', 'Davis'],
'amount' => ['1000', '1500', '2000', '2500', '3000'],
'appliance_type_name' => ['fridge', 'tv', 'cooker', 'ac', 'washing machine'],
'remaining' => ['3', '4', '5', '6', '7'],
'due_date' => ['2021/04/01', '2021/05/01', '2021/06/01', '2021/07/01'],
'meter' => ['47782371232', '47782371233', '47782371234', '47782371235'],
'token' => ['5111 3511 9911 1177 7711', '5111 3511 9911 1177 7712'],
'vat_energy' => ['15', '16', '17', '18', '19'],
'vat_others' => ['10', '11', '12', '13', '14'],
'energy' => ['5123.1', '5124.2', '5125.3', '5126.4'],
'transaction_amount' => ['500', '600', '700', '800', '900'],
];

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
$variable = $this->faker->randomElement(array_keys($this->variables));

return [
'variable' => $variable,
'value' => $this->faker->randomElement($this->variables[$variable]),
'created_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
'updated_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
];
}
}
29 changes: 29 additions & 0 deletions src/backend/database/seeders/AgentBalanceHistorySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Database\Seeders;

use App\Models\Agent;
use App\Models\AgentBalanceHistory;
use Illuminate\Database\Seeder;
use MPM\DatabaseProxy\DatabaseProxyManagerService;

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

/**
* Run the database seeds.
*
* @return void
*/
public function run() {
$agents = Agent::all();

foreach ($agents as $agent) {
AgentBalanceHistory::factory()->create(['agent_id' => $agent->id]);
}
}
}
4 changes: 4 additions & 0 deletions src/backend/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function run() {
TicketSeeder::class,
TransactionSeeder::class,
CountrySeeder::class,
SubConnectionTypeSeeder::class,
SmsSeeder::class,
AgentBalanceHistorySeeder::class,
TargetSeeder::class,
]);
} else {
// If the database already includes the Demo data we don't throw an error,
Expand Down
31 changes: 31 additions & 0 deletions src/backend/database/seeders/SmsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Database\Seeders;

use App\Models\Sms;
use App\Models\SmsAndroidSetting;
use App\Models\SmsResendInformationKey;
use App\Models\SmsVariableDefaultValue;
use Illuminate\Database\Seeder;
use MPM\DatabaseProxy\DatabaseProxyManagerService;

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

/**
* Run the database seeds.
*
* @return void
*/
public function run() {
SmsVariableDefaultValue::factory()->count(12)->create();
SmsResendInformationKey::factory()->create();

SmsAndroidSetting::factory()->count(2)->create();
Sms::factory()->count(2000)->create();
}
}
Loading

0 comments on commit 4f9d8e9

Please sign in to comment.