|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Enums\Subscription; |
| 6 | +use App\Models\License; |
| 7 | +use App\Notifications\UltraLicenseHolderPromotion; |
| 8 | +use Illuminate\Console\Command; |
| 9 | + |
| 10 | +class SendUltraLicenseHolderPromotion extends Command |
| 11 | +{ |
| 12 | + protected $signature = 'ultra:send-license-holder-promo |
| 13 | + {--dry-run : Show what would be sent without actually sending}'; |
| 14 | + |
| 15 | + protected $description = 'Send a promotional email to license holders without an active subscription encouraging them to subscribe to Ultra'; |
| 16 | + |
| 17 | + public function handle(): int |
| 18 | + { |
| 19 | + $dryRun = $this->option('dry-run'); |
| 20 | + |
| 21 | + if ($dryRun) { |
| 22 | + $this->info('DRY RUN - No emails will be sent'); |
| 23 | + } |
| 24 | + |
| 25 | + $legacyLicenses = License::query() |
| 26 | + ->whereNull('subscription_item_id') |
| 27 | + ->whereHas('user') |
| 28 | + ->with('user') |
| 29 | + ->get(); |
| 30 | + |
| 31 | + // Group by user to avoid sending multiple emails to the same person |
| 32 | + $userLicenses = $legacyLicenses->groupBy('user_id'); |
| 33 | + |
| 34 | + $eligible = 0; |
| 35 | + $skipped = 0; |
| 36 | + |
| 37 | + foreach ($userLicenses as $userId => $licenses) { |
| 38 | + $user = $licenses->first()->user; |
| 39 | + |
| 40 | + if (! $user) { |
| 41 | + $skipped++; |
| 42 | + |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + // Skip users who already have an active subscription |
| 47 | + $hasActiveSubscription = $user->subscriptions() |
| 48 | + ->where('stripe_status', 'active') |
| 49 | + ->exists(); |
| 50 | + |
| 51 | + if ($hasActiveSubscription) { |
| 52 | + $this->line("Skipping {$user->email} - already has active subscription"); |
| 53 | + $skipped++; |
| 54 | + |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $license = $licenses->sortBy('created_at')->first(); |
| 59 | + $planName = Subscription::from($license->policy_name)->name(); |
| 60 | + |
| 61 | + if ($dryRun) { |
| 62 | + $this->line("Would send to: {$user->email} ({$planName})"); |
| 63 | + } else { |
| 64 | + $user->notify(new UltraLicenseHolderPromotion($planName)); |
| 65 | + $this->line("Sent to: {$user->email} ({$planName})"); |
| 66 | + } |
| 67 | + |
| 68 | + $eligible++; |
| 69 | + } |
| 70 | + |
| 71 | + $this->newLine(); |
| 72 | + $this->info("Found {$eligible} eligible license holder(s)"); |
| 73 | + $this->info($dryRun ? "Would send: {$eligible} email(s)" : "Sent: {$eligible} email(s)"); |
| 74 | + $this->info("Skipped: {$skipped} user(s)"); |
| 75 | + |
| 76 | + return Command::SUCCESS; |
| 77 | + } |
| 78 | +} |
0 commit comments