- Add DatabaseService with MockDatabaseStrategy and ProductionDatabaseStrategy - Use strategy pattern to switch between test and production database implementations - Improve type safety and testability of database operations - Update database seeder to use new database service architecture This is the foundation for modernizing the database layer.
102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { databaseService } from './database';
|
|
import { AccountStatus } from './auth/auth.constants';
|
|
import { UserRole } from '../types';
|
|
|
|
export class DatabaseSeeder {
|
|
private static seedingInProgress = false;
|
|
private static seedingCompleted = false;
|
|
|
|
async seedDefaultAdmin(): Promise<void> {
|
|
const adminEmail = 'admin@localhost';
|
|
const adminPassword = 'admin123!';
|
|
|
|
console.warn('🌱 Starting admin user seeding...');
|
|
console.warn('📧 Admin email:', adminEmail);
|
|
|
|
try {
|
|
// Check if admin already exists
|
|
const existingAdmin = await databaseService.findUserByEmail(adminEmail);
|
|
|
|
if (existingAdmin) {
|
|
console.warn('✅ Default admin user already exists');
|
|
console.warn('👤 Existing admin:', existingAdmin);
|
|
|
|
// Check if admin needs to be updated to correct role/status
|
|
if (
|
|
existingAdmin.role !== UserRole.ADMIN ||
|
|
existingAdmin.status !== AccountStatus.ACTIVE
|
|
) {
|
|
console.warn('🔧 Updating admin user role and status...');
|
|
const updatedAdmin = {
|
|
...existingAdmin,
|
|
role: UserRole.ADMIN,
|
|
status: AccountStatus.ACTIVE,
|
|
emailVerified: true,
|
|
};
|
|
await databaseService.updateUser(updatedAdmin);
|
|
console.warn('✅ Admin user updated successfully');
|
|
console.warn('👤 Updated admin:', updatedAdmin);
|
|
}
|
|
return;
|
|
}
|
|
|
|
console.warn('🚀 Creating new admin user...');
|
|
// Create default admin user
|
|
const adminUser = await databaseService.createUserWithPassword(
|
|
adminEmail,
|
|
adminPassword,
|
|
'admin'
|
|
);
|
|
|
|
console.warn('👤 Admin user created:', adminUser);
|
|
|
|
// Update user to admin role and active status
|
|
const updatedAdmin = {
|
|
...adminUser,
|
|
role: UserRole.ADMIN,
|
|
status: AccountStatus.ACTIVE,
|
|
emailVerified: true,
|
|
createdAt: new Date(),
|
|
lastLoginAt: new Date(),
|
|
};
|
|
|
|
await databaseService.updateUser(updatedAdmin);
|
|
|
|
console.warn('✅ Admin user created successfully');
|
|
console.warn('👤 Final admin user:', updatedAdmin);
|
|
console.warn('📧 Email:', adminEmail);
|
|
console.warn('🔑 Password:', adminPassword);
|
|
console.warn('⚠️ Please change the default password after first login!');
|
|
} catch (error) {
|
|
console.error('❌ Failed to create default admin user:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async seedDatabase(): Promise<void> {
|
|
// Prevent multiple seeding attempts
|
|
if (DatabaseSeeder.seedingInProgress || DatabaseSeeder.seedingCompleted) {
|
|
console.warn('🔄 Seeding already in progress or completed, skipping...');
|
|
return;
|
|
}
|
|
|
|
DatabaseSeeder.seedingInProgress = true;
|
|
console.warn('🌱 Starting database seeding...');
|
|
|
|
try {
|
|
await this.seedDefaultAdmin();
|
|
DatabaseSeeder.seedingCompleted = true;
|
|
console.warn('🎯 Admin seeding completed successfully');
|
|
} catch (error) {
|
|
console.error('💥 Database seeding failed:', error);
|
|
throw error;
|
|
} finally {
|
|
DatabaseSeeder.seedingInProgress = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
export const databaseSeeder = new DatabaseSeeder();
|
|
|
|
// The seeding will be called explicitly from App.tsx
|