139 lines
4.7 KiB
PHP
139 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Models\Invoice;
|
|
use App\Models\ModuleVmware;
|
|
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', [
|
|
'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', [
|
|
'id' => $moduleVmware->id,
|
|
'vmware_id' => $moduleVmware->vmware_id,
|
|
'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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle the ModuleVmware "deleted" event.
|
|
*/
|
|
public function deleted(ModuleVmware $moduleVmware): void
|
|
{
|
|
Log::info('ModuleVmware deleted', [
|
|
'id' => $moduleVmware->id,
|
|
'vmware_id' => $moduleVmware->vmware_id,
|
|
'name' => $moduleVmware->name,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handle the ModuleVmware "restored" event.
|
|
*/
|
|
public function restored(ModuleVmware $moduleVmware): void
|
|
{
|
|
Log::info('ModuleVmware restored', [
|
|
'id' => $moduleVmware->id,
|
|
'vmware_id' => $moduleVmware->vmware_id,
|
|
'name' => $moduleVmware->name,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handle the ModuleVmware "force deleted" event.
|
|
*/
|
|
public function forceDeleted(ModuleVmware $moduleVmware): void
|
|
{
|
|
Log::info('ModuleVmware force deleted', [
|
|
'id' => $moduleVmware->id,
|
|
'vmware_id' => $moduleVmware->vmware_id,
|
|
'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,
|
|
]);
|
|
}
|
|
}
|
|
}
|