Files
portal-api/database/seeders/EmailScenarioDemoSeeder.php
T
baodev0andClaude Opus 5.5 30898ea46f feat: email gửi tay/nhắc gia hạn, gói hosting, sửa dữ liệu sản phẩm
- Gửi email theo template cho 1 khách, tab lịch sử email, nhắc gia hạn dịch vụ hàng loạt
- Mail lưu from/bcc/sent_by; mẫu MARKETING và mẫu cũ trong TemplateSeeder
- Gói hosting: giới hạn, trạng thái, lấy plan Plesk qua CLI gateway
- Backfill payment_method và ngày đơn hàng còn thiếu
- Bổ sung field CPU/RAM/Disk/Băng thông cho loại sản phẩm vps và cloud

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 11:32:05 +07:00

138 lines
5.3 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Customer;
use App\Models\Mail;
use App\Models\Product;
use App\Models\Service;
use App\Models\Setting as SettingModel;
use App\Models\Template;
use App\Models\User;
use App\Support\EmailTemplateShell as Shell;
use Database\Seeders\EmailTemplates\MarketingTemplates;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* Dữ liệu thử các luồng gửi email của admin (gửi 1 khách, email marketing, nhắc gia hạn, tab Email).
* Chạy trên DB trống: php artisan migrate:fresh --seed --seeder=EmailScenarioDemoSeeder
*/
class EmailScenarioDemoSeeder extends Seeder
{
private const CUSTOMER_PASSWORD = '!admin@123!@#';
private const CUSTOMERS = [
'pbao.dev@gmail.com' => ['Phạm Bảo (dev)', '0900000001', [['vps', 2], ['email', 25], ['vps', -4]]],
'pbao.business@gmail.com' => ['Phạm Bảo Business', '0900000002', [['email', 5], ['server', 45]]],
'phamgiabao48756@gmail.com' => ['Phạm Gia Bảo', '0900000003', [['website', 12], ['vps', 75]]],
'pbao.drive@gmail.com' => ['Phạm Bảo Drive', '0900000004', [['server', 0], ['support', 150], ['email', 9, 'suspended']]],
];
private const PRODUCT_KEYWORDS = [
'vps' => 'VPS',
'email' => 'Email',
'server' => 'Máy Chủ',
'website' => 'Website',
'support' => 'IT Support',
];
public function run(): void
{
$this->call([
RoleSeeder::class,
RolesAndPermissions::class,
TestAccountSeeder::class,
TemplateSeeder::class,
Setting::class,
ProductCategorySeeder::class,
ProductTypeFieldSeeder::class,
ProductMockupSeeder::class,
VPSProductSeeder::class,
]);
$this->enableMail();
$marketing = Template::whereIn('name', array_column(MarketingTemplates::all(), 'name'))->orderBy('id')->get();
$admin = User::where('username', 'admin')->firstOrFail();
foreach (self::CUSTOMERS as $email => [$name, $phone, $services]) {
$customer = $this->seedCustomer($email, $name, $phone);
foreach ($services as $service) {
$this->seedService($customer, ...$service);
}
$this->seedMailHistory($customer, $admin, $marketing->first());
}
$this->command?->info('Đã tạo ' . count(self::CUSTOMERS) . ' khách hàng, ' . count($marketing) . ' mẫu marketing và dịch vụ sắp hết hạn.');
}
private function enableMail(): void
{
$setting = SettingModel::firstOrNew(['key' => 'email_settings']);
$value = json_decode($setting->value ?? '{}', true) ?: [];
$setting->value = json_encode([...$value, 'mail_enabled' => true], JSON_UNESCAPED_UNICODE);
$setting->save();
}
private function seedCustomer(string $email, string $name, string $phone): Customer
{
$customer = Customer::firstOrNew(['email' => $email]);
$customer->fill([
'fullname' => $name,
'username' => Str::before($email, '@'),
'phone' => $phone,
'password' => Hash::make(self::CUSTOMER_PASSWORD),
'email_verified_at' => now(),
'email_verify' => 1,
]);
$customer->forceFill(['status' => 1])->save();
Service::where('customer_id', $customer->id)->where('note', 'seed:email-demo')->delete();
return $customer;
}
private function seedService(Customer $customer, string $kind, int $daysLeft, string $status = 'active'): void
{
$product = Product::where('name', 'like', '%' . self::PRODUCT_KEYWORDS[$kind] . '%')->first() ?? Product::first();
Service::create([
'uuid' => (string) Str::uuid(),
'customer_id' => $customer->id,
'product_id' => $product?->id,
'server_name' => Str::upper($kind) . '-' . Str::before($customer->email, '@'),
'status' => $status,
'auto_renew' => $daysLeft > 30,
'started_at' => now()->addDays($daysLeft)->subYear(),
'expires_at' => now()->addDays($daysLeft)->setTime(17, 0),
'note' => 'seed:email-demo',
]);
}
private function seedMailHistory(Customer $customer, User $admin, Template $template): void
{
Mail::where('customer_id', $customer->id)->delete();
Mail::create([
'template_id' => $template->id,
'subject' => str_replace('{{client_name}}', $customer->fullname, $template->subject),
'from_address' => config('mail.from.address'),
'to' => $customer->email,
'bcc' => 'audit@tgs.com.vn',
'customer_id' => $customer->id,
'sent_by' => $admin->id,
'message' => str_replace(['{{client_name}}', '{{promo_code}}', '{{frontend_url}}'], [$customer->fullname, 'TGS30', '#'], $template->message),
]);
Mail::create([
'subject' => 'Chào mừng ' . $customer->fullname . ' đến với Thế Giới Số',
'to' => $customer->email,
'customer_id' => $customer->id,
'message' => Template::renderWithLayout('Chào mừng', Shell::paragraph('Tài khoản của Quý khách đã được kích hoạt.')),
]);
}
}