Files
portal-api/app/Services/Email/EmailTemplateService.php
T
baodev0andClaude Opus 5.5 94614d9cb3 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>
2026-10-11 09:56:18 +07:00

57 lines
1.6 KiB
PHP

<?php
namespace App\Services\Email;
use App\Jobs\Mail\SendTemplateEmailJob;
use App\Models\Template;
use App\Support\AutoEmailGate;
use Illuminate\Support\Facades\Cache;
/**
* Entry point chung để gửi email theo scenario. Load Template (có cache),
* render qua EmailTemplateRenderer, gửi qua queue (SendTemplateEmailJob).
*/
class EmailTemplateService
{
public function __construct(
private readonly EmailTemplateRenderer $renderer,
) {}
/**
* @param string|array<int, string> $to
*/
public function send(string $scenario, string|array $to, array $data = []): void
{
if (!AutoEmailGate::allows($scenario)) {
return;
}
SendTemplateEmailJob::dispatch($scenario, is_array($to) ? $to : [$to], $data);
}
/**
* Render ngay (không gửi) — dùng cho preview trong Email Builder.
*
* @return array{subject: string, html: string}
*/
public function preview(string $scenario, array $data = []): array
{
return $this->renderer->render($this->loadTemplate($scenario), $data);
}
/**
* Tìm template theo `key` (định danh riêng, ưu tiên) hoặc `scenario`
* (tương thích ngược với các lời gọi cũ truyền scenario làm định danh).
*/
public function loadTemplate(string $key): Template
{
return Cache::remember(
Template::CACHE_PREFIX . $key,
3600,
fn () => Template::where('status', 1)
->where(fn ($q) => $q->where('key', $key)->orWhere('scenario', $key))
->firstOrFail(),
);
}
}