feat: nhắc gia hạn QR theo dịch vụ, failover server hosting, CCCD admin, log cấp phát dịch vụ
- Mail campaign: mẫu nhắc gia hạn thanh toán QR, chọn dịch vụ + chu kỳ từng dòng (renewal_services) - Hosting: nhiều server Plesk cho một gói (service_plan_servers), chuyển server khi server chết - Admin xem/sửa giấy tờ định danh khách, tuỳ chọn gia hạn cho khách, nhật ký cấp phát dịch vụ - Đánh dấu mẫu email dịch vụ là non-marketing Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
047c23beff
commit
55a834a2f8
@@ -7,6 +7,8 @@ namespace App\Jobs\Hosting;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\OrderItem;
|
||||
use App\Models\ServicePlan;
|
||||
use App\Services\Hosting\HostingProvisionFailure;
|
||||
use App\Services\Hosting\HostingServerSelector;
|
||||
use App\Services\Plesk\PleskHostingService;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
@@ -14,6 +16,8 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
/**
|
||||
* Auto-provisions a Plesk hosting account when a hosting order is paid,
|
||||
@@ -26,97 +30,89 @@ class ProvisionHostingAccountJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/** Plesk subscription creation can take a couple of minutes */
|
||||
public int $timeout = 300;
|
||||
|
||||
/** No auto-retry — hosting provisioning errors need manual review */
|
||||
public int $tries = 1;
|
||||
|
||||
public function __construct(public OrderItem $item)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(PleskHostingService $plesk): void
|
||||
public function handle(PleskHostingService $plesk, HostingServerSelector $servers, HostingProvisionFailure $failure): void
|
||||
{
|
||||
try {
|
||||
$error = $this->provision($plesk, $servers, $failure);
|
||||
} catch (Throwable $e) {
|
||||
$error = $e->getMessage();
|
||||
}
|
||||
|
||||
if ($error !== null) {
|
||||
$failure->handle($this->item, $error);
|
||||
}
|
||||
}
|
||||
|
||||
private function provision(PleskHostingService $plesk, HostingServerSelector $servers, HostingProvisionFailure $failure): ?string
|
||||
{
|
||||
$service = $this->item->service()->first();
|
||||
$domainName = $this->item->server_name;
|
||||
|
||||
if (! $service || ! $domainName) {
|
||||
Log::channel('Custom')->warning('ProvisionHostingAccountJob skipped: missing service or domain', [
|
||||
'order_item_id' => $this->item->id,
|
||||
]);
|
||||
|
||||
return;
|
||||
return 'Không tìm thấy dịch vụ hoặc tên miền của đơn hàng.';
|
||||
}
|
||||
|
||||
$servicePlanId = $this->item->product?->service_plan_id;
|
||||
$servicePlan = $servicePlanId
|
||||
? ServicePlan::with(['plesk', 'dnsServer'])->find($servicePlanId)
|
||||
: null;
|
||||
|
||||
$servicePlan = $servicePlanId ? ServicePlan::with('plesk')->find($servicePlanId) : null;
|
||||
$hostingPlan = $servicePlan?->plesk?->plesk_plan_name;
|
||||
$server = $servicePlan?->dnsServer;
|
||||
|
||||
Log::channel('Custom')->info('ProvisionHostingAccountJob resolved service plan', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'product_id' => $this->item->product_id,
|
||||
'product_name' => $this->item->product?->name,
|
||||
'service_plan_id' => $servicePlanId,
|
||||
'plesk_plan_name' => $hostingPlan,
|
||||
'dns_manager_server_id' => $server?->id,
|
||||
'dns_manager_server_ip' => $server?->ip_address,
|
||||
'order_item_id' => $this->item->id,
|
||||
'product_id' => $this->item->product_id,
|
||||
'product_name' => $this->item->product?->name,
|
||||
'service_plan_id' => $servicePlanId,
|
||||
'plesk_plan_name' => $hostingPlan,
|
||||
]);
|
||||
|
||||
if (! $hostingPlan) {
|
||||
Log::channel('Custom')->error('ProvisionHostingAccountJob failed: no service_plan_plesk mapping for this product', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'product_id' => $this->item->product_id,
|
||||
'service_plan_id' => $servicePlanId,
|
||||
]);
|
||||
|
||||
return;
|
||||
return 'Sản phẩm chưa gắn gói Plesk (service_plan_plesk).';
|
||||
}
|
||||
|
||||
if (! $server) {
|
||||
Log::channel('Custom')->error('ProvisionHostingAccountJob failed: service plan has no dns_manager_server_id configured', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'product_id' => $this->item->product_id,
|
||||
'service_plan_id' => $servicePlanId,
|
||||
]);
|
||||
ServiceProvisionLog::record($service, 'provision_started', 'Bắt đầu khởi tạo hosting', ['domain' => $domainName, 'plesk_plan' => $hostingPlan]);
|
||||
|
||||
return;
|
||||
}
|
||||
$invoiceId = Invoice::where('order_id', $this->item->order_id)->where('invoice_type', 1)->orderBy('id')->value('id');
|
||||
$triedServerIds = [];
|
||||
$lastError = null;
|
||||
|
||||
if (! $server->servesHosting()) {
|
||||
Log::channel('Custom')->error('ProvisionHostingAccountJob failed: configured server does not serve hosting', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'service_plan_id' => $servicePlanId,
|
||||
while ($server = $servers->pick($servicePlan, $triedServerIds)) {
|
||||
$triedServerIds[] = $server->id;
|
||||
|
||||
$result = $plesk->provision([
|
||||
'customer_id' => $this->item->order->customer_id,
|
||||
'service_id' => $service->id,
|
||||
'dns_manager_server_id' => $server->id,
|
||||
'server_type' => $server->type,
|
||||
'domain_name' => $domainName,
|
||||
'hosting_plan' => $hostingPlan,
|
||||
'invoice_id' => $invoiceId,
|
||||
]);
|
||||
|
||||
return;
|
||||
if (! $result['error']) {
|
||||
$service->update(['status' => 'active']);
|
||||
ServiceProvisionLog::record($service, 'provision_completed', 'Khởi tạo hoàn tất — dịch vụ đã hoạt động', ['server_id' => $server->id]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$lastError = $result['message'] ?? 'Plesk trả về lỗi không rõ.';
|
||||
$servers->forget($server);
|
||||
|
||||
if ($servers->isHealthy($server)) {
|
||||
return $lastError;
|
||||
}
|
||||
|
||||
$failure->serverFailedOver($this->item, $server, $lastError);
|
||||
}
|
||||
|
||||
$result = $plesk->provision([
|
||||
'customer_id' => $this->item->order->customer_id,
|
||||
'service_id' => $service->id,
|
||||
'dns_manager_server_id' => $server->id,
|
||||
'domain_name' => $domainName,
|
||||
'hosting_plan' => $hostingPlan,
|
||||
'invoice_id' => Invoice::where('order_id', $this->item->order_id)->where('invoice_type', 1)->orderBy('id')->value('id'),
|
||||
]);
|
||||
|
||||
if ($result['error']) {
|
||||
Log::channel('Custom')->error('ProvisionHostingAccountJob failed', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'hosting_plan' => $hostingPlan,
|
||||
'message' => $result['message'] ?? 'Unknown error',
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$service->update(['status' => 'active']);
|
||||
return $lastError === null
|
||||
? 'Gói hosting không có server Plesk nào đang hoạt động.'
|
||||
: "Tất cả server Plesk của gói đều lỗi. Lỗi cuối: {$lastError}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Models\MailCampaign;
|
||||
use App\Models\Notification;
|
||||
use App\Services\Email\MailCampaignRenderer;
|
||||
use App\Support\EmailRecipients;
|
||||
use App\Support\RenewalCampaign;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
@@ -27,6 +28,7 @@ class SendMailCampaignRecipient implements ShouldQueue
|
||||
private readonly int $campaignId,
|
||||
private readonly ?int $customerId,
|
||||
private readonly string $email,
|
||||
private readonly ?int $serviceId = null,
|
||||
) {}
|
||||
|
||||
public function handle(MailCampaignRenderer $renderer): void
|
||||
@@ -43,7 +45,29 @@ class SendMailCampaignRecipient implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
$rendered = $renderer->render($campaign, $renderer->recipientValues($campaign, $customer, $this->email));
|
||||
$values = $renderer->recipientValues($campaign, $customer, $this->email);
|
||||
if ($this->serviceId !== null) {
|
||||
$renewal = $customer ? RenewalCampaign::valuesFor($campaign, $customer, $this->serviceId) : null;
|
||||
if ($renewal === null) {
|
||||
MailLog::create([
|
||||
'campaign_id' => $campaign->id,
|
||||
'template_id' => $campaign->template_id ?? 0,
|
||||
'subject' => $campaign->subject,
|
||||
'to' => $this->email,
|
||||
'customer_id' => $customer?->id,
|
||||
'service_id' => $this->serviceId,
|
||||
'sent_by' => $campaign->created_by,
|
||||
'message' => 'Không gửi: dịch vụ không còn thuộc khách hàng hoặc không thể tính giá gia hạn.',
|
||||
'status' => 'failed',
|
||||
]);
|
||||
$this->markDone($campaign, true);
|
||||
|
||||
return;
|
||||
}
|
||||
$values = [...$values, ...$renewal];
|
||||
}
|
||||
|
||||
$rendered = $renderer->render($campaign, $values);
|
||||
$cc = EmailRecipients::parse($campaign->cc);
|
||||
$bcc = EmailRecipients::parse($campaign->bcc);
|
||||
$mailConfig = Arr::only($campaign->template?->mailConfig() ?? [], ['from_email', 'from_name', 'reply_to', 'reply_to_name']);
|
||||
@@ -81,6 +105,7 @@ class SendMailCampaignRecipient implements ShouldQueue
|
||||
'cc' => implode(', ', $cc),
|
||||
'bcc' => implode(', ', $bcc),
|
||||
'customer_id' => $customer?->id,
|
||||
'service_id' => $this->serviceId,
|
||||
'sent_by' => $campaign->created_by,
|
||||
'message' => $rendered['html'],
|
||||
'status' => $failed ? 'failed' : 'sent',
|
||||
|
||||
@@ -28,6 +28,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
class CreateVirtualMachineJob implements ShouldQueue
|
||||
{
|
||||
@@ -70,6 +71,8 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
['customer_id' => $customerId, 'status' => 'running'],
|
||||
);
|
||||
|
||||
ServiceProvisionLog::forOrderItem($this->item->id, 'provision_started', $this->isRetry ? 'Bắt đầu khởi tạo lại máy chủ' : 'Bắt đầu khởi tạo máy chủ', ['is_retry' => $this->isRetry]);
|
||||
|
||||
$vmLogger->step('Create VM job started', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'is_retry' => $this->isRetry,
|
||||
@@ -110,6 +113,8 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
|
||||
$this->item->server_name = $combinedName;
|
||||
|
||||
ServiceProvisionLog::forOrderItem($this->item->id, 'provision_step', 'Đã giữ IP và bắt đầu clone VM', ['ip' => $reservedIp]);
|
||||
|
||||
$vmLogger->step('IP reserved for VM', [
|
||||
'order_item_id' => $this->item->id,
|
||||
'ip' => $reservedIp,
|
||||
@@ -226,6 +231,8 @@ class CreateVirtualMachineJob implements ShouldQueue
|
||||
|
||||
$adminDetail = $e instanceof VmProvisionException ? $e->adminDetail : $e->getMessage();
|
||||
|
||||
ServiceProvisionLog::forOrderItem($this->item->id, 'provision_failed', 'Khởi tạo máy chủ thất bại — ' . $adminDetail, ['stage' => 'create_vm']);
|
||||
|
||||
$vmLogger->exception('Create VM job failed', $e, [
|
||||
'order_item_id' => $this->item->id,
|
||||
'is_retry' => $this->isRetry,
|
||||
|
||||
@@ -22,6 +22,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
use App\Jobs\VirtualMachine\Provision\Concerns\ManagesIpReservation;
|
||||
/**
|
||||
@@ -228,6 +229,8 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
'attempt' => $attempt,
|
||||
]);
|
||||
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_step', 'Đã gán IP cho máy chủ', ['ip' => $ip]);
|
||||
|
||||
// CPU/RAM config was already applied by UpdateVmConfigJob before IP assignment.
|
||||
ChangePasswordViaVsphereJob::dispatch(
|
||||
$this->moduleVmwareId,
|
||||
@@ -247,6 +250,7 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
|
||||
ModuleVmware::where('id', $this->moduleVmwareId)->update(['status' => 0]);
|
||||
Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'failed']);
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_failed', "Gán IP thất bại sau {$maxAttempts} lần thử", ['stage' => 'assign_ip']);
|
||||
|
||||
// Notify admin via Telegram with order/customer context
|
||||
try {
|
||||
@@ -311,6 +315,7 @@ class AssignIpToGuestJob implements ShouldQueue
|
||||
|
||||
ModuleVmware::where('id', $this->moduleVmwareId)->update(['status' => 0]);
|
||||
Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'failed']);
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_failed', 'Gán IP thất bại — ' . $e->getMessage(), ['stage' => 'assign_ip']);
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -16,6 +16,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
/**
|
||||
* ChangePasswordViaVsphereJob
|
||||
@@ -91,6 +92,8 @@ class ChangePasswordViaVsphereJob implements ShouldQueue
|
||||
// Provisioning complete — activate the service record
|
||||
Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'active']);
|
||||
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_step', 'Đã đặt mật khẩu máy chủ');
|
||||
|
||||
// [8] Send credentials email — IP + new password are both ready at this point
|
||||
SendVmCredentialsEmailJob::dispatch($this->moduleVmwareId);
|
||||
|
||||
@@ -112,6 +115,7 @@ class ChangePasswordViaVsphereJob implements ShouldQueue
|
||||
|
||||
ModuleVmware::where('id', $this->moduleVmwareId)->update(['status' => 0]);
|
||||
Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'failed']);
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_failed', 'Đặt mật khẩu máy chủ thất bại — ' . $e->getMessage(), ['stage' => 'set_password']);
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -18,6 +18,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
/**
|
||||
* RetryProvisioningOrchestrator
|
||||
@@ -337,6 +338,8 @@ class RetryProvisioningOrchestrator implements ShouldQueue
|
||||
|
||||
$service->save();
|
||||
|
||||
ServiceProvisionLog::record($service, 'provision_completed', 'Khởi tạo lại hoàn tất — dịch vụ đã hoạt động', ['ip' => $moduleVmware->ip]);
|
||||
|
||||
if (!$moduleVmware->service_id) {
|
||||
$moduleVmware->update(['service_id' => $service->id]);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
/**
|
||||
* SendVmCredentialsEmailJob
|
||||
@@ -154,6 +155,8 @@ class SendVmCredentialsEmailJob implements ShouldQueue
|
||||
'service_id' => $service->id,
|
||||
]);
|
||||
|
||||
ServiceProvisionLog::record($service, 'provision_completed', 'Khởi tạo hoàn tất — dịch vụ đã hoạt động', ['ip' => $vm->ip]);
|
||||
|
||||
Log::info('SendVmCredentialsEmailJob: service activated', [
|
||||
'module_vmware_id' => $vm->id,
|
||||
'service_id' => $service->id,
|
||||
|
||||
@@ -22,6 +22,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use App\Support\TlsHttpClientFactory;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Support\ServiceProvisionLog;
|
||||
|
||||
/**
|
||||
* UpdateVmConfigJob
|
||||
@@ -208,6 +209,10 @@ class UpdateVmConfigJob implements ShouldQueue
|
||||
|
||||
$moduleVmware->update(['cpu' => $totalCpu, 'ram_mb' => $totalRam * 1024]);
|
||||
|
||||
if (! $isManualOverride) {
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_step', "Đã cấu hình CPU/RAM ({$totalCpu} vCPU, {$totalRam} GB)");
|
||||
}
|
||||
|
||||
if ($isManualOverride) {
|
||||
$this->syncServiceAddons($moduleVmware, $totalCpu, $totalRam, $logger);
|
||||
return;
|
||||
@@ -227,6 +232,7 @@ class UpdateVmConfigJob implements ShouldQueue
|
||||
|
||||
ModuleVmware::where('id', $this->moduleVmwareId)->update(['status' => 0]);
|
||||
Service::where('vm_id', $this->moduleVmwareId)->update(['status' => 'failed']);
|
||||
ServiceProvisionLog::forVm($this->moduleVmwareId, 'provision_failed', 'Cấu hình CPU/RAM thất bại — ' . $e->getMessage(), ['stage' => 'update_config']);
|
||||
|
||||
try {
|
||||
$telegram = app(Telegram::class);
|
||||
|
||||
Reference in New Issue
Block a user