Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify DemoData script and Ticket/Maintenance Seeders #420

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 22 additions & 44 deletions src/backend/app/Console/Commands/DemoDataCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ class DemoDataCreator extends AbstractSharedCommand {
AirtelTransaction::class,
];

public function __construct(
private Transaction $transaction,
private AgentTransaction $agentTransaction,
private WaveMoneyTransaction $waveMoneyTransaction,
private SwiftaTransaction $swiftaTransaction,
private WaveComTransaction $waveComTransaction,
private VodacomTransaction $vodacomTransaction,
private AirtelTransaction $airtelTransaction,
private Meter $meter,
private Token $token,
private MeterToken $meterToken,
private CalinTransaction $calinTransaction,
private MainSettings $mainSettings,
private TicketCategory $ticketCategory,
private User $user,
private TicketUser $ticketUser,
private Person $person,
private Ticket $ticket,
private MaintenanceUsers $maintenanceUsers,
private TicketOutsource $ticketOutsource,
) {
parent::__construct();
}

public function handle() {
$companyId = $this->option('company-id');
$type = $this->option('type') ?? 'transaction';
Expand All @@ -87,14 +63,15 @@ public function handle() {
} catch (\Exception $e) {
DB::connection('shard')->rollBack();
echo $e->getMessage();
throw $e;
}
}
}

private function generateTransaction(): void {
try {
// get randomly a user
$randomMeter = $this->meter::inRandomOrder()->with([
$randomMeter = Meter::inRandomOrder()->with([
'device',
'tariff',
])->limit(1)->firstOrFail();
Expand Down Expand Up @@ -127,7 +104,7 @@ private function generateTransaction(): void {
$randomTransactionType = $this->getTransactionTypeRandomlyFromTransactionTypes();
$transactionType = app()->make($randomTransactionType);

$transaction = $this->transaction->newQuery()->make([
$transaction = Transaction::query()->make([
'amount' => $amount,
'type' => 'energy',
'message' => $randomMeter['serial_number'],
Expand All @@ -137,13 +114,14 @@ private function generateTransaction(): void {
]);
$subTransaction = null;

$manufacturerTransaction = $this->calinTransaction->newQuery()->create([]);
// FIXME: What is this?
$manufacturerTransaction = CalinTransaction::query()->create([]);

if ($transactionType instanceof AgentTransaction) {
$city = $randomMeter->device->person->addresses()->first()->city()->first();
$miniGrid = $city->miniGrid()->first();
$agent = $miniGrid->agent()->first();
$subTransaction = $this->agentTransaction->newQuery()->create([
$subTransaction = AgentTransaction::query()->create([
'agent_id' => $agent->id,
'device_id' => 'test-device',
'status' => 1,
Expand All @@ -155,7 +133,7 @@ private function generateTransaction(): void {
}

if ($transactionType instanceof SwiftaTransaction) {
$subTransaction = $this->swiftaTransaction->newQuery()->create([
$subTransaction = SwiftaTransaction::query()->create([
'transaction_reference' => Str::random(10),
'status' => 1,
'amount' => $amount,
Expand All @@ -169,8 +147,8 @@ private function generateTransaction(): void {
}

if ($transactionType instanceof WaveMoneyTransaction) {
$mainSettings = $this->mainSettings->newQuery()->first();
$subTransaction = $this->waveMoneyTransaction->newQuery()->create([
$mainSettings = MainSettings::query()->first();
$subTransaction = WaveMoneyTransaction::query()->create([
'transaction_reference' => Str::random(10),
'status' => 1,
'amount' => $amount,
Expand All @@ -189,7 +167,7 @@ private function generateTransaction(): void {
}

if ($transactionType instanceof WaveComTransaction) {
$subTransaction = $this->waveComTransaction->newQuery()->create([
$subTransaction = WaveComTransaction::query()->create([
'transaction_id' => Str::random(10),
'sender' => $meterOwnerPhoneNumber['phone'],
'message' => $randomMeter['serial_number'],
Expand All @@ -203,7 +181,7 @@ private function generateTransaction(): void {
}

if ($transactionType instanceof VodacomTransaction) {
$subTransaction = $this->vodacomTransaction->newQuery()->create([
$subTransaction = VodacomTransaction::query()->create([
'conversation_id' => Str::random(20),
'originator_conversation_id' => Str::random(20),
'mpesa_receipt' => Str::random(10),
Expand All @@ -218,7 +196,7 @@ private function generateTransaction(): void {
}

if ($transactionType instanceof AirtelTransaction) {
$subTransaction = $this->airtelTransaction->newQuery()->create([
$subTransaction = AirtelTransaction::query()->create([
'interface_id' => Str::random(20),
'business_number' => Str::random(20),
'trans_id' => Str::random(10),
Expand All @@ -238,7 +216,7 @@ private function generateTransaction(): void {
// create an object for the token job
$transactionData = \App\Misc\TransactionDataContainer::initialize($transaction);
} catch (\Exception $exception) {
event('transaction.failed', [$this->transaction, $exception->getMessage()]);
event('transaction.failed', [$transaction, $exception->getMessage()]);
throw $exception;
}

Expand All @@ -265,7 +243,7 @@ private function generateTransaction(): void {
2
),
];
$token = $this->token->newQuery()->make([
$token = Token::query()->make([
'token' => $tokenData['token'],
'load' => $tokenData['load'],
]);
Expand All @@ -284,7 +262,7 @@ private function generateTransaction(): void {
),
'transaction_id' => $transaction->id,
];
$meterToken = $this->meterToken->newQuery()->make([
$meterToken = MeterToken::query()->make([
'meter_id' => $meterTokenData['meter_id'],
'token' => $meterTokenData['token'],
'energy' => $meterTokenData['energy'],
Expand Down Expand Up @@ -316,17 +294,17 @@ private function getTransactionTypeRandomlyFromTransactionTypes() {
}

private function generateTicket() {
$randomCategory = $this->ticketCategory->newQuery()->inRandomOrder()->first();
$randomCategory = TicketCategory::query()->inRandomOrder()->first();
$fakeSentence = $this->generateFakeSentence();
$randomCreator = $this->user->inRandomOrder()->first();
$randomCreator = User::inRandomOrder()->first();
$demoDate = date('Y-m-d', strtotime('-'.mt_rand(0, 365).' days'));
$ticketUser = $this->ticketUser->inRandomOrder()->first();
$randomMaintenanceUser = $this->maintenanceUsers->inRandomOrder()->first();
$randomPerson = $this->person->inRandomOrder()->where('is_customer', 1)->first();
$ticketUser = TicketUser::inRandomOrder()->first();
$randomMaintenanceUser = MaintenanceUsers::inRandomOrder()->first();
$randomPerson = Person::inRandomOrder()->where('is_customer', 1)->first();
$dueDate = date('Y-m-d', strtotime('+3 days', strtotime($demoDate)));
$status = rand(0, 1);

$ticket = $this->ticket->newQuery()->make([
$ticket = Ticket::query()->make([
'ticket_id' => Str::random(10),
'creator_type' => 'admin',
'creator_id' => $randomCreator->id,
Expand All @@ -349,7 +327,7 @@ private function generateTicket() {
} catch (\Exception $e) {
$amount = 50;
}
$this->ticketOutsource->newQuery()->create([
TicketOutsource::query()->create([
'ticket_id' => $ticket->id,
'amount' => $amount,
'created_at' => $demoDate,
Expand Down
3 changes: 3 additions & 0 deletions src/backend/app/Models/MaintenanceUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use App\Models\Base\BaseModel;
use App\Models\Person\Person;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Inensus\Ticket\Models\Ticket;

class MaintenanceUsers extends BaseModel {
use HasFactory;

public const RELATION_NAME = 'maintenance_user';

public function person(): BelongsTo {
Expand Down
Loading
Loading