- Created migration to add 'group_label' to 'product_type_fields' table. - Implemented ItSupportCatalogSeeder to populate IT Support features and tiers. - Added ProductCategorySeeder for initial product categories. - Developed ProductMockupSeeder for mockup products in various categories. - Introduced ProductTypeFieldSeeder to define fields for different product types. - Created email templates for VPS service information and service renewal reminders.
186 lines
7.2 KiB
PHP
186 lines
7.2 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\Services\VirtualMachine\VmCredentialsEmailDataBuilder;
|
|
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 ───────────────────────────────────────────
|
|
|
|
$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' => $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' => $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,
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────
|
|
|
|
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',
|
|
};
|
|
}
|
|
}
|