feat: Implement UUID-based customer routing, enhance order request validation, and refactor VM creation job with updated payment logic and ModuleVmware handling.

This commit is contained in:
pbao9
2026-03-12 16:30:02 +07:00
parent 508d5186db
commit dbd3cdafcd
11 changed files with 207 additions and 283 deletions
@@ -14,7 +14,6 @@ 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;
@@ -160,7 +159,7 @@ class CreateVirtualMachineJob implements ShouldQueue
'template' => $template,
'addon' => $addonData,
]);
Log::info('CreateVirtualMachineJob - Result: ' . json_encode($result));
if (is_array($result) && ($result['error'] ?? false)) {
$errorMessage = 'Tạo máy ảo thất bại: ' . json_encode($result);
Log::warning($errorMessage);
@@ -202,22 +201,14 @@ class CreateVirtualMachineJob implements ShouldQueue
if ($vmId !== 'unknown' && $vmId !== 'UNKNOWN') {
try {
// Lưu vm_id vào bảng invoice
$invoice = \App\Models\Invoice::where('order_id', $this->item->order->id)->first();
if ($invoice) {
$invoice->update(['vm_id' => $vmId]);
}
// Start VM ngay sau khi tạo thành công (sử dụng API trực tiếp)
// vm_id được lưu vào tbl.services qua ModuleVmwareObserver::created()
$startResult = $this->startVirtualMachineDirectly($vCenter, $vmId);
if (is_array($startResult) && $startResult['error'] === false) {
// Dispatch ConfigCLICommand để cấu hình VM và lấy password
$configJob = new ConfigCLICommand($vmId, $this->item, $vCenter);
dispatch($configJob)->delay(now()->addMinutes(2)); // Chờ 2 phút để VM boot hoàn toàn
}
// Lưu dữ liệu VM vào database
try {
$customer = $this->item->order->customer;
$serverName = $this->item->server_name ?? 'VM-' . $vmId;
@@ -236,42 +227,32 @@ class CreateVirtualMachineJob implements ShouldQueue
'status' => 1 // 1 = Assigned
]);
$months = (int) ($this->item->months ?? 0);
$firstPayment = (int) $this->item->price * ($months ?: 1);
$nextDueDate = $months > 0 ? now()->addMonths($months)->timestamp : null;
$moduleVmware = ModuleVmware::create([
'name' => $serverName,
'module_ip_address_id' => $moduleIpAddress->id,
'module_ip_address' => $moduleIpAddress->id,
'port' => 22,
'order_id' => $this->item->order->id,
'first_payment' => $this->item->order->first_payment ?? 0,
'next_payment' => $this->item->order->next_payment ?? 0,
'next_due_date' => $this->item->order->next_due_date ?? null,
'next_invoice' => $this->item->order->next_invoice ?? 0,
'user' => 'Administrator',
'password' => '',
'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
'status' => 1,
]);
AssignIPJob::dispatch($vmId, $moduleVmware->id, $moduleIpAddress->ip)
->delay(now()->addMinutes(5));
// Lưu Service record để track dịch vụ của khách hàng
Service::create([
'customer_id' => $customer->id,
'product_id' => $this->item->product_id,
'order_item_id' => $this->item->id,
'invoice_id' => $invoice?->id,
'vm_id' => $vmId,
'server_name' => $serverName,
'status' => 'active',
'auto_renew' => false,
'started_at' => now(),
'expires_at' => $this->item->months > 0
? now()->addMonths($this->item->months)
: null,
]);
// 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,