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:
@@ -4,37 +4,55 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\ModuleIpAddress;
|
||||
use App\Models\OrderItem;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ModuleVmwareObserver
|
||||
{
|
||||
/**
|
||||
* Handle the ModuleVmware "created" event.
|
||||
* Fired by CreateVirtualMachineJob khi tạo bản ghi mới sau khi VM được provisioned.
|
||||
*/
|
||||
public function created(ModuleVmware $moduleVmware): void
|
||||
{
|
||||
Log::info('✅ ModuleVmware created', [
|
||||
'vm_id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'name' => $moduleVmware->name,
|
||||
'order_id' => $moduleVmware->order_id,
|
||||
'customer_id' => $moduleVmware->customer_id
|
||||
Log::info('ModuleVmware created', [
|
||||
'id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'name' => $moduleVmware->name,
|
||||
'order_id' => $moduleVmware->order_id,
|
||||
'customer_id' => $moduleVmware->customer_id,
|
||||
]);
|
||||
|
||||
if ($moduleVmware->vmware_id && $moduleVmware->order_id) {
|
||||
$this->syncServiceRecord($moduleVmware);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the ModuleVmware "updated" event.
|
||||
* Fired by CreateVMJobs khi update vmware_id vào bản ghi đã tồn tại.
|
||||
*/
|
||||
public function updated(ModuleVmware $moduleVmware): void
|
||||
{
|
||||
Log::info('🔄 ModuleVmware updated', [
|
||||
'vm_id' => $moduleVmware->id,
|
||||
Log::info('ModuleVmware updated', [
|
||||
'id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'name' => $moduleVmware->name,
|
||||
'status' => $moduleVmware->status
|
||||
'name' => $moduleVmware->name,
|
||||
'status' => $moduleVmware->status,
|
||||
]);
|
||||
|
||||
// Chỉ tạo Service khi vmware_id vừa được set (chuyển từ null → có giá trị)
|
||||
if (
|
||||
$moduleVmware->wasChanged('vmware_id')
|
||||
&& $moduleVmware->vmware_id
|
||||
&& !$moduleVmware->getOriginal('vmware_id')
|
||||
&& $moduleVmware->order_id
|
||||
) {
|
||||
$this->syncServiceRecord($moduleVmware);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,10 +60,10 @@ class ModuleVmwareObserver
|
||||
*/
|
||||
public function deleted(ModuleVmware $moduleVmware): void
|
||||
{
|
||||
Log::info('🗑️ ModuleVmware deleted', [
|
||||
'vm_id' => $moduleVmware->id,
|
||||
Log::info('ModuleVmware deleted', [
|
||||
'id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'name' => $moduleVmware->name
|
||||
'name' => $moduleVmware->name,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -54,10 +72,10 @@ class ModuleVmwareObserver
|
||||
*/
|
||||
public function restored(ModuleVmware $moduleVmware): void
|
||||
{
|
||||
Log::info('🔄 ModuleVmware restored', [
|
||||
'vm_id' => $moduleVmware->id,
|
||||
Log::info('ModuleVmware restored', [
|
||||
'id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'name' => $moduleVmware->name
|
||||
'name' => $moduleVmware->name,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -66,10 +84,55 @@ class ModuleVmwareObserver
|
||||
*/
|
||||
public function forceDeleted(ModuleVmware $moduleVmware): void
|
||||
{
|
||||
Log::info('🗑️ ModuleVmware force deleted', [
|
||||
'vm_id' => $moduleVmware->id,
|
||||
Log::info('ModuleVmware force deleted', [
|
||||
'id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'name' => $moduleVmware->name
|
||||
'name' => $moduleVmware->name,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Tạo hoặc cập nhật bản ghi Service tương ứng với VM vừa được provisioned.
|
||||
* Dùng chung cho cả created và updated event.
|
||||
*/
|
||||
private function syncServiceRecord(ModuleVmware $moduleVmware): void
|
||||
{
|
||||
try {
|
||||
$orderItem = OrderItem::where('order_id', $moduleVmware->order_id)
|
||||
->where('os_id', $moduleVmware->os_id)
|
||||
->first();
|
||||
|
||||
$invoice = Invoice::where('order_id', $moduleVmware->order_id)->first();
|
||||
|
||||
Service::firstOrCreate(
|
||||
['order_item_id' => $orderItem?->id],
|
||||
[
|
||||
'customer_id' => $moduleVmware->customer_id,
|
||||
'product_id' => $orderItem?->product_id,
|
||||
'invoice_id' => $invoice?->id,
|
||||
'vm_id' => $moduleVmware->vmware_id,
|
||||
'server_name' => $moduleVmware->hostname ?? $moduleVmware->name,
|
||||
'status' => 'active',
|
||||
'auto_renew' => false,
|
||||
'started_at' => now(),
|
||||
'expires_at' => ($orderItem && (int) $orderItem->months > 0)
|
||||
? now()->addMonths((int) $orderItem->months)
|
||||
: null,
|
||||
]
|
||||
);
|
||||
|
||||
Log::info('Service record synced for ModuleVmware', [
|
||||
'module_vmware_id' => $moduleVmware->id,
|
||||
'vmware_id' => $moduleVmware->vmware_id,
|
||||
'order_item_id' => $orderItem?->id,
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Lỗi khi tạo Service từ ModuleVmwareObserver: ' . $e->getMessage(), [
|
||||
'module_vmware_id' => $moduleVmware->id,
|
||||
'order_id' => $moduleVmware->order_id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user