refactor(logging): replace console usage with logger
This commit is contained in:
@@ -3,6 +3,7 @@ import { User, UserRole } from '../../types';
|
||||
import { AccountStatus } from '../../services/auth/auth.constants';
|
||||
import { databaseService } from '../../services/database';
|
||||
import { useUser } from '../../contexts/UserContext';
|
||||
import { logger } from '../../services/logging';
|
||||
|
||||
interface AdminInterfaceProps {
|
||||
onClose: () => void;
|
||||
@@ -64,7 +65,7 @@ const AdminInterface: React.FC<AdminInterfaceProps> = ({ onClose }) => {
|
||||
setUsers(users);
|
||||
} catch (error) {
|
||||
setError('Failed to load users');
|
||||
console.error('Error loading users:', error);
|
||||
logger.ui.error('Error loading users', error as Error);
|
||||
pushToast('Failed to load users', 'error');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -78,7 +79,7 @@ const AdminInterface: React.FC<AdminInterfaceProps> = ({ onClose }) => {
|
||||
await loadUsers();
|
||||
} catch (error) {
|
||||
setError('Failed to suspend user');
|
||||
console.error('Error suspending user:', error);
|
||||
logger.ui.error('Error suspending user', error as Error);
|
||||
pushToast('Failed to suspend user', 'error');
|
||||
}
|
||||
};
|
||||
@@ -90,7 +91,7 @@ const AdminInterface: React.FC<AdminInterfaceProps> = ({ onClose }) => {
|
||||
await loadUsers();
|
||||
} catch (error) {
|
||||
setError('Failed to activate user');
|
||||
console.error('Error activating user:', error);
|
||||
logger.ui.error('Error activating user', error as Error);
|
||||
pushToast('Failed to activate user', 'error');
|
||||
}
|
||||
};
|
||||
@@ -110,7 +111,7 @@ const AdminInterface: React.FC<AdminInterfaceProps> = ({ onClose }) => {
|
||||
await loadUsers();
|
||||
} catch (error) {
|
||||
setError('Failed to delete user');
|
||||
console.error('Error deleting user:', error);
|
||||
logger.ui.error('Error deleting user', error as Error);
|
||||
pushToast('Failed to delete user', 'error');
|
||||
} finally {
|
||||
setIsDeletingUser(false);
|
||||
@@ -137,7 +138,7 @@ const AdminInterface: React.FC<AdminInterfaceProps> = ({ onClose }) => {
|
||||
pushToast('Password changed successfully', 'success');
|
||||
} catch (error) {
|
||||
setError('Failed to change password');
|
||||
console.error('Error changing password:', error);
|
||||
logger.ui.error('Error changing password', error as Error);
|
||||
pushToast('Failed to change password', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import { Medication, Frequency } from '../../types';
|
||||
import { medicationIcons } from '../icons/Icons';
|
||||
import { logger } from '../../services/logging';
|
||||
|
||||
interface AddMedicationModalProps {
|
||||
isOpen: boolean;
|
||||
@@ -67,7 +68,7 @@ const AddMedicationModal: React.FC<AddMedicationModalProps> = ({
|
||||
icon,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to add medication', error);
|
||||
logger.ui.error('Failed to add medication', error as Error);
|
||||
alert('There was an error saving your medication. Please try again.');
|
||||
setIsSaving(false);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import { Medication, Frequency } from '../../types';
|
||||
import { medicationIcons } from '../icons/Icons';
|
||||
import { logger } from '../../services/logging';
|
||||
|
||||
interface EditMedicationModalProps {
|
||||
isOpen: boolean;
|
||||
@@ -72,7 +73,7 @@ const EditMedicationModal: React.FC<EditMedicationModalProps> = ({
|
||||
icon,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to update medication', error);
|
||||
logger.ui.error('Failed to update medication', error as Error);
|
||||
alert('There was an error updating your medication. Please try again.');
|
||||
setIsSaving(false);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MAX_REMINDER_FREQUENCY_MINUTES,
|
||||
validateReminderInputs,
|
||||
} from './reminderValidation';
|
||||
import { logger } from '../../services/logging';
|
||||
|
||||
interface AddReminderModalProps {
|
||||
isOpen: boolean;
|
||||
@@ -77,7 +78,7 @@ const AddReminderModal: React.FC<AddReminderModalProps> = ({
|
||||
endTime,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to add reminder', error);
|
||||
logger.ui.error('Failed to add reminder', error as Error);
|
||||
alert('There was an error saving your reminder. Please try again.');
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MAX_REMINDER_FREQUENCY_MINUTES,
|
||||
validateReminderInputs,
|
||||
} from './reminderValidation';
|
||||
import { logger } from '../../services/logging';
|
||||
|
||||
interface EditReminderModalProps {
|
||||
isOpen: boolean;
|
||||
@@ -79,7 +80,7 @@ const EditReminderModal: React.FC<EditReminderModalProps> = ({
|
||||
endTime,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to update reminder', error);
|
||||
logger.ui.error('Failed to update reminder', error as Error);
|
||||
alert('There was an error updating your reminder. Please try again.');
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { logger } from '../services/logging';
|
||||
|
||||
/**
|
||||
* Unified Application Configuration System
|
||||
*
|
||||
@@ -754,10 +756,10 @@ function validateConfig(config: UnifiedConfig): void {
|
||||
|
||||
// Log warnings and throw errors
|
||||
if (warnings.length > 0) {
|
||||
console.warn('⚠️ Configuration warnings:', warnings);
|
||||
logger.warn('Configuration warnings', 'CONFIG', warnings);
|
||||
}
|
||||
if (errors.length > 0) {
|
||||
console.error('❌ Configuration errors:', errors);
|
||||
logger.error('Configuration errors', 'CONFIG', errors);
|
||||
throw new Error(`Configuration validation failed: ${errors.join(', ')}`);
|
||||
}
|
||||
}
|
||||
@@ -956,7 +958,7 @@ export function exportAsEnvVars(
|
||||
*/
|
||||
export function logConfig(): void {
|
||||
if (unifiedConfig.features.debugMode) {
|
||||
console.warn('🔧 Unified Configuration (Single Source of Truth):', {
|
||||
logger.info('Unified Configuration (Single Source of Truth)', 'CONFIG', {
|
||||
environment: unifiedConfig.app.environment,
|
||||
app: unifiedConfig.app.name,
|
||||
version: unifiedConfig.app.version,
|
||||
|
||||
+8
-2
@@ -1,12 +1,18 @@
|
||||
import { databaseSeeder } from '../services/database.seeder';
|
||||
import { logger } from '../services/logging';
|
||||
|
||||
const run = async () => {
|
||||
try {
|
||||
await databaseSeeder.seedDatabase();
|
||||
console.log('✅ Database seeding complete');
|
||||
logger.info('Database seeding complete', 'SEEDER');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ Database seeding failed', error);
|
||||
logger.error(
|
||||
'Database seeding failed',
|
||||
'SEEDER',
|
||||
undefined,
|
||||
error as Error
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+34
-22
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -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