|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Services\GiveawayEligibility; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | +use Illuminate\Support\Facades\Storage; |
| 9 | + |
| 10 | +class SeedGiveawayDemoCommand extends Command |
| 11 | +{ |
| 12 | + protected $signature = 'giveaway:seed-demo |
| 13 | + {--count=20 : Number of fake attendees to seed (ignored with --clear)} |
| 14 | + {--matches= : How many required vendors each attendee should hit (default: all)} |
| 15 | + {--clear : Remove all rows inserted by previous demo seeds}'; |
| 16 | + |
| 17 | + protected $description = 'Seed fake share_intents rows so /giveaway has data to demo (non-production only)'; |
| 18 | + |
| 19 | + private const TRACKING_FILE = 'giveaway-demo-seeded.json'; |
| 20 | + |
| 21 | + public function handle(GiveawayEligibility $eligibility): int |
| 22 | + { |
| 23 | + if (app()->environment('production')) { |
| 24 | + $this->error('Refusing to run in the production environment.'); |
| 25 | + |
| 26 | + return self::FAILURE; |
| 27 | + } |
| 28 | + |
| 29 | + return $this->option('clear') |
| 30 | + ? $this->clear() |
| 31 | + : $this->seed($eligibility); |
| 32 | + } |
| 33 | + |
| 34 | + private function seed(GiveawayEligibility $eligibility): int |
| 35 | + { |
| 36 | + $required = $eligibility->requiredVendorSlugs(); |
| 37 | + |
| 38 | + if ($required->isEmpty()) { |
| 39 | + $this->error('No required vendor slugs found. Check CONFERENCE_UUID and conference_sponsor rows on phptek_tv.'); |
| 40 | + |
| 41 | + return self::FAILURE; |
| 42 | + } |
| 43 | + |
| 44 | + $count = (int) $this->option('count'); |
| 45 | + $matchesOption = $this->option('matches'); |
| 46 | + $matches = $matchesOption !== null ? (int) $matchesOption : $required->count(); |
| 47 | + $matches = max(1, min($matches, $required->count())); |
| 48 | + |
| 49 | + $userUuids = DB::connection('phptek_tv') |
| 50 | + ->table('users') |
| 51 | + ->whereNotNull('uuid') |
| 52 | + ->inRandomOrder() |
| 53 | + ->limit($count) |
| 54 | + ->pluck('uuid'); |
| 55 | + |
| 56 | + if ($userUuids->isEmpty()) { |
| 57 | + $this->error('No users found on phptek_tv to seed against.'); |
| 58 | + |
| 59 | + return self::FAILURE; |
| 60 | + } |
| 61 | + |
| 62 | + $now = now(); |
| 63 | + $rows = []; |
| 64 | + |
| 65 | + foreach ($userUuids as $uuid) { |
| 66 | + $slugsForUser = $required->shuffle()->take($matches); |
| 67 | + |
| 68 | + foreach ($slugsForUser as $slug) { |
| 69 | + $rows[] = [ |
| 70 | + 'source_badge_uuid' => $uuid, |
| 71 | + 'target_type' => 'vendor', |
| 72 | + 'target_id' => $slug, |
| 73 | + 'created_at' => $now, |
| 74 | + 'updated_at' => $now, |
| 75 | + ]; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + $connection = DB::connection('mobile_app'); |
| 80 | + $insertedIds = []; |
| 81 | + |
| 82 | + $connection->transaction(function () use ($connection, $rows, &$insertedIds) { |
| 83 | + foreach ($rows as $row) { |
| 84 | + $insertedIds[] = $connection->table('share_intents')->insertGetId($row); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + $this->trackInserted($insertedIds); |
| 89 | + |
| 90 | + $this->info(sprintf( |
| 91 | + 'Seeded %d share_intents rows across %d attendees (matches=%d of %d required vendors).', |
| 92 | + count($insertedIds), |
| 93 | + $userUuids->count(), |
| 94 | + $matches, |
| 95 | + $required->count() |
| 96 | + )); |
| 97 | + $this->line('Run `php artisan giveaway:seed-demo --clear` to remove them.'); |
| 98 | + |
| 99 | + return self::SUCCESS; |
| 100 | + } |
| 101 | + |
| 102 | + private function clear(): int |
| 103 | + { |
| 104 | + $ids = $this->loadTrackedIds(); |
| 105 | + |
| 106 | + if (empty($ids)) { |
| 107 | + $this->info('No tracked demo rows to remove.'); |
| 108 | + |
| 109 | + return self::SUCCESS; |
| 110 | + } |
| 111 | + |
| 112 | + $deleted = DB::connection('mobile_app') |
| 113 | + ->table('share_intents') |
| 114 | + ->whereIn('id', $ids) |
| 115 | + ->delete(); |
| 116 | + |
| 117 | + Storage::disk('local')->delete(self::TRACKING_FILE); |
| 118 | + |
| 119 | + $this->info("Removed {$deleted} demo share_intents rows."); |
| 120 | + |
| 121 | + return self::SUCCESS; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param array<int, int> $newIds |
| 126 | + */ |
| 127 | + private function trackInserted(array $newIds): void |
| 128 | + { |
| 129 | + $existing = $this->loadTrackedIds(); |
| 130 | + $merged = array_values(array_unique([...$existing, ...$newIds])); |
| 131 | + |
| 132 | + Storage::disk('local')->put(self::TRACKING_FILE, json_encode($merged, JSON_PRETTY_PRINT)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return array<int, int> |
| 137 | + */ |
| 138 | + private function loadTrackedIds(): array |
| 139 | + { |
| 140 | + if (! Storage::disk('local')->exists(self::TRACKING_FILE)) { |
| 141 | + return []; |
| 142 | + } |
| 143 | + |
| 144 | + $raw = Storage::disk('local')->get(self::TRACKING_FILE); |
| 145 | + $decoded = json_decode($raw, true); |
| 146 | + |
| 147 | + return is_array($decoded) ? array_map('intval', $decoded) : []; |
| 148 | + } |
| 149 | +} |
0 commit comments