88 lines
3.2 KiB
PHP
Executable File
88 lines
3.2 KiB
PHP
Executable File
<?php
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Amministratore;
|
|
use App\Models\Stabile;
|
|
use App\Services\TenantArchiveRegistryService;
|
|
use Illuminate\Console\Command;
|
|
|
|
class NetgesconArchiveRegistrySyncCommand extends Command
|
|
{
|
|
protected $signature = 'netgescon:archive-registry-sync
|
|
{--amministratore= : Codice amministratore canonico o univoco}
|
|
{--stabile= : Codice stabile da riallineare}
|
|
{--dry-run : Mostra i record senza salvarli}';
|
|
|
|
protected $description = 'Allinea il registry centrale degli archivi tenant-aware per amministratori e stabili';
|
|
|
|
public function __construct(private TenantArchiveRegistryService $registryService)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function handle(): int
|
|
{
|
|
$adminFilter = trim((string) $this->option('amministratore'));
|
|
$stabileFilter = trim((string) $this->option('stabile'));
|
|
$dryRun = (bool) $this->option('dry-run');
|
|
|
|
$amministratori = Amministratore::query()
|
|
->when($adminFilter !== '', function ($query) use ($adminFilter) {
|
|
$query->where('codice_amministratore', $adminFilter)
|
|
->orWhere('codice_univoco', $adminFilter);
|
|
})
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
if ($amministratori->isEmpty()) {
|
|
$this->error('Nessun amministratore trovato per i criteri richiesti.');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$stabili = Stabile::query()
|
|
->with(['amministratore', 'latestAmministratoreTransfer'])
|
|
->whereIn('amministratore_id', $amministratori->pluck('id'))
|
|
->when($stabileFilter !== '', fn($query) => $query->where('codice_stabile', $stabileFilter))
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
$records = [];
|
|
|
|
foreach ($amministratori as $amministratore) {
|
|
$records[] = $dryRun
|
|
? $this->registryService->buildAmministratorePayload($amministratore)
|
|
: $this->registryService->syncAmministratore($amministratore)->toArray();
|
|
}
|
|
|
|
foreach ($stabili as $stabile) {
|
|
$records[] = $dryRun
|
|
? $this->registryService->buildStabilePayload($stabile)
|
|
: $this->registryService->syncStabile($stabile)->toArray();
|
|
}
|
|
|
|
$this->info(sprintf(
|
|
'%s registry completato: %d amministratori, %d stabili.',
|
|
$dryRun ? 'Dry-run' : 'Sync',
|
|
$amministratori->count(),
|
|
$stabili->count()
|
|
));
|
|
|
|
$this->table(
|
|
['Tipo', 'Codice', 'Owner', 'Database', 'Storage', 'Stato'],
|
|
collect($records)->map(function (array $record) {
|
|
return [
|
|
'tipo' => $record['archive_type'] ?? '-',
|
|
'codice' => $record['archive_code'] ?? '-',
|
|
'owner' => $record['owner_amministratore_code'] ?? '-',
|
|
'database' => $record['database_name'] ?? ($record['database_strategy'] ?? '-'),
|
|
'storage' => $record['storage_relative_path'] ?? '-',
|
|
'stato' => $record['status'] ?? '-',
|
|
];
|
|
})->all()
|
|
);
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|