feat: create PostCategoryFactory for generating post category data feat: create PostFactory for generating post data feat: add retry_requested_at column to services table feat: add vcenter provisioning fields to products table feat: add module_host_id to os table feat: create email_notification_templates table for email templates feat: add scenario and format fields to template table feat: add admin fields to transactions table fix: update ticket table columns and types feat: add uuid to ticket table and backfill existing records fix: make ticket_reply user_id nullable feat: create notifications table for user notifications fix: make ticket uuid nullable feat: create post_categories table for blog categories feat: create posts table for blog posts feat: add invoice email template for PDF generation
230 lines
9.4 KiB
PHP
230 lines
9.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs\VirtualMachine\Provision;
|
|
|
|
use App\Http\Services\Telegram;
|
|
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
|
|
*
|
|
* 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
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
public int $timeout = 60;
|
|
|
|
public function __construct(
|
|
public readonly int $moduleVmwareId,
|
|
) {}
|
|
|
|
public function handle(Telegram $telegram): void
|
|
{
|
|
$vm = ModuleVmware::with([
|
|
'customer',
|
|
'os.osGroup',
|
|
'order.orderItems.service',
|
|
])->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 ───────────────────────────────────────────
|
|
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
$settingValue = $setting ? json_decode((string) $setting->value) : null;
|
|
|
|
$months = (int) ($orderItem?->months ?? 0);
|
|
$osGroupName = $vm->os?->osGroup?->name ?? '';
|
|
$isWindows = str_contains(strtolower($osGroupName), 'windows');
|
|
$osUsername = $isWindows ? 'Administrator' : 'root';
|
|
|
|
$emailData = $this->safeUtf8([
|
|
'company_name' => $settingValue->company ?? 'Thế Giới Số',
|
|
'company_logo' => $settingValue->logo ?? '',
|
|
'client_name' => $customer->fullname ?? '',
|
|
'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS',
|
|
'product_billing' => $this->billingLabel($months),
|
|
'product_price' => $orderItem ? number_format((float) $orderItem->price, 0, ',', '.') : 'N/A',
|
|
'service_host' => $vm->ip ?? 'N/A',
|
|
'service_username' => $osUsername,
|
|
'service_port' => (string) ($vm->port ?? '22'),
|
|
'service_password' => $vm->plain_password ?? '(xem tai khoan quan ly)',
|
|
'os_name' => $vm->os ? ($osGroupName . ' ' . $vm->os->version) : 'Unknown OS',
|
|
'service_due_exp' => $vm->next_due_date
|
|
? date('d/m/Y', (int) $vm->next_due_date)
|
|
: 'N/A',
|
|
]);
|
|
|
|
// 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' => $this->billingLabel($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' => $osUsername,
|
|
'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,
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────
|
|
|
|
private function billingLabel(?int $months): string
|
|
{
|
|
return match ($months) {
|
|
1 => 'Hang thang',
|
|
3 => '3 Thang',
|
|
6 => '6 Thang',
|
|
12 => 'Hang nam',
|
|
24 => '2 Nam',
|
|
default => ($months ?? 0) . ' Thang',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Sanitize tất cả string trong array thành UTF-8 hợp lệ.
|
|
* Loại bỏ ký tự binary/invalid trước khi đẩy vào queue (tránh JSON encode lỗi).
|
|
*/
|
|
private function safeUtf8(array $data): array
|
|
{
|
|
return array_map(function ($value) {
|
|
if (!is_string($value)) return $value;
|
|
|
|
// Nếu đã là UTF-8 hợp lệ → giữ nguyên
|
|
if (mb_check_encoding($value, 'UTF-8')) return $value;
|
|
|
|
// Thử convert từ encoding khác sang UTF-8
|
|
$converted = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value, ['UTF-8', 'ISO-8859-1', 'Windows-1252'], true) ?: 'ISO-8859-1');
|
|
|
|
// Nếu vẫn invalid → strip ký tự lỗi
|
|
return mb_check_encoding($converted, 'UTF-8')
|
|
? $converted
|
|
: preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]|[^\x09\x0A\x0D\x20-\x{10FFFF}]/u', '', $value);
|
|
}, $data);
|
|
}
|
|
}
|