Files
app-agent/database/seeders/LocalDevelopmentSeeder.php
2026-08-24 19:44:24 +03:30

154 lines
6.9 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\BazaarApiRequest;
use App\Models\BazaarPurchase;
use App\Models\BazaarRelease;
use App\Models\BazaarReview;
use App\Models\BazaarSubscription;
use App\Models\MobileApp;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LocalDevelopmentSeeder extends Seeder
{
public function run(): void
{
if (! app()->environment(['local', 'testing'])) {
$this->command?->warn('Local development data was not seeded outside the local or testing environment.');
return;
}
DB::transaction(fn () => $this->seedLocalData());
}
private function seedLocalData(): void
{
$packageNames = ['ir.local.cafe', 'ir.local.wallet'];
MobileApp::query()
->whereIn('package_name', $packageNames)
->get()
->each(fn (MobileApp $app) => $app->delete());
$ownerAttributes = User::factory()->make([
'name' => 'Local Admin',
'email' => 'admin@example.test',
'password' => 'password',
]);
$owner = User::query()->updateOrCreate(
['email' => $ownerAttributes->email],
$ownerAttributes->only(['name', 'password', 'email_verified_at']),
);
$cafeApp = MobileApp::factory()
->has(
BazaarPurchase::factory()->count(8)->sequence(
fn (Sequence $sequence): array => [
'product_id' => $sequence->index % 2 === 0 ? 'coffee_credits_100' : 'premium_theme',
'status' => $sequence->index === 6 ? 'refunded' : 'purchased',
'is_consumed' => $sequence->index % 3 === 0,
'purchased_at' => now()->subDays($sequence->index + 1),
],
),
'bazaarPurchases',
)
->has(
BazaarSubscription::factory()->count(6)->sequence(
['subscription_id' => 'monthly_pro', 'status' => 'active', 'valid_until' => now()->addMonth(), 'auto_renewing' => true],
['subscription_id' => 'yearly_pro', 'status' => 'active', 'valid_until' => now()->addYear(), 'auto_renewing' => true],
['subscription_id' => 'monthly_pro', 'status' => 'expired', 'valid_until' => now()->subMonth(), 'auto_renewing' => false],
['subscription_id' => 'yearly_pro', 'status' => 'cancelled', 'valid_until' => now()->addDays(12), 'auto_renewing' => false],
),
'bazaarSubscriptions',
)
->has(
BazaarRelease::factory()->count(3)->sequence(
['bazaar_release_id' => 1001, 'status' => 'draft'],
['bazaar_release_id' => 1002, 'status' => 'committed', 'committed_at' => now()->subWeek()],
['bazaar_release_id' => 1003, 'status' => 'published', 'committed_at' => now()->subMonth(), 'published_at' => now()->subWeeks(3)],
),
'bazaarReleases',
)
->has(
BazaarReview::factory()->count(12)->sequence(
fn (Sequence $sequence): array => [
'bazaar_review_id' => 100_000 + $sequence->index,
'rate' => ($sequence->index % 5) + 1,
'text' => "Local review number {$sequence->index}",
'app_version_code' => $sequence->index < 6 ? 10 : 11,
'published_at' => now()->subDays($sequence->index),
'developer_reply_text' => $sequence->index % 3 === 0 ? 'Thank you for your feedback.' : null,
'developer_replied_at' => $sequence->index % 3 === 0 ? now()->subDays($sequence->index)->addHour() : null,
],
),
'bazaarReviews',
)
->has(
BazaarApiRequest::factory()->count(15)->sequence(
fn (Sequence $sequence): array => [
'operation' => ['purchase.validate', 'subscription.show', 'reviews.list', 'release.last_published'][$sequence->index % 4],
'successful' => $sequence->index % 5 !== 0,
'status_code' => $sequence->index % 5 === 0 ? 502 : 200,
'duration_ms' => 80 + ($sequence->index * 17),
],
),
'bazaarApiRequests',
)
->create([
'name' => 'Local Cafe',
'package_name' => $packageNames[0],
'client_key' => 'local-cafe-client-key',
'management_key' => 'local-cafe-management-key',
'latest_version_name' => '1.1.0',
'latest_version_code' => 11,
'minimum_version_code' => 9,
'support_email' => 'support@example.test',
'configuration' => ['welcome_message' => 'Welcome to the local Cafe demo'],
'feature_flags' => ['new_checkout' => true, 'recommendations' => false],
'bazaar_sync_status' => 'succeeded',
'bazaar_last_synced_at' => now()->subMinutes(5),
]);
BazaarApiRequest::factory()
->for($cafeApp)
->create([
'operation' => 'subscription.show',
'successful' => false,
'status_code' => 400,
'error_message' => 'Seeded invalid subscription token response.',
]);
$walletApp = MobileApp::factory()
->has(BazaarPurchase::factory()->count(3), 'bazaarPurchases')
->has(BazaarSubscription::factory()->count(2), 'bazaarSubscriptions')
->has(BazaarRelease::factory()->count(1)->state(['status' => 'published', 'published_at' => now()->subDay()]), 'bazaarReleases')
->has(BazaarReview::factory()->count(4), 'bazaarReviews')
->has(BazaarApiRequest::factory()->count(5), 'bazaarApiRequests')
->create([
'name' => 'Local Wallet',
'package_name' => $packageNames[1],
'client_key' => 'local-wallet-client-key',
'management_key' => 'local-wallet-management-key',
'maintenance_mode' => true,
'latest_version_name' => '2.4.1',
'latest_version_code' => 241,
'minimum_version_code' => 230,
'feature_flags' => ['new_checkout' => false],
'bazaar_sync_status' => 'partial',
'bazaar_sync_error' => 'Seeded partial synchronization for UI testing.',
'bazaar_last_synced_at' => now()->subHour(),
]);
$owner->mobileApps()->syncWithoutDetaching([
$cafeApp->id => ['role' => 'owner'],
$walletApp->id => ['role' => 'owner'],
]);
}
}