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