Files
portal-api/database/seeders/TestAccountSeeder.php
T
2026-09-28 16:39:22 +07:00

91 lines
3.4 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Customer;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* Tài khoản dùng để kiểm thử luồng order/invoice sau đợt sửa.
* Chạy: php artisan db:seed --class=TestAccountSeeder
*
* Idempotent — chạy lại chỉ đặt lại mật khẩu, không tạo trùng.
*/
class TestAccountSeeder extends Seeder
{
private const CUSTOMER_EMAIL = 'pbao.dev@gmail.com';
private const CUSTOMER_PASSWORD = '!admin@123!@#';
private const ADMIN_USERNAME = 'admin';
private const ADMIN_PASSWORD = '!admin@123!@#';
public function run(): void
{
$this->seedAdmin();
$this->seedCustomer();
}
private function seedAdmin(): void
{
$admin = User::where('username', self::ADMIN_USERNAME)->first();
if ($admin) {
$admin->update(['password' => Hash::make(self::ADMIN_PASSWORD)]);
$this->command?->info('Admin đã có — đặt lại mật khẩu cho "' . self::ADMIN_USERNAME . '".');
} else {
$admin = User::create([
'fullname' => 'System Admin',
'username' => self::ADMIN_USERNAME,
'email' => 'admin@tgs.com.vn',
'email_verified_at' => now(),
'password' => Hash::make(self::ADMIN_PASSWORD),
]);
$this->command?->info('Đã tạo admin "' . self::ADMIN_USERNAME . '".');
}
// status nằm ngoài $fillable nên phải gán thẳng.
$admin->forceFill(['status' => 1])->save();
if (method_exists($admin, 'syncRoles') && \Spatie\Permission\Models\Role::where('name', 'Full Administrator')->exists()) {
$admin->syncRoles(['Full Administrator']);
$this->command?->info(' → gán role Full Administrator.');
}
}
private function seedCustomer(): void
{
$customer = Customer::where('email', self::CUSTOMER_EMAIL)->first();
if ($customer) {
$customer->update(['password' => Hash::make(self::CUSTOMER_PASSWORD)]);
$this->command?->info('Khách hàng đã có — đặt lại mật khẩu cho ' . self::CUSTOMER_EMAIL . '.');
} else {
$customer = Customer::create([
'fullname' => 'Phạm Bảo (test)',
'username' => 'pbao.dev',
'email' => self::CUSTOMER_EMAIL,
'phone' => '0900000000',
'password' => Hash::make(self::CUSTOMER_PASSWORD),
'email_verified_at' => now(),
'email_verify' => 1,
'address' => '128/38 Tân Hương, Phú Thọ Hòa, TP. Hồ Chí Minh',
]);
$this->command?->info('Đã tạo khách hàng ' . self::CUSTOMER_EMAIL . '.');
}
// Nạp sẵn số dư để đặt và thanh toán đơn thử, kể cả các ca trả từng phần.
$customer->forceFill([
'status' => 1,
'total' => max((int) $customer->total, 50_000_000),
])->save();
if (empty($customer->uuid)) {
$customer->forceFill(['uuid' => (string) Str::uuid()])->save();
}
$this->command?->info(' → số dư: ' . number_format((int) $customer->fresh()->total) . ' VNĐ');
}
}