Files
portal-api/database/seeders/TemplateSeeder.php
T

208 lines
12 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Template;
use App\Support\EmailTemplateShell;
use App\Support\MailPlaceholders;
use Database\Seeders\EmailTemplates\AccountTemplates;
use Database\Seeders\EmailTemplates\AdminTemplates;
use Database\Seeders\EmailTemplates\BillingTemplates;
use Database\Seeders\EmailTemplates\IpTemplates;
use Database\Seeders\EmailTemplates\LegacyTemplates;
use Database\Seeders\EmailTemplates\MarketingTemplates;
use Database\Seeders\EmailTemplates\ServiceLifecycleTemplates;
use Database\Seeders\EmailTemplates\SubjectFormat;
use Database\Seeders\EmailTemplates\ServiceTemplates;
use Database\Seeders\EmailTemplates\TemplateVariables;
use Illuminate\Database\Seeder;
class TemplateSeeder extends Seeder
{
public function run(): void
{
foreach ([...self::templates(), ...self::legacyTemplates()] as $item) {
$this->upsert($item);
}
foreach (MarketingTemplates::all() as $item) {
$this->createMarketingIfMissing($item);
}
$this->seedServiceLifecycleTemplates();
$this->createMarketingIfMissing(self::ipRevocationTemplate());
}
public function seedServiceLifecycleTemplates(): void
{
foreach (ServiceLifecycleTemplates::all() as $item) {
$this->createMarketingIfMissing($item);
}
}
public function seedScenarioIfMissing(string $scenario): void
{
if (Template::query()->where('key', $scenario)->orWhere('scenario', $scenario)->exists()) {
return;
}
foreach (self::templates() as $item) {
if ($item['scenario'] === $scenario) {
$this->upsert($item);
}
}
}
public function reseedScenario(string $scenario): void
{
foreach (self::templates() as $item) {
if ($item['scenario'] === $scenario) {
$this->upsert($item);
}
}
}
private function createMarketingIfMissing(array $item): void
{
$template = Template::firstOrNew(['name' => $item['name']]);
if ($template->exists) {
return;
}
$template->fill([
'type_id' => 0,
'format' => 'html',
'subject' => SubjectFormat::for($item['name']) ?? $item['subject'],
'message' => $item['message'] ?? EmailTemplateShell::page($item['body']),
'status' => 1,
]);
$template->syncVariables($item['variables']);
$template->save();
}
/**
* @return array<int, array{scenario: string, name: string, subject: string, message: string, types?: array<string, string>}>
*/
public static function templates(): array
{
$layout = [
'scenario' => 'layout',
'name' => 'Bố cục Header/Footer (mặc định)',
'subject' => '',
'message' => EmailTemplateShell::page('{{$content}}'),
];
$scenarios = array_map(
fn (array $item) => [...$item, 'subject' => SubjectFormat::for($item['scenario']) ?? $item['subject'], 'message' => EmailTemplateShell::page($item['body'])],
[
...AccountTemplates::all(),
...ServiceTemplates::all(),
...IpTemplates::all(),
...BillingTemplates::all(),
...AdminTemplates::all(),
],
);
return [$layout, ...$scenarios];
}
/**
* @return array<int, array{scenario: string, name: string, subject: string, message: string}>
*/
public static function legacyTemplates(): array
{
return array_map(
fn (array $item) => [...$item, 'subject' => SubjectFormat::for($item['scenario']) ?? $item['subject'], 'message' => EmailTemplateShell::page($item['body'])],
LegacyTemplates::all(),
);
}
/**
* Template mẫu thông báo danh sách tài nguyên IP đã thu hồi.
*
* @return array{name: string, subject: string, message: string, variables: array{}}
*/
private static function ipRevocationTemplate(): array
{
$subject = 'V/v: Thế Giới Số gửi thông tin Thu hồi IP — {{service_name}} — {{customer_name}}';
$message = <<<'HTML'
<div style="margin:0;padding:24px 10px;background:#f2f1ef;font-family:'Times New Roman',Times,serif;color:#2b2b2b">
<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" style="max-width:820px;margin:0 auto;background:#fff;border-radius:14px;border-collapse:separate;overflow:hidden">
<tbody><tr><td><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td width="70%" style="height:5px;background:#a8a29e;font-size:0;line-height:0">&nbsp;</td><td width="30%" style="height:5px;background:#f26522;font-size:0;line-height:0">&nbsp;</td></tr></tbody></table></td></tr>
<tr><td style="padding:26px 40px 0;text-align:center"><img src="https://tgs.com.vn/wp-content/uploads/2024/11/cropped-LOGO-THE-GIOI-SO-2024-2.png" width="200" style="display:inline-block;border:0;max-width:200px;height:auto" alt="Thế Giới Số"></td></tr>
<tr><td style="padding:14px 40px 0;text-align:center"><div style="font-size:21px;font-weight:bold;letter-spacing:1px">THÔNG BÁO THU HỒI TÀI NGUYÊN IP</div><table align="center" cellpadding="0" cellspacing="0" border="0" style="margin:10px auto 0"><tbody><tr><td style="width:30px;height:2px;background:#a8a29e;font-size:0">&nbsp;</td><td style="width:4px;font-size:0">&nbsp;</td><td style="width:30px;height:2px;background:#f26522;font-size:0">&nbsp;</td></tr></tbody></table><div style="font-size:13px;color:#888;margin-top:10px;font-style:italic">{{notification_date}}</div></td></tr>
<tr><td style="padding:26px 40px 6px;font-size:16px;line-height:1.85"><p style="margin:0 0 14px">Kính gửi Quý Khách hàng <b>{{customer_name}}</b>,</p><p style="margin:0 0 20px">Thế Giới Số trân trọng thông báo thông tin thu hồi tài nguyên IP theo yêu cầu vận hành/dịch vụ.</p>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0 0 10px"><tbody><tr><td style="font-size:15px;font-weight:bold"><span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#a8a29e;margin-right:8px"></span>Thông tin IP</td><td align="right"><span style="background:#efedea;color:#5f5b57;font:700 12px Arial,sans-serif;padding:4px 12px;border-radius:999px">● Đã thu hồi</span></td></tr></tbody></table>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #e4e1de;border-radius:10px;border-collapse:separate;overflow:hidden"><tbody><tr><td style="padding:11px 14px;color:#5f5b57;font-size:13px;font-weight:bold;border-bottom:1px solid #e4e1de;background:#f7f6f4;text-align:center;width:60px">STT</td><td style="padding:11px 14px;color:#5f5b57;font-size:13px;font-weight:bold;border-bottom:1px solid #e4e1de;background:#f7f6f4;text-align:center">IP Address</td><td style="padding:11px 14px;color:#5f5b57;font-size:13px;font-weight:bold;border-bottom:1px solid #e4e1de;background:#f7f6f4;text-align:center">Dịch vụ</td><td style="padding:11px 14px;color:#5f5b57;font-size:13px;font-weight:bold;border-bottom:1px solid #e4e1de;background:#f7f6f4;text-align:center">Ngày thu hồi</td></tr><tr><td style="padding:13px 14px;font-size:15px;text-align:center;width:60px">1</td><td style="padding:13px 14px;font-size:15px;text-align:center;font-family:Consolas,'Courier New',monospace;font-weight:bold;color:#3a3633">{{ip_address}}</td><td style="padding:13px 14px;font-size:15px;text-align:center">{{service_name}}</td><td style="padding:13px 14px;font-size:15px;text-align:center">{{revocation_date}}</td></tr></tbody></table>
<div style="font-size:16px;line-height:1.85;margin-top:24px"><div style="margin-bottom:6px">Thế Giới Số xin chân thành cảm ơn Quý Khách hàng đã tin tưởng và đồng hành cùng chúng tôi trong thời gian qua.</div><div style="margin-bottom:6px">Khi có nhu cầu <b>sử dụng lại dịch vụ</b> hoặc <b>cấp mới tài nguyên IP</b>,<br>Quý Khách hàng vui lòng liên hệ Thế Giới Số qua các kênh hỗ trợ sau.</div><div>Rất mong tiếp tục được phục vụ Quý Khách hàng trong thời gian tới.</div></div>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:22px 0 24px"><tbody><tr><td></td><td width="300" align="center" style="font-size:15px;line-height:1.7"><div>Trân trọng,</div><div style="font-weight:bold;margin-top:4px">BỘ PHẬN KỸ THUẬT</div><div style="color:#d9480f;font-weight:bold">THẾ GIỚI SỐ</div></td></tr></tbody></table></td></tr>
<tr><td style="border-top:1px solid #f26522;padding:18px 32px 22px;text-align:center;line-height:1.8"><div style="color:#d9480f;font-size:19px;font-weight:bold">CÔNG TY CỔ PHẦN GIẢI PHÁP CÔNG NGHỆ THẾ GIỚI SỐ</div><div style="color:#f07022;font-size:12px">✆ Tel: (028) 7309 7379 &nbsp;|&nbsp; ✉ <a href="mailto:Support@tgs.com.vn" style="color:#f07022;text-decoration:none">Support@tgs.com.vn</a></div><div style="font-size:12px">🌐 <a href="https://www.TGS.com.vn" style="color:#1a73e8;text-decoration:none">www.TGS.com.vn</a> <span style="color:#1a73e8">&amp;</span> <a href="https://www.thegioiso.CLOUD" style="color:#1a73e8;text-decoration:none">www.thegioiso.CLOUD</a> <span style="color:#1a73e8">&amp;</span> <a href="https://www.thegioiSERVER.com" style="color:#1a73e8;text-decoration:none">www.thegioiSERVER.com</a> <span style="color:#1a73e8">&amp;</span> <a href="https://www.CloudEmail.vn" style="color:#1a73e8;text-decoration:none">www.CloudEmail.vn</a></div><div style="color:#f07022;font-size:12px;font-weight:bold">🥇 Thế Giới Số 🥇 - Premium HOSTING • CLOUD • SERVER &amp; WEBSITE | 15+ năm Phụng Sự 🚀</div></td></tr></tbody></table></div>
HTML;
return [
'name' => 'Thông báo thu hồi tài nguyên IP',
'subject' => $subject,
'message' => $message,
'variables' => [
['key' => 'customer_name', 'label' => 'Tên khách hàng', 'example' => 'CÔNG TY TNHH KHÁCH HÀNG MẪU'],
['key' => 'service_name', 'label' => 'Tên dịch vụ', 'example' => 'Dịch vụ IP'],
['key' => 'notification_date', 'label' => 'Ngày lập thông báo', 'example' => 'TP. Hồ Chí Minh, ngày 15 tháng 10 năm 2026'],
['key' => 'ip_address', 'label' => 'Địa chỉ IP', 'example' => '192.0.2.1'],
['key' => 'revocation_date', 'label' => 'Ngày thu hồi', 'example' => '15/10/2026'],
],
];
}
private function upsert(array $item): void
{
$scenario = $item['scenario'];
$template = Template::query()
->where(fn ($q) => $q->where('key', $scenario)->orWhere('scenario', $scenario))
->orderByRaw('`key` = ? desc', [$scenario])
->orderBy('id')
->first() ?? new Template(['type_id' => 0, 'status' => 1]);
$template->fill([
'key' => $scenario,
'scenario' => $scenario,
'name' => $item['name'],
'subject' => $item['subject'],
'message' => $item['message'],
'format' => 'html',
]);
$template->metadata = [
...($template->metadata ?? []),
'variables' => $this->variables($template, $item['types'] ?? []),
];
$template->save();
}
/**
* @param array<string, string> $typeOverrides
* @return array<int, array{key: string, label: string, type: string, example: string}>
*/
private function variables(Template $template, array $typeOverrides): array
{
$existing = $template->variables();
return collect(MailPlaceholders::extract($template->subject, $template->message))
->map(function (string $key) use ($existing, $typeOverrides) {
[$label, $type, $example] = TemplateVariables::DEFINITIONS[$key] ?? [$key, 'string', ''];
return [
'key' => $key,
'label' => $existing[$key]['label'] ?? $label,
'type' => $typeOverrides[$key] ?? $type,
'example' => $existing[$key]['example'] ?? $example,
];
})
->values()
->all();
}
}