Initial commit: Complete NodeJS-native setup
- Migrated from Python pre-commit to NodeJS-native solution - Reorganized documentation structure - Set up Husky + lint-staged for efficient pre-commit hooks - Fixed Dockerfile healthcheck issue - Added comprehensive documentation index
This commit is contained in:
101
services/database.seeder.ts
Normal file
101
services/database.seeder.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { dbService } from './couchdb.factory';
|
||||
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.log('🌱 Starting admin user seeding...');
|
||||
console.log('📧 Admin email:', adminEmail);
|
||||
|
||||
try {
|
||||
// Check if admin already exists
|
||||
const existingAdmin = await dbService.findUserByEmail(adminEmail);
|
||||
|
||||
if (existingAdmin) {
|
||||
console.log('✅ Default admin user already exists');
|
||||
console.log('👤 Existing admin:', existingAdmin);
|
||||
|
||||
// Check if admin needs to be updated to correct role/status
|
||||
if (
|
||||
existingAdmin.role !== UserRole.ADMIN ||
|
||||
existingAdmin.status !== AccountStatus.ACTIVE
|
||||
) {
|
||||
console.log('🔧 Updating admin user role and status...');
|
||||
const updatedAdmin = {
|
||||
...existingAdmin,
|
||||
role: UserRole.ADMIN,
|
||||
status: AccountStatus.ACTIVE,
|
||||
emailVerified: true,
|
||||
};
|
||||
await dbService.updateUser(updatedAdmin);
|
||||
console.log('✅ Admin user updated successfully');
|
||||
console.log('👤 Updated admin:', updatedAdmin);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔨 Creating new admin user...');
|
||||
// Create default admin user
|
||||
const adminUser = await dbService.createUserWithPassword(
|
||||
adminEmail,
|
||||
adminPassword,
|
||||
'admin'
|
||||
);
|
||||
|
||||
console.log('👤 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 dbService.updateUser(updatedAdmin);
|
||||
|
||||
console.log('✅ Default admin user created successfully');
|
||||
console.log('👤 Final admin user:', updatedAdmin);
|
||||
console.log('📧 Email:', adminEmail);
|
||||
console.log('🔑 Password:', adminPassword);
|
||||
console.log('⚠️ 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.log('🔄 Seeding already in progress or completed, skipping...');
|
||||
return;
|
||||
}
|
||||
|
||||
DatabaseSeeder.seedingInProgress = true;
|
||||
console.log('🌱 Starting database seeding...');
|
||||
|
||||
try {
|
||||
await this.seedDefaultAdmin();
|
||||
DatabaseSeeder.seedingCompleted = true;
|
||||
console.log('🎉 Database 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
|
||||
Reference in New Issue
Block a user