|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Models\Plugin; |
| 6 | +use App\Models\PluginBundle; |
| 7 | +use App\Models\PluginLicense; |
| 8 | +use App\Models\User; |
| 9 | +use App\Notifications\BundlePluginAdded; |
| 10 | +use Illuminate\Console\Command; |
| 11 | + |
| 12 | +class GrantPluginToBundleOwners extends Command |
| 13 | +{ |
| 14 | + protected $signature = 'plugins:grant-to-bundle-owners |
| 15 | + {bundle : The bundle slug} |
| 16 | + {plugin : The plugin name (vendor/package)} |
| 17 | + {--dry-run : Preview what would happen without making changes} |
| 18 | + {--no-email : Grant the plugin without sending notification emails}'; |
| 19 | + |
| 20 | + protected $description = 'Grant a plugin to all users who have purchased a specific bundle and notify them via email'; |
| 21 | + |
| 22 | + public function handle(): int |
| 23 | + { |
| 24 | + $bundle = PluginBundle::where('slug', $this->argument('bundle'))->first(); |
| 25 | + |
| 26 | + if (! $bundle) { |
| 27 | + $this->error("Bundle not found: {$this->argument('bundle')}"); |
| 28 | + |
| 29 | + return Command::FAILURE; |
| 30 | + } |
| 31 | + |
| 32 | + $plugin = Plugin::where('name', $this->argument('plugin'))->first(); |
| 33 | + |
| 34 | + if (! $plugin) { |
| 35 | + $this->error("Plugin not found: {$this->argument('plugin')}"); |
| 36 | + |
| 37 | + return Command::FAILURE; |
| 38 | + } |
| 39 | + |
| 40 | + $dryRun = $this->option('dry-run'); |
| 41 | + $noEmail = $this->option('no-email'); |
| 42 | + |
| 43 | + // Find all unique users who have purchased this bundle |
| 44 | + // (they have at least one active PluginLicense linked to this bundle) |
| 45 | + $userIds = PluginLicense::where('plugin_bundle_id', $bundle->id) |
| 46 | + ->active() |
| 47 | + ->distinct() |
| 48 | + ->pluck('user_id'); |
| 49 | + |
| 50 | + $users = User::whereIn('id', $userIds)->get(); |
| 51 | + |
| 52 | + if ($users->isEmpty()) { |
| 53 | + $this->warn('No users found who have purchased this bundle.'); |
| 54 | + |
| 55 | + return Command::SUCCESS; |
| 56 | + } |
| 57 | + |
| 58 | + $this->info("Bundle: {$bundle->name} (slug: {$bundle->slug})"); |
| 59 | + $this->info("Plugin: {$plugin->name}"); |
| 60 | + $this->info("Users found: {$users->count()}"); |
| 61 | + |
| 62 | + if ($dryRun) { |
| 63 | + $this->warn('[DRY RUN] No changes will be made.'); |
| 64 | + } |
| 65 | + |
| 66 | + $this->newLine(); |
| 67 | + |
| 68 | + $granted = 0; |
| 69 | + $skipped = 0; |
| 70 | + |
| 71 | + foreach ($users as $user) { |
| 72 | + // Check if user already has an active license for this plugin |
| 73 | + $existingLicense = PluginLicense::where('user_id', $user->id) |
| 74 | + ->where('plugin_id', $plugin->id) |
| 75 | + ->active() |
| 76 | + ->exists(); |
| 77 | + |
| 78 | + if ($existingLicense) { |
| 79 | + $this->line(" Skipped {$user->email} — already has an active license"); |
| 80 | + $skipped++; |
| 81 | + |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + if (! $dryRun) { |
| 86 | + PluginLicense::create([ |
| 87 | + 'user_id' => $user->id, |
| 88 | + 'plugin_id' => $plugin->id, |
| 89 | + 'plugin_bundle_id' => $bundle->id, |
| 90 | + 'price_paid' => 0, |
| 91 | + 'currency' => 'USD', |
| 92 | + 'is_grandfathered' => true, |
| 93 | + 'purchased_at' => now(), |
| 94 | + ]); |
| 95 | + |
| 96 | + if (! $noEmail) { |
| 97 | + $user->notify( |
| 98 | + (new BundlePluginAdded($plugin, $bundle)) |
| 99 | + ->delay(now()->addSeconds($granted * 2)) |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + $this->line(" Granted to {$user->email}"); |
| 105 | + $granted++; |
| 106 | + } |
| 107 | + |
| 108 | + $this->newLine(); |
| 109 | + $this->info("Granted: {$granted}"); |
| 110 | + $this->info("Skipped (already licensed): {$skipped}"); |
| 111 | + |
| 112 | + if ($dryRun) { |
| 113 | + $this->warn('This was a dry run. Run again without --dry-run to apply changes.'); |
| 114 | + } |
| 115 | + |
| 116 | + return Command::SUCCESS; |
| 117 | + } |
| 118 | +} |
0 commit comments