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

bug fixes: Ensure agent transactions exists while seeder #477

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/backend/app/Models/Meter/MeterToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\PaymentHistory;
use App\Models\Transaction\Transaction;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Facades\DB;
Expand All @@ -17,6 +18,8 @@
* @property float $energy
*/
class MeterToken extends BaseModel {
use HasFactory;

public const RELATION_NAME = 'meter_token';

public function transaction(): BelongsTo {
Expand Down
3 changes: 3 additions & 0 deletions src/backend/app/Models/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

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\MorphOne;

class Token extends BaseModel {
use HasFactory;

public const RELATION_NAME = 'token';

public function transaction(): BelongsTo {
Expand Down
2 changes: 2 additions & 0 deletions src/backend/app/Models/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\Sms;
use App\Models\Token;
use App\Relations\BelongsToMorph;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphOne;
Expand All @@ -28,6 +29,7 @@
* @property string $original_transaction_type
*/
class Transaction extends BaseModel {
use HasFactory;
use RelationsManager;

public const RELATION_NAME = 'transaction';
Expand Down
21 changes: 20 additions & 1 deletion src/backend/database/seeders/AgentBalanceHistorySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand All @@ -23,7 +24,25 @@ public function run() {
$agents = Agent::all();

foreach ($agents as $agent) {
AgentBalanceHistory::factory()->create(['agent_id' => $agent->id]);
// Fetch all transactions for this agent
$transactions = AgentTransaction::where('agent_id', $agent->id)->pluck('id')->toArray();

// If no transaction exists, create a default one
if (empty($transactions)) {
$transaction = AgentTransaction::create([
'agent_id' => $agent->id,
'status' => 1,
'sender' => 'System',
]);
$transactions = [$transaction->id];
}

for ($i = 0; $i < 30; ++$i) {
AgentBalanceHistory::factory()->create([
'agent_id' => $agent->id,
'transaction_id' => fake()->randomElement($transactions),
]);
}
}
}
}
Loading