159 lines
6.1 KiB
PHP
159 lines
6.1 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\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\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.
|
|
*/
|
|
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'])->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;
|
|
}
|
|
|
|
$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);
|
|
|
|
$osGroupName = $vm->os?->osGroup?->name ?? '';
|
|
$isWindows = str_contains(strtolower($osGroupName), 'windows');
|
|
$osUsername = $isWindows ? 'Administrator' : 'root';
|
|
|
|
$emailData = [
|
|
'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' => $vm->port,
|
|
'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)',
|
|
'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',
|
|
];
|
|
|
|
// Resolve invoice ID — PDF is generated inside SendInformationVirtualMachine::handle()
|
|
// to avoid passing large binary blobs through the 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,
|
|
]);
|
|
|
|
// Notify admin with VM credentials after successful provisioning
|
|
$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());
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
private function billingLabel(?int $months): string
|
|
{
|
|
return match ($months) {
|
|
1 => 'Hàng tháng',
|
|
3 => '3 Tháng',
|
|
6 => '6 Tháng',
|
|
12 => 'Hàng năm',
|
|
24 => '2 Năm',
|
|
default => ($months ?? 0) . ' Tháng',
|
|
};
|
|
}
|
|
}
|