Files
portal-api/app/Console/Commands/SyncLegacyPortal.php
2026-06-15 09:11:57 +07:00

57 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\Imports\LegacyPortalImporter;
use Illuminate\Console\Command;
class SyncLegacyPortal extends Command
{
protected $signature = 'legacy:sync-portal
{--preview : Count and validate only, no writes}
{--default-product=1 : Fallback portal.products.id used when legacy product is not mapped}
{--map=* : Explicit legacy→new product map, format "legacyId:newId" (repeatable)}';
protected $description = 'Sync customers + services from legacy `portal` DB (connection: legacy) into current schema';
public function handle(LegacyPortalImporter $importer): int
{
$preview = (bool) $this->option('preview');
$importer->setDefaultVpsProductId((int) $this->option('default-product'));
$map = [];
foreach ((array) $this->option('map') as $pair) {
if (! str_contains($pair, ':')) continue;
[$legacy, $new] = explode(':', $pair, 2);
$map[(int) $legacy] = (int) $new;
}
if ($map) {
$importer->setProductMap($map);
$this->info('Product map: ' . json_encode($map));
}
if (! $preview && ! $this->confirm('This will write to the current DB. Continue?', true)) {
$this->warn('Aborted.');
return self::SUCCESS;
}
try {
$summary = $importer->import(
preview: $preview,
log: fn ($msg) => $this->line($msg),
);
} catch (\Throwable $e) {
$this->error($e->getMessage());
return self::FAILURE;
}
$this->newLine();
$this->info('=== Summary ===');
foreach ($summary as $section => $stats) {
$this->line(str_pad(ucfirst($section), 12) . ': ' . json_encode($stats));
}
return self::SUCCESS;
}
}