54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\License;
|
|
use App\Services\Payments\CustomerAccountProvisioner;
|
|
use Illuminate\Console\Command;
|
|
|
|
final class SonarDeskAccountsBackfill extends Command
|
|
{
|
|
protected $signature = 'sonardesk:accounts:backfill {--dry-run : Show missing customer accounts without creating them}';
|
|
|
|
protected $description = 'Create portal accounts for existing licence customers that do not yet have a user account';
|
|
|
|
public function handle(CustomerAccountProvisioner $accounts): int
|
|
{
|
|
$customers = License::query()
|
|
->whereNotNull('customer_email')
|
|
->where('customer_email', '!=', '')
|
|
->orderBy('id')
|
|
->get(['customer_email', 'customer_name'])
|
|
->unique(fn (License $license) => strtolower(trim((string) $license->customer_email)));
|
|
|
|
$created = 0;
|
|
|
|
foreach ($customers as $license) {
|
|
$email = strtolower(trim((string) $license->customer_email));
|
|
$exists = \App\Models\User::whereRaw('lower(email) = ?', [$email])->exists();
|
|
if ($exists) {
|
|
continue;
|
|
}
|
|
|
|
if ($this->option('dry-run')) {
|
|
$this->line("Missing account: {$email}");
|
|
$created++;
|
|
continue;
|
|
}
|
|
|
|
$user = $accounts->ensure($email, $license->customer_name);
|
|
if ($user) {
|
|
$this->info("Created account: {$email}");
|
|
$created++;
|
|
}
|
|
}
|
|
|
|
$this->newLine();
|
|
$this->info($this->option('dry-run')
|
|
? "{$created} customer account(s) need creating."
|
|
: "{$created} customer account(s) created.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|