Files
gridpilot.gg/packages/notifications/infrastructure/repositories/InMemoryNotificationPreferenceRepository.ts
2025-12-14 18:11:59 +01:00

84 lines
3.4 KiB
TypeScript

/**
* In-Memory Implementation: InMemoryNotificationPreferenceRepository
*
* Provides an in-memory storage implementation for notification preferences.
*/
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
export class InMemoryNotificationPreferenceRepository implements INotificationPreferenceRepository {
private preferences: Map<string, NotificationPreference> = new Map();
private readonly logger: ILogger;
constructor(logger: ILogger, initialPreferences: NotificationPreference[] = []) {
this.logger = logger;
this.logger.info('InMemoryNotificationPreferenceRepository initialized.');
initialPreferences.forEach(pref => {
this.preferences.set(pref.driverId, pref);
this.logger.debug(`Seeded preference for driver: ${pref.driverId}`);
});
}
async findByDriverId(driverId: string): Promise<NotificationPreference | null> {
this.logger.debug(`Finding notification preference for driver: ${driverId}`);
try {
const preference = this.preferences.get(driverId) || null;
if (preference) {
this.logger.info(`Found preference for driver: ${driverId}`);
} else {
this.logger.warn(`Preference not found for driver: ${driverId}`);
}
return preference;
} catch (error) {
this.logger.error(`Error finding preference for driver ${driverId}:`, error);
throw error;
}
}
async save(preference: NotificationPreference): Promise<void> {
this.logger.debug(`Saving notification preference for driver: ${preference.driverId}`);
try {
this.preferences.set(preference.driverId, preference);
this.logger.info(`Preference for driver ${preference.driverId} saved successfully.`);
} catch (error) {
this.logger.error(`Error saving preference for driver ${preference.driverId}:`, error);
throw error;
}
}
async delete(driverId: string): Promise<void> {
this.logger.debug(`Deleting notification preference for driver: ${driverId}`);
try {
if (this.preferences.delete(driverId)) {
this.logger.info(`Preference for driver ${driverId} deleted successfully.`);
} else {
this.logger.warn(`Preference for driver ${driverId} not found for deletion.`);
}
} catch (error) {
this.logger.error(`Error deleting preference for driver ${driverId}:`, error);
throw error;
}
}
async getOrCreateDefault(driverId: string): Promise<NotificationPreference> {
this.logger.debug(`Getting or creating default notification preference for driver: ${driverId}`);
try {
const existing = this.preferences.get(driverId);
if (existing) {
this.logger.debug(`Existing preference found for driver: ${driverId}.`);
return existing;
}
this.logger.info(`Creating default preference for driver: ${driverId}.`);
const defaultPreference = NotificationPreference.createDefault(driverId);
this.preferences.set(driverId, defaultPreference);
this.logger.info(`Default preference created and saved for driver: ${driverId}.`);
return defaultPreference;
} catch (error) {
this.logger.error(`Error getting or creating default preference for driver ${driverId}:`, error);
throw error;
}
}
}