Skip to content

Commit

Permalink
convert plugins data dump to seeder (#467)
Browse files Browse the repository at this point in the history
* convert plugins data dump to seeder

* remove plugins sql data

* fix laravel lint errors
  • Loading branch information
beesaferoot authored Jan 28, 2025
1 parent c1815d5 commit bccebb0
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class CreateDummyCompanyWithData extends Command {
public const SQL_DUMMY_DATA_FILE_NAMES = [
'dummy_plugin_data.sql',
];
public const DUMMY_COMPANY_DATA = [
'name' => 'Dummy Company',
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Models;

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

class Plugins extends BaseModel {
use HasFactory;
public const ACTIVE = 1;
public const INACTIVE = 0;
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Inensus\CalinMeter\Models\CalinCredential;

class CalinCredentialFactory extends Factory {
protected $model = CalinCredential::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'api_url' => 'http://api.calinhost.com/api',
'user_id' => 'Inensus'.$this->faker->randomNumber(1, 100),
'api_key' => '123123',
];
}
}
23 changes: 23 additions & 0 deletions src/backend/database/factories/CsvDataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Inensus\BulkRegistration\Models\CsvData;

class CsvDataFactory extends Factory {
protected $model = CsvData::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'user_id' => $this->faker->randomNumber(1, 10),
'csv_filename' => 'bulk_registration'.$this->faker->randomNumber(1, 4).'csv',
'csv_data' => '',
];
}
}
22 changes: 22 additions & 0 deletions src/backend/database/factories/PluginsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Factories;

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

class PluginsFactory extends Factory {
protected $model = Plugins::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'mpm_plugin_id' => $this->faker->randomNumber(1, 15),
'status' => 1,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class SmsResendInformationKeyFactory extends Factory {

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

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Inensus\WaveMoneyPaymentProvider\Models\WaveMoneyCredential;

class WaveMoneyCredentialFactory extends Factory {
protected $model = WaveMoneyCredential::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'merchant_id' => 'MERCHANT ID '.$this->faker->randomNumber(1, 100),
'merchant_name' => 'Demo Merchant',
'secret_key' => '123123',
'callback_url' => 'https://staging.micropowermanager.com/api/wave-money/wave-money-transaction/callback/11',
'payment_url' => 'https://staging.micropowermanager.com/api/wave-money/payment/Demo Merchant/11',
'result_url' => 'https://staging.micropowermanager.com/api/wave-money/wave-money-transaction/result/Demo Merchant/11',
];
}
}
1 change: 1 addition & 0 deletions src/backend/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function run() {
SmsSeeder::class,
AgentBalanceHistorySeeder::class,
TargetSeeder::class,
PluginsSeeder::class,
]);
} else {
// If the database already includes the Demo data we don't throw an error,
Expand Down
70 changes: 70 additions & 0 deletions src/backend/database/seeders/PluginsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Database\Seeders;

use App\Models\Plugins;
use Database\Factories\CalinCredentialFactory;
use Database\Factories\CsvDataFactory;
use Database\Factories\WaveMoneyCredentialFactory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use MPM\DatabaseProxy\DatabaseProxyManagerService;

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

/**
* Run the database seeds.
*
* @return void
*/
public function run() {
$this->generatePlugins();
}

private function generatePlugins() {
Plugins::factory()->count(10)->create();
$this->generateBulkRegistrationCsvData();
$this->generateCalinCredential();
$this->generateWaveMoneyCredential();
}

private function generateBulkRegistrationCsvData() {
$factory = new CsvDataFactory();
$dummyDataPath = database_path('dummyData');

// Get the contents of the CSV files as arrays
$csvFiles = [
$dummyDataPath.'/bulk-registration-template-1.hex',
$dummyDataPath.'/bulk-registration-template-2.hex',
];
$csvData = [];

foreach ($csvFiles as $csvFile) {
if (file_exists($csvFile)) {
// Read the entire CSV file content as a string
$csvData[] = file_get_contents($csvFile);
} else {
throw new \Exception('CSV file not found: '.$csvFile);
}
}

foreach ($csvData as $data) {
$factory->create([
'csv_data' => DB::raw("X'{$data}'"),
]);
}
}

private function generateCalinCredential($count = 2) {
(new CalinCredentialFactory())->count($count)->create();
}

private function generateWaveMoneyCredential($count = 2) {
(new WaveMoneyCredentialFactory())->count($count)->create();
}
}

0 comments on commit bccebb0

Please sign in to comment.