Files
rxminder/services/database/MockDatabaseStrategy.ts
William Valentin 8c591563c9 feat: consolidate architecture and eliminate code duplication
🏗️ Major architectural improvements:

Database Layer:
- Consolidated duplicate CouchDB services (~800 lines of duplicated code eliminated)
- Implemented strategy pattern with MockDatabaseStrategy and ProductionDatabaseStrategy
- Created unified DatabaseService with automatic environment detection
- Maintained backward compatibility via updated factory pattern

Configuration System:
- Centralized all environment variables in single config/app.config.ts
- Added comprehensive configuration validation with clear error messages
- Eliminated hardcoded base URLs and scattered env var access across 8+ files
- Supports both legacy and new environment variable names

Logging Infrastructure:
- Replaced 25+ scattered console.log statements with structured Logger service
- Added log levels (ERROR, WARN, INFO, DEBUG, TRACE) and contexts (AUTH, DATABASE, API, UI)
- Production-safe logging with automatic level adjustment
- Development helpers for debugging and performance monitoring

Docker & Deployment:
- Removed duplicate docker/Dockerfile configuration
- Enhanced root Dockerfile with comprehensive environment variable support
- Added proper health checks and security improvements

Code Quality:
- Fixed package name consistency (rxminder → RxMinder)
- Updated services to use centralized configuration and logging
- Resolved all ESLint errors and warnings
- Added comprehensive documentation and migration guides

📊 Impact:
- Eliminated ~500 lines of duplicate code
- Single source of truth for database, configuration, and logging
- Better type safety and error handling
- Improved development experience and maintainability

📚 Documentation:
- Added ARCHITECTURE_MIGRATION.md with detailed migration guide
- Created IMPLEMENTATION_SUMMARY.md with metrics and benefits
- Inline documentation for all new services and interfaces

🔄 Backward Compatibility:
- All existing code continues to work unchanged
- Legacy services show deprecation warnings but remain functional
- Gradual migration path available for development teams

Breaking Changes: None (full backward compatibility maintained)
2025-09-08 01:09:48 -07:00

264 lines
7.0 KiB
TypeScript

import { v4 as uuidv4 } from 'uuid';
import {
User,
Medication,
UserSettings,
TakenDoses,
CustomReminder,
CouchDBDocument,
UserRole,
} from '../../types';
import { AccountStatus } from '../auth/auth.constants';
import { DatabaseStrategy, DatabaseError } from './types';
// Simulate network latency for realistic testing
const latency = () =>
new Promise(res => setTimeout(res, Math.random() * 200 + 50));
export class MockDatabaseStrategy implements DatabaseStrategy {
private async getDb<T>(dbName: string): Promise<T[]> {
await latency();
const db = localStorage.getItem(dbName);
return db ? JSON.parse(db) : [];
}
private async saveDb<T>(dbName: string, data: T[]): Promise<void> {
await latency();
localStorage.setItem(dbName, JSON.stringify(data));
}
private async getDoc<T extends CouchDBDocument>(
dbName: string,
id: string
): Promise<T | null> {
const allDocs = await this.getDb<T>(dbName);
return allDocs.find(doc => doc._id === id) || null;
}
private async query<T>(
dbName: string,
predicate: (doc: T) => boolean
): Promise<T[]> {
const allDocs = await this.getDb<T>(dbName);
return allDocs.filter(predicate);
}
private async putDoc<T extends CouchDBDocument>(
dbName: string,
doc: T
): Promise<T> {
const allDocs = await this.getDb<T>(dbName);
const existingIndex = allDocs.findIndex(d => d._id === doc._id);
if (existingIndex !== -1) {
const existing = allDocs[existingIndex];
if (existing._rev !== doc._rev) {
throw new DatabaseError(`Document update conflict for ${doc._id}`, 409);
}
allDocs[existingIndex] = { ...doc, _rev: uuidv4() };
} else {
allDocs.push({ ...doc, _rev: uuidv4() });
}
await this.saveDb(dbName, allDocs);
return allDocs.find(d => d._id === doc._id)!;
}
private async deleteDoc(dbName: string, id: string): Promise<boolean> {
const allDocs = await this.getDb<CouchDBDocument>(dbName);
const filtered = allDocs.filter(doc => doc._id !== id);
if (filtered.length === allDocs.length) {
return false; // Document not found
}
await this.saveDb(dbName, filtered);
return true;
}
// User operations
async createUser(user: Omit<User, '_id' | '_rev'>): Promise<User> {
const newUser: User = {
...user,
_id: uuidv4(),
_rev: uuidv4(),
status: user.status || AccountStatus.ACTIVE,
role: user.role || UserRole.USER,
createdAt: user.createdAt || new Date(),
};
return this.putDoc('users', newUser);
}
async updateUser(user: User): Promise<User> {
return this.putDoc('users', user);
}
async getUserById(id: string): Promise<User | null> {
return this.getDoc<User>('users', id);
}
async findUserByEmail(email: string): Promise<User | null> {
const users = await this.query<User>('users', user => user.email === email);
return users[0] || null;
}
async deleteUser(id: string): Promise<boolean> {
return this.deleteDoc('users', id);
}
async getAllUsers(): Promise<User[]> {
return this.getDb<User>('users');
}
// Medication operations
async createMedication(
userId: string,
medication: Omit<Medication, '_id' | '_rev'>
): Promise<Medication> {
const newMedication: Medication = {
...medication,
_id: `${userId}-med-${uuidv4()}`,
_rev: uuidv4(),
};
return this.putDoc('medications', newMedication);
}
async updateMedication(medication: Medication): Promise<Medication> {
return this.putDoc('medications', medication);
}
async getMedications(userId: string): Promise<Medication[]> {
return this.query<Medication>('medications', med =>
med._id.startsWith(`${userId}-med-`)
);
}
async deleteMedication(id: string): Promise<boolean> {
return this.deleteDoc('medications', id);
}
// User settings operations
async getUserSettings(userId: string): Promise<UserSettings> {
const existing = await this.getDoc<UserSettings>('settings', userId);
if (existing) {
return existing;
}
// Create default settings if none exist
const defaultSettings: UserSettings = {
_id: userId,
_rev: uuidv4(),
notificationsEnabled: true,
hasCompletedOnboarding: false,
};
return this.putDoc('settings', defaultSettings);
}
async updateUserSettings(settings: UserSettings): Promise<UserSettings> {
return this.putDoc('settings', settings);
}
// Taken doses operations
async getTakenDoses(userId: string): Promise<TakenDoses> {
const existing = await this.getDoc<TakenDoses>('taken_doses', userId);
if (existing) {
return existing;
}
// Create default taken doses record if none exists
const defaultTakenDoses: TakenDoses = {
_id: userId,
_rev: uuidv4(),
doses: {},
};
return this.putDoc('taken_doses', defaultTakenDoses);
}
async updateTakenDoses(takenDoses: TakenDoses): Promise<TakenDoses> {
return this.putDoc('taken_doses', takenDoses);
}
// Custom reminders operations
async createCustomReminder(
userId: string,
reminder: Omit<CustomReminder, '_id' | '_rev'>
): Promise<CustomReminder> {
const newReminder: CustomReminder = {
...reminder,
_id: `${userId}-reminder-${uuidv4()}`,
_rev: uuidv4(),
};
return this.putDoc('reminders', newReminder);
}
async updateCustomReminder(
reminder: CustomReminder
): Promise<CustomReminder> {
return this.putDoc('reminders', reminder);
}
async getCustomReminders(userId: string): Promise<CustomReminder[]> {
return this.query<CustomReminder>('reminders', reminder =>
reminder._id.startsWith(`${userId}-reminder-`)
);
}
async deleteCustomReminder(id: string): Promise<boolean> {
return this.deleteDoc('reminders', id);
}
// User operations with password
async createUserWithPassword(
email: string,
hashedPassword: string,
username?: string
): Promise<User> {
// Check if user already exists
const existingUser = await this.findUserByEmail(email);
if (existingUser) {
throw new DatabaseError('User already exists with this email', 409);
}
return this.createUser({
username: username || email.split('@')[0],
email,
password: hashedPassword,
emailVerified: false,
status: AccountStatus.PENDING,
role: UserRole.USER,
createdAt: new Date(),
});
}
async createUserFromOAuth(
email: string,
username: string,
_provider: string
): Promise<User> {
// Check if user already exists
const existingUser = await this.findUserByEmail(email);
if (existingUser) {
// Update last login and return existing user
return this.updateUser({
...existingUser,
lastLoginAt: new Date(),
});
}
return this.createUser({
username,
email,
emailVerified: true, // OAuth emails are considered verified
status: AccountStatus.ACTIVE,
role: UserRole.USER,
createdAt: new Date(),
lastLoginAt: new Date(),
});
}
}