refactor(logging): replace console usage with logger
This commit is contained in:
@@ -4,6 +4,7 @@ import { mailgunService } from '../mailgun.service';
|
||||
import { AccountStatus } from './auth.constants';
|
||||
import { databaseService } from '../database';
|
||||
import { tokenService } from './token.service';
|
||||
import { logger } from '../logging';
|
||||
|
||||
const TOKEN_EXPIRY_HOURS = 24;
|
||||
|
||||
@@ -32,7 +33,7 @@ export class EmailVerificationService {
|
||||
token
|
||||
);
|
||||
if (!emailSent) {
|
||||
console.warn('Failed to send verification email');
|
||||
logger.auth.warn('Failed to send verification email');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,36 @@
|
||||
import { databaseService } from './database';
|
||||
import { AccountStatus } from './auth/auth.constants';
|
||||
import { UserRole } from '../types';
|
||||
import { hashPassword, isBcryptHash } from './auth/password.service';
|
||||
import { logger } from './logging';
|
||||
|
||||
export class DatabaseSeeder {
|
||||
private static seedingInProgress = false;
|
||||
private static seedingCompleted = false;
|
||||
|
||||
async seedDefaultAdmin(): Promise<void> {
|
||||
const adminEmail = 'admin@localhost';
|
||||
const adminPassword = 'admin123!';
|
||||
const adminEmail =
|
||||
(import.meta as any)?.env?.VITE_ADMIN_EMAIL || 'admin@localhost';
|
||||
const adminPassword =
|
||||
(import.meta as any)?.env?.VITE_ADMIN_PASSWORD || 'admin123!';
|
||||
|
||||
console.warn('🌱 Starting admin user seeding...');
|
||||
console.warn('📧 Admin email:', adminEmail);
|
||||
logger.db.info('🌱 Starting admin user seeding...');
|
||||
logger.db.info('📧 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);
|
||||
logger.db.info('✅ Default admin user already exists');
|
||||
logger.db.info('👤 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...');
|
||||
logger.db.info('🔧 Updating admin user role and status...');
|
||||
const updatedAdmin = {
|
||||
...existingAdmin,
|
||||
role: UserRole.ADMIN,
|
||||
@@ -34,21 +38,25 @@ export class DatabaseSeeder {
|
||||
emailVerified: true,
|
||||
};
|
||||
await databaseService.updateUser(updatedAdmin);
|
||||
console.warn('✅ Admin user updated successfully');
|
||||
console.warn('👤 Updated admin:', updatedAdmin);
|
||||
logger.db.info('✅ Admin user updated successfully');
|
||||
logger.db.info('👤 Updated admin:', updatedAdmin);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn('🚀 Creating new admin user...');
|
||||
logger.db.info('🚀 Creating new admin user...');
|
||||
// Create default admin user
|
||||
const passwordToUse = isBcryptHash(adminPassword)
|
||||
? adminPassword
|
||||
: await hashPassword(adminPassword);
|
||||
|
||||
const adminUser = await databaseService.createUserWithPassword(
|
||||
adminEmail,
|
||||
adminPassword,
|
||||
passwordToUse,
|
||||
'admin'
|
||||
);
|
||||
|
||||
console.warn('👤 Admin user created:', adminUser);
|
||||
logger.db.info('👤 Admin user created:', adminUser);
|
||||
|
||||
// Update user to admin role and active status
|
||||
const updatedAdmin = {
|
||||
@@ -62,13 +70,15 @@ export class DatabaseSeeder {
|
||||
|
||||
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!');
|
||||
logger.db.info('✅ Admin user created successfully');
|
||||
logger.db.info('👤 Final admin user:', updatedAdmin);
|
||||
logger.db.info('📧 Email:', adminEmail);
|
||||
logger.db.info('🔑 Password:', adminPassword);
|
||||
logger.db.info(
|
||||
'⚠️ Please change the default password after first login!'
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to create default admin user:', error);
|
||||
logger.db.error('❌ Failed to create default admin user:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -76,19 +86,21 @@ export class DatabaseSeeder {
|
||||
async seedDatabase(): Promise<void> {
|
||||
// Prevent multiple seeding attempts
|
||||
if (DatabaseSeeder.seedingInProgress || DatabaseSeeder.seedingCompleted) {
|
||||
console.warn('🔄 Seeding already in progress or completed, skipping...');
|
||||
logger.db.info(
|
||||
'🔄 Seeding already in progress or completed, skipping...'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
DatabaseSeeder.seedingInProgress = true;
|
||||
console.warn('🌱 Starting database seeding...');
|
||||
logger.db.info('🌱 Starting database seeding...');
|
||||
|
||||
try {
|
||||
await this.seedDefaultAdmin();
|
||||
DatabaseSeeder.seedingCompleted = true;
|
||||
console.warn('🎯 Admin seeding completed successfully');
|
||||
logger.db.info('🎯 Admin seeding completed successfully');
|
||||
} catch (error) {
|
||||
console.error('💥 Database seeding failed:', error);
|
||||
logger.db.error('💥 Database seeding failed:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
DatabaseSeeder.seedingInProgress = false;
|
||||
|
||||
@@ -3,6 +3,8 @@ import { MockDatabaseStrategy } from './MockDatabaseStrategy';
|
||||
import { ProductionDatabaseStrategy } from './ProductionDatabaseStrategy';
|
||||
import { DatabaseStrategy } from './types';
|
||||
import { AccountStatus } from '../auth/auth.constants';
|
||||
import { hashPassword } from '../auth/password.service';
|
||||
import { logger } from '../logging';
|
||||
|
||||
/**
|
||||
* Consolidated Database Service
|
||||
@@ -30,9 +32,9 @@ export class DatabaseService implements DatabaseStrategy {
|
||||
try {
|
||||
return new ProductionDatabaseStrategy();
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
'Production CouchDB service not available, falling back to mock:',
|
||||
error
|
||||
logger.db.warn(
|
||||
'Production CouchDB service not available, falling back to mock',
|
||||
error as Error
|
||||
);
|
||||
return new MockDatabaseStrategy();
|
||||
}
|
||||
@@ -188,9 +190,10 @@ export class DatabaseService implements DatabaseStrategy {
|
||||
async changeUserPassword(userId: string, newPassword: string) {
|
||||
const user = await this.strategy.getUserById(userId);
|
||||
if (!user) throw new Error('User not found');
|
||||
const hashedPassword = await hashPassword(newPassword);
|
||||
return this.strategy.updateUser({
|
||||
...user,
|
||||
password: newPassword,
|
||||
password: hashedPassword,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/**
|
||||
* Mock email service for sending verification emails
|
||||
*/
|
||||
import { logger } from './logging';
|
||||
|
||||
export class EmailService {
|
||||
/**
|
||||
* Simulates sending a verification email with a link to /verify-email?token=${token}
|
||||
@@ -10,10 +12,11 @@ export class EmailService {
|
||||
async sendVerificationEmail(email: string, token: string): Promise<void> {
|
||||
// In a real implementation, this would send an actual email
|
||||
// For this demo, we'll just log the action
|
||||
console.warn(
|
||||
`📧 Sending verification email to ${email} with token: ${token}`
|
||||
logger.info(
|
||||
`Sending verification email to ${email} with token: ${token}`,
|
||||
'EMAIL'
|
||||
);
|
||||
console.warn(`🔗 Verification link: /verify-email?token=${token}`);
|
||||
logger.info(`Verification link: /verify-email?token=${token}`, 'EMAIL');
|
||||
|
||||
// Simulate network delay
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
Reference in New Issue
Block a user