-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathMarkCompedSubscriptions.php
More file actions
125 lines (91 loc) · 3.36 KB
/
MarkCompedSubscriptions.php
File metadata and controls
125 lines (91 loc) · 3.36 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
119
120
121
122
123
124
125
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use Laravel\Cashier\Subscription;
class MarkCompedSubscriptions extends Command
{
protected $signature = 'subscriptions:mark-comped
{file : Path to a CSV file containing email addresses (one per line or in an "email" column)}';
protected $description = 'Mark subscriptions as comped for email addresses in a CSV file';
public function handle(): int
{
$path = $this->argument('file');
if (! file_exists($path)) {
$this->error("File not found: {$path}");
return self::FAILURE;
}
$emails = $this->parseEmails($path);
if (empty($emails)) {
$this->error('No valid email addresses found in the file.');
return self::FAILURE;
}
$this->info('Found '.count($emails).' email(s) to process.');
$updated = 0;
$skipped = [];
foreach ($emails as $email) {
$user = User::where('email', $email)->first();
if (! $user) {
$skipped[] = "{$email} — user not found";
continue;
}
$subscription = Subscription::where('user_id', $user->id)
->where('stripe_status', 'active')
->first();
if (! $subscription) {
$skipped[] = "{$email} — no active subscription";
continue;
}
if ($subscription->is_comped) {
$skipped[] = "{$email} — already marked as comped";
continue;
}
$subscription->update(['is_comped' => true]);
$updated++;
$this->info("Marked {$email} as comped (subscription #{$subscription->id})");
}
if (count($skipped) > 0) {
$this->warn('Skipped:');
foreach ($skipped as $reason) {
$this->warn(" - {$reason}");
}
}
$this->info("Done. {$updated} subscription(s) marked as comped.");
return self::SUCCESS;
}
/**
* Parse email addresses from a CSV file.
* Supports: plain list (one email per line), or CSV with an "email" column header.
*
* @return array<string>
*/
private function parseEmails(string $path): array
{
$handle = fopen($path, 'r');
if (! $handle) {
return [];
}
$emails = [];
$emailColumnIndex = null;
$isFirstRow = true;
while (($row = fgetcsv($handle)) !== false) {
if ($isFirstRow) {
$isFirstRow = false;
$headers = array_map(fn ($h) => strtolower(trim($h)), $row);
$emailColumnIndex = array_search('email', $headers);
// If the first row looks like an email itself (no header), treat it as data
if ($emailColumnIndex === false && filter_var(trim($row[0]), FILTER_VALIDATE_EMAIL)) {
$emailColumnIndex = 0;
$emails[] = strtolower(trim($row[0]));
}
continue;
}
$value = trim($row[$emailColumnIndex] ?? '');
if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
$emails[] = strtolower($value);
}
}
fclose($handle);
return array_unique($emails);
}
}