feat: implement JSON-based IP configuration scripts for Windows and Linux and integrate service activation in provisioning flow
This commit is contained in:
@@ -9,22 +9,24 @@ use App\Jobs\Mail\Admin\AdminOrderNotificationJob;
|
||||
use App\Jobs\Mail\SendInformationVirtualMachine;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\Service;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* SendVmCredentialsEmailJob
|
||||
*
|
||||
* Sends the final VM credential email to the customer with an attached PDF invoice.
|
||||
* Always loads fresh data from DB so it gets the real IP and
|
||||
* the final password saved by ChangePasswordJob.
|
||||
*
|
||||
* This is step [11] in the provisioning flow.
|
||||
* Final step in the VM provisioning chain:
|
||||
* [1] Activate service: services.status → 'active', link services.vm_id → module_vmware.id
|
||||
* [2] Activate VM: module_vmware.status → 1, link module_vmware.service_id → services.id
|
||||
* [3] Send credential email to customer with PDF invoice
|
||||
* [4] Notify admin via AdminOrderNotificationJob
|
||||
*/
|
||||
class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
{
|
||||
@@ -39,7 +41,11 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
|
||||
public function handle(Telegram $telegram): void
|
||||
{
|
||||
$vm = ModuleVmware::with(['customer', 'os.osGroup', 'order.orderItems'])->find($this->moduleVmwareId);
|
||||
$vm = ModuleVmware::with([
|
||||
'customer',
|
||||
'os.osGroup',
|
||||
'order.orderItems.service',
|
||||
])->find($this->moduleVmwareId);
|
||||
|
||||
if (!$vm) {
|
||||
$msg = "SendVmCredentialsEmailJob: ModuleVmware #{$this->moduleVmwareId} not found";
|
||||
@@ -57,13 +63,19 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
// ── [1 & 2] Activate service + VM records ────────────────────────────
|
||||
|
||||
$orderItem = $vm->order?->orderItems?->first();
|
||||
$service = $orderItem?->service;
|
||||
|
||||
$this->activateService($vm, $service);
|
||||
|
||||
// ── [3] Build email payload ───────────────────────────────────────────
|
||||
|
||||
$setting = Setting::where('key', 'setting')->first();
|
||||
$settingValue = $setting ? json_decode((string) $setting->value) : null;
|
||||
|
||||
// Resolve billing info from order items
|
||||
$orderItem = $vm->order?->orderItems?->first();
|
||||
$months = (int) ($orderItem?->months ?? 0);
|
||||
|
||||
$months = (int) ($orderItem?->months ?? 0);
|
||||
$osGroupName = $vm->os?->osGroup?->name ?? '';
|
||||
$isWindows = str_contains(strtolower($osGroupName), 'windows');
|
||||
$osUsername = $isWindows ? 'Administrator' : 'root';
|
||||
@@ -80,15 +92,14 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
'service_port' => $vm->port,
|
||||
'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)',
|
||||
'os_name' => $vm->os
|
||||
? (($osGroupName . ' ' . $vm->os->version))
|
||||
? ($osGroupName . ' ' . $vm->os->version)
|
||||
: 'Unknown OS',
|
||||
'service_due_exp' => $vm->next_due_date
|
||||
? date('d/m/Y', (int) $vm->next_due_date)
|
||||
: 'N/A',
|
||||
];
|
||||
|
||||
// Resolve invoice ID — PDF is generated inside SendInformationVirtualMachine::handle()
|
||||
// to avoid passing large binary blobs through the queue payload
|
||||
// PDF is generated inside the mail job — never pass binary blobs through queue payload
|
||||
$invoice = Invoice::where('order_id', $vm->order_id)->first();
|
||||
|
||||
SendInformationVirtualMachine::dispatch($emailData, $customer, $invoice?->id);
|
||||
@@ -97,10 +108,11 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
'module_vmware_id' => $this->moduleVmwareId,
|
||||
'customer_email' => $customer->email,
|
||||
'ip' => $vm->ip,
|
||||
'invoice_id' => $invoice?->id,
|
||||
'invoice_id' => $invoice?->id,
|
||||
]);
|
||||
|
||||
// Notify admin with VM credentials after successful provisioning
|
||||
// ── [4] Admin notification ────────────────────────────────────────────
|
||||
|
||||
$order = $vm->order;
|
||||
AdminOrderNotificationJob::dispatch([
|
||||
'customer_name' => $customer->fullname ?? '',
|
||||
@@ -142,7 +154,46 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Activate the Service record and link it bidirectionally with ModuleVmware.
|
||||
*
|
||||
* services: status → 'active', vm_id → module_vmware.id
|
||||
* module_vmware: status → 1, service_id → services.id
|
||||
*/
|
||||
private function activateService(ModuleVmware $vm, ?Service $service): void
|
||||
{
|
||||
DB::transaction(function () use ($vm, $service) {
|
||||
if ($service) {
|
||||
$service->update([
|
||||
'status' => 'active',
|
||||
'vm_id' => $vm->id,
|
||||
'server_name' => $service->server_name ?: ($vm->hostname ?? $vm->name),
|
||||
]);
|
||||
|
||||
$vm->update([
|
||||
'status' => 1,
|
||||
'service_id' => $service->id,
|
||||
]);
|
||||
|
||||
Log::info('SendVmCredentialsEmailJob: service activated', [
|
||||
'module_vmware_id' => $vm->id,
|
||||
'service_id' => $service->id,
|
||||
'ip' => $vm->ip,
|
||||
]);
|
||||
} else {
|
||||
// Service record not found — still activate the VM so it is not stuck at pending
|
||||
$vm->update(['status' => 1]);
|
||||
|
||||
Log::warning('SendVmCredentialsEmailJob: no Service record found, activated module_vmware only', [
|
||||
'module_vmware_id' => $vm->id,
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
private function billingLabel(?int $months): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user