find($this->moduleVmwareId); if (!$vm) { $msg = "SendVmCredentialsEmailJob: ModuleVmware #{$this->moduleVmwareId} not found"; Log::error($msg); $telegram->SendErrorMessage($msg); return; } $customer = $vm->customer; if (!$customer?->email) { $msg = "SendVmCredentialsEmailJob: customer or email missing for ModuleVmware #{$this->moduleVmwareId}"; Log::error($msg, ['module_vmware_id' => $this->moduleVmwareId]); $telegram->SendErrorMessage($msg); return; } // ── [1 & 2] Activate service + VM records ──────────────────────────── $orderItem = $vm->order?->orderItems?->first(); $service = $orderItem?->service; $this->activateService($vm, $service); // ── [3] Build email payload ─────────────────────────────────────────── $months = (int) ($orderItem?->months ?? 0); $emailData = app(VmCredentialsEmailDataBuilder::class)->build($vm, $orderItem); // 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); Log::info('SendVmCredentialsEmailJob: email dispatched', [ 'module_vmware_id' => $this->moduleVmwareId, 'customer_email' => $customer->email, 'ip' => $vm->ip, 'invoice_id' => $invoice?->id, ]); // ── [4] Admin notification ──────────────────────────────────────────── $order = $vm->order; AdminOrderNotificationJob::dispatch([ 'customer_name' => $customer->fullname ?? '', 'customer_email' => $customer->email, 'client_ip' => null, 'order_num' => $order?->order_num ?? $order?->id ?? 'N/A', 'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS', 'product_type' => 'Cloud VPS', 'billing_cycle' => ProductBillingCycle::vietnameseLabelForMonths($months), 'total' => $orderItem?->price ?? 0, 'created_at' => $order?->created_at?->format('d/m/Y H:i:s') ?? now()->format('d/m/Y H:i:s'), 'ip' => $vm->ip ?? '', 'username' => $emailData['service_username'], 'password' => $vm->plain_password ?? '', ]); Log::info('SendVmCredentialsEmailJob: admin notification dispatched', [ 'module_vmware_id' => $this->moduleVmwareId, ]); } public function failed(\Throwable $e): void { Log::error('SendVmCredentialsEmailJob: failed', [ 'module_vmware_id' => $this->moduleVmwareId, 'error' => $e->getMessage(), ]); try { app(Telegram::class)->SendErrorMessage([ 'job' => 'SendVmCredentialsEmailJob', 'module_vmware_id' => $this->moduleVmwareId, 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), ]); } catch (\Throwable $te) { Log::error('SendVmCredentialsEmailJob: could not send Telegram notification: ' . $te->getMessage()); } } // ───────────────────────────────────────────────────────────────────────── /** * 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, ]); } }); } // ───────────────────────────────────────────────────────────────────────── }