feat: khởi tạo/huỷ domain Plesk qua job, gửi mail hoá đơn qua queue, đính kèm triển khai

- ProvisionDnsDomainJob/SuspendDnsDomainJob: tạo domain admin lên Plesk, huỷ = suspend (is_created=3)
- Khách xem zone DNS được gán ở /dich-vu/domain, quản lý bản ghi A/AAAA/CNAME/TXT/MX/SRV
- send-mail hoá đơn đẩy SendInvoiceMailJob, preview + sửa nội dung, ghi mail.order_id
- invoice_attachments.type (payment_proof/deployment)
- CacheService xoá theo SCAN pattern, Redis không persistent mặc định
- Backfill transactions.public_code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-11 09:56:18 +07:00
co-authored by Claude Opus 5.5
parent 30898ea46f
commit 94614d9cb3
55 changed files with 1190 additions and 236 deletions
@@ -2,15 +2,15 @@
namespace App\Http\Controllers\Api\Admin\Invoice\Concerns;
use App\Jobs\Mail\SendInvoiceMailJob;
use App\Mail\InvoiceNotificationMail;
use App\Models\Customer;
use App\Models\Invoice;
use App\Services\InvoiceReminderService;
use App\Support\AutoEmailGate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Queue;
trait SendsInvoiceNotifications
{
@@ -29,6 +29,10 @@ trait SendsInvoiceNotifications
], 404);
}
if ($disabled = $this->mailDisabledResponse()) {
return $disabled;
}
if (!$reminders->isRemindable($invoice)) {
return response()->json([
'error' => true,
@@ -52,11 +56,69 @@ trait SendsInvoiceNotifications
]);
}
public function previewMail(Request $request, int $id): JsonResponse
{
$invoice = $this->invoiceForMail($request, $id);
if ($invoice instanceof JsonResponse) {
return $invoice;
}
$mail = InvoiceNotificationMail::forInvoice($invoice, $request->input('type') ?: 'invoice', $request->input('message'));
return response()->json([
'error' => false,
'data' => [
'to' => $invoice->customer->email,
'subject' => $mail->envelope()->subject,
'html' => $mail->render(),
'attachment_filename' => $this->invoicePdfService->generateFileName($invoice),
],
]);
}
public function sendMail(Request $request, int $id): JsonResponse
{
if ($disabled = $this->mailDisabledResponse()) {
return $disabled;
}
$invoice = $this->invoiceForMail($request, $id);
if ($invoice instanceof JsonResponse) {
return $invoice;
}
AutoEmailGate::markManual();
$jobId = Queue::push(new SendInvoiceMailJob(
$invoice->id,
$request->input('type') ?: 'invoice',
$request->input('message'),
$request->input('subject'),
$request->filled('html') ? $request->input('html') : null,
Auth::guard('admin')->id(),
));
return response()->json([
'error' => false,
'message' => "Đã đưa email vào hàng đợi gửi tới {$invoice->customer->email}",
'data' => ['job_id' => $jobId],
]);
}
private function mailDisabledResponse(): ?JsonResponse
{
return AutoEmailGate::mailEnabled() ? null : response()->json([
'error' => true,
'message' => 'Chức năng gửi email đang tắt. Bật lại tại Cài đặt → Thông báo.',
], 422);
}
private function invoiceForMail(Request $request, int $id): Invoice|JsonResponse
{
$request->validate([
'message' => 'nullable|string|max:1000',
'type' => 'nullable|string|in:invoice,renewal',
'subject' => 'nullable|required_with:html|string|max:255',
'html' => 'nullable|required_with:subject|string|max:500000',
]);
$invoice = $this->invoiceRepository->find($id);
@@ -65,57 +127,10 @@ trait SendsInvoiceNotifications
}
$invoice->load('customer');
$customer = $invoice->customer;
if (!$customer || !$customer->email) {
if (!$invoice->customer?->email) {
return response()->json(['error' => true, 'message' => 'Customer email not found'], 422);
}
$attachments = $this->attachmentRepository->getByInvoice($id);
$companyName = setting('general.company_name') ?: config('app.name');
$type = $request->get('type', 'invoice');
$message = $request->get(
'message',
$type === 'renewal'
? "Dịch vụ của quý khách sắp đến hạn gia hạn. Vui lòng kiểm tra hóa đơn #{$invoice->order_num} được đính kèm và hoàn tất thanh toán để dịch vụ không bị gián đoạn."
: "Vui lòng xem thông tin hóa đơn #{$invoice->order_num} của bạn."
);
$paymentUrl = url('/client/invoices/' . $invoice->invoice_number . '/payment');
try {
AutoEmailGate::markManual();
$invoicePdf = $this->invoicePdfService->generate($invoice);
$invoicePdfFileName = $this->invoicePdfService->generateFileName($invoice);
Mail::to($customer->email)->send(
new InvoiceNotificationMail(
$invoice,
$attachments,
$customer->fullname,
$message,
$companyName,
$type,
$invoicePdf,
$invoicePdfFileName,
$paymentUrl
)
);
} catch (Throwable $e) {
Log::error('InvoiceController@sendMail failed', [
'invoice_id' => $id,
'invoice_number' => $invoice->invoice_number ?? null,
'customer_email' => $customer->email,
'type' => $type,
'error' => $e->getMessage(),
]);
return response()->json([
'error' => true,
'message' => 'Không thể gửi email: ' . $e->getMessage(),
], 500);
}
return response()->json([
'error' => false,
'message' => "Email đã được gửi tới {$customer->email}",
]);
return $invoice;
}
}