with(['hostingAccount.sites']) ->where(function ($q) { $q->where('domain_name', 'pending-setup.local') ->orWhereNull('domain_name') ->orWhere('domain_name', ''); }) ->whereIn('status', [ CustomerHostingOrder::STATUS_ACTIVE, CustomerHostingOrder::STATUS_SUSPENDED, CustomerHostingOrder::STATUS_PROVISIONING, ]) ->orderBy('id') ->get(); if ($orders->isEmpty()) { $this->info('No placeholder hosting orders found.'); return self::SUCCESS; } $changed = 0; foreach ($orders as $order) { $account = $order->hostingAccount; if (! $account && $order->user_id) { $account = HostingAccount::query() ->with('sites') ->where('user_id', $order->user_id) ->when($order->hosting_product_id, fn ($q) => $q->where('hosting_product_id', $order->hosting_product_id)) ->latest('id') ->first(); } $realDomain = null; if ($account) { if (! $account->relationLoaded('sites')) { $account->load('sites'); } $realDomain = $account->linkedDomainLabel(); if (PlaceholderPrimaryDomainService::isPlaceholderDomain($realDomain)) { $realDomain = null; } } $this->line(sprintf( '[order #%d] user=%s domain=%s account=%s → %s', $order->id, $order->user_id ?: '—', $order->domain_name ?: '(empty)', $account?->id ?: 'none', $realDomain ?: '(no real domain yet)' )); if ($this->option('dry-run')) { continue; } if ($account && $this->option('delete-duplicates') && (int) $order->hosting_account_id !== (int) $account->id) { // Separate Active order + separate HostingAccount = duplicate UX. $order->update([ 'hosting_account_id' => $order->hosting_account_id ?: $account->id, 'domain_name' => $realDomain ?: $order->domain_name, 'server_username' => $order->server_username ?: $account->username, 'status' => CustomerHostingOrder::STATUS_CANCELLED, 'cancelled_at' => now(), 'approval_notes' => trim(($order->approval_notes ? $order->approval_notes."\n" : ''). 'Auto-cancelled: superseded by Hosting account #'.$account->id), ]); $order->delete(); $this->info(' soft-deleted duplicate order'); $changed++; continue; } $updates = []; if ($account && ! $order->hosting_account_id) { $updates['hosting_account_id'] = $account->id; } if ($realDomain) { $updates['domain_name'] = $realDomain; } if ($account && ! $order->server_username) { $updates['server_username'] = $account->username; } if ($account?->node?->ip_address && ! $order->server_ip) { $account->loadMissing('node'); $updates['server_ip'] = $account->node?->ip_address; } if ($updates !== []) { $order->update($updates); $this->info(' updated: '.json_encode($updates)); $changed++; } } if ($this->option('dry-run')) { $this->warn('Dry run: no changes applied.'); } else { $this->info("Updated {$changed} order(s)."); } return self::SUCCESS; } }