Files
baodev0 0b08689ea4 feat: harden admin auth and ticket workflows
- add rotating admin refresh tokens and password gates\n- expand ticket galleries, exports, and client resources\n- improve IP allocation auditing and dashboard data\n- enforce encrypted client access tokens on public routes
2026-10-16 17:00:55 +07:00

113 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Events\OrderProvisioned;
use App\Jobs\Mail\Customer\VMCreationFailedJob;
use App\Models\Log as CustomerLog;
use App\Models\ModuleVmware;
use App\Models\OrderItem;
use Illuminate\Support\Facades\Log;
use Throwable;
class VmNotificationService
{
public function success(OrderItem $item, ModuleVmware $moduleVmware, string $vmId): void
{
$this->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' => (string) $customer->fullname,
'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 = (string) $customer->fullname;
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());
}
}
}