writeCustomerSuccessLog($item); $this->broadcastProvisioned($item, $vmId, true); } public function failed(OrderItem $item, string $errorMessage): void { $this->sendFailureEmail($item, $errorMessage); $this->writeCustomerFailureLog($item, $errorMessage); } private function sendFailureEmail(OrderItem $item, string $errorMessage): void { try { $customer = $item->order->customer; $product = $item->getProductAttribute(); VMCreationFailedJob::dispatch([ 'customer_email' => $customer->email, 'customer_name' => trim($customer->firstname . ' ' . $customer->lastname), 'product_name' => $product?->name ?? 'Unknown Product', 'order_id' => $item->order->id, 'order_num' => $item->order->order_num, 'error_message' => $errorMessage, 'order_item_id' => $item->id, ]); } catch (Throwable $e) { Log::error('VmNotificationService: cannot dispatch failure email: ' . $e->getMessage()); } } private function writeCustomerSuccessLog(OrderItem $item): void { try { $order = $item->order; $customer = $order?->customer; if (!$order || !$customer) { return; } CustomerLog::create([ 'agent' => 'system', 'client_ip' => null, 'customer_id' => $customer->id, 'description' => 'Khởi tạo dịch vụ thành công cho đơn hàng #' . $order->order_num . ' (product_id: ' . $item->product_id . ', os_id: ' . $item->os_id . ').', ]); } catch (Throwable $e) { Log::warning('VmNotificationService: cannot write success log: ' . $e->getMessage()); } } private function writeCustomerFailureLog(OrderItem $item, string $errorMessage): void { try { $order = $item->order; $customer = $order?->customer; if (!$order || !$customer) { return; } CustomerLog::create([ 'agent' => 'system', 'client_ip' => null, 'customer_id' => $customer->id, 'description' => 'Khởi tạo dịch vụ thất bại cho đơn hàng #' . $order->order_num . ' (product_id: ' . $item->product_id . ', os_id: ' . $item->os_id . ').' . ' Lỗi: ' . $errorMessage . '. ' . 'Nếu có lỗi xảy ra khi khởi tạo dịch vụ quý khách nhấn khởi tạo tại đây, hoặc quý khách có thể liên hệ với kỹ thuật support@tgs.com.vn', ]); } catch (Throwable $e) { Log::warning('VmNotificationService: cannot write failure log: ' . $e->getMessage()); } } private function broadcastProvisioned(OrderItem $item, string $vmId, bool $success): void { try { $order = $item->order; $customer = $order->customer; $name = trim($customer->firstname . ' ' . $customer->lastname); broadcast(new OrderProvisioned( orderId: $order->id, orderNum: $order->order_num, customerName: $name, vmId: $vmId, success: $success, message: 'Máy ảo đã được khởi tạo thành công cho khách hàng ' . $name . ' (Đơn #' . $order->order_num . ')', )); } catch (Throwable $e) { Log::warning('VmNotificationService: cannot broadcast OrderProvisioned: ' . $e->getMessage()); } } }