Enhance VM service management: add service_id and plain_password fields, sync service records, and update related migrations

This commit is contained in:
pbao9
2026-04-16 09:39:52 +07:00
parent bb73d6c5a2
commit d68fda2cf0
11 changed files with 206 additions and 104 deletions
@@ -14,6 +14,7 @@ use App\Models\ModuleVmware;
use App\Models\ModuleIpAddress;
use App\Models\IpPool;
use App\Models\Log as CustomerLog;
use App\Models\Service;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -231,28 +232,33 @@ class CreateVirtualMachineJob implements ShouldQueue
$firstPayment = (int) $this->item->price * ($months ?: 1);
$nextDueDate = $months > 0 ? now()->addMonths($months)->timestamp : null;
$initialPassword = 'TGS@' . \Illuminate\Support\Str::random(8);
$moduleVmware = ModuleVmware::create([
'name' => $serverName,
'module_ip_address' => $moduleIpAddress->id,
'port' => 22,
'order_id' => $this->item->order->id,
'first_payment' => $firstPayment,
'next_payment' => $firstPayment,
'next_due_date' => $nextDueDate,
'next_invoice' => $nextDueDate,
'user' => 'administrator',
'password' => 'TGS@123',
'hostname' => $serverName,
'vmware_id' => $vmId,
'vcenter_id' => $vcenter->id,
'os_id' => $this->item->os_id ?? 1,
'customer_id' => $customer->id,
'status' => 1,
'name' => $serverName,
'module_ip_address' => $moduleIpAddress->id,
'port' => 22,
'order_id' => $this->item->order->id,
'first_payment' => $firstPayment,
'next_payment' => $firstPayment,
'next_due_date' => $nextDueDate,
'next_invoice' => $nextDueDate,
'user' => 'administrator',
'password' => $initialPassword,
'plain_password' => $initialPassword,
'hostname' => $serverName,
'vmware_id' => $vmId,
'vcenter_id' => $vcenter->id,
'os_id' => $this->item->os_id ?? 1,
'customer_id' => $customer->id,
'status' => 1,
]);
// Explicit sync: update services table directly (belt + suspenders vs observer)
$this->syncServiceRecord($moduleVmware);
AssignIPJob::dispatch($vmId, $moduleVmware->id, $moduleIpAddress->ip)
->delay(now()->addMinutes(5));
// Service record sẽ được tạo tự động bởi ModuleVmwareObserver::created()
} catch (\Exception $e) {
Log::error('❌ Lỗi khi lưu dữ liệu VM và IP address: ' . $e->getMessage(), [
'vmware_id' => $vmId,
@@ -459,4 +465,43 @@ class CreateVirtualMachineJob implements ShouldQueue
Log::error('Không thể gửi email thông báo lỗi VM: ' . $e->getMessage());
}
}
/**
* Sync module_vmware info → services table.
* Link: module_vmware.order_id → order_items.order_id → services.order_item_id
*/
private function syncServiceRecord(ModuleVmware $moduleVmware): void
{
try {
$orderItem = OrderItem::where('order_id', $moduleVmware->order_id)->first();
if (!$orderItem) {
Log::warning('syncServiceRecord: không tìm thấy OrderItem', [
'order_id' => $moduleVmware->order_id,
'module_vmware_id' => $moduleVmware->id,
]);
return;
}
$months = (int) ($orderItem->months ?? 0);
$updated = Service::where('order_item_id', $orderItem->id)
->update([
'vm_id' => $moduleVmware->id,
'status' => 'active',
'expires_at' => $months > 0 ? now()->addMonths($months) : null,
]);
Log::info('syncServiceRecord: service synced', [
'module_vmware_id' => $moduleVmware->id,
'vmware_id' => $moduleVmware->vmware_id,
'order_item_id' => $orderItem->id,
'rows_updated' => $updated,
]);
} catch (\Throwable $e) {
Log::error('syncServiceRecord: exception — ' . $e->getMessage(), [
'module_vmware_id' => $moduleVmware->id,
'order_id' => $moduleVmware->order_id,
]);
}
}
}