55 lines
2.5 KiB
TypeScript
55 lines
2.5 KiB
TypeScript
import { INotificationPreferenceRepository } from '@core/notifications/domain/repositories/INotificationPreferenceRepository';
|
|
import { NotificationPreference } from '@core/notifications/domain/entities/NotificationPreference';
|
|
import { Logger } from '@core/shared/application';
|
|
|
|
export class InMemoryNotificationPreferenceRepository implements INotificationPreferenceRepository {
|
|
private preferences: Map<string, NotificationPreference> = new Map();
|
|
|
|
constructor(private readonly logger: Logger, initialPreferences: NotificationPreference[] = []) {
|
|
this.logger.info('InMemoryNotificationPreferenceRepository initialized.');
|
|
for (const pref of initialPreferences) {
|
|
this.preferences.set(pref.id, pref);
|
|
this.logger.debug(`Seeded preference: ${pref.id}.`);
|
|
}
|
|
}
|
|
|
|
async findByDriverId(driverId: string): Promise<NotificationPreference | null> {
|
|
this.logger.debug(`[InMemoryNotificationPreferenceRepository] Finding preferences for driver: ${driverId}`);
|
|
const preference = this.preferences.get(driverId) ?? null;
|
|
if (preference) {
|
|
this.logger.info(`Found preferences for driver: ${driverId}.`);
|
|
} else {
|
|
this.logger.warn(`No preferences found for driver: ${driverId}.`);
|
|
}
|
|
return Promise.resolve(preference);
|
|
}
|
|
|
|
async save(preference: NotificationPreference): Promise<void> {
|
|
this.logger.debug(`[InMemoryNotificationPreferenceRepository] Saving preferences for driver: ${preference.driverId}.`);
|
|
this.preferences.set(preference.id, preference);
|
|
this.logger.info(`Preferences for driver ${preference.driverId} saved successfully.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async delete(driverId: string): Promise<void> {
|
|
this.logger.debug(`[InMemoryNotificationPreferenceRepository] Deleting preferences for driver: ${driverId}.`);
|
|
if (this.preferences.delete(driverId)) {
|
|
this.logger.info(`Preferences for driver ${driverId} deleted successfully.`);
|
|
} else {
|
|
this.logger.warn(`No preferences found for driver ${driverId} to delete.`);
|
|
}
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async getOrCreateDefault(driverId: string): Promise<NotificationPreference> {
|
|
this.logger.debug(`[InMemoryNotificationPreferenceRepository] Getting or creating default preferences for driver: ${driverId}.`);
|
|
let preference = await this.findByDriverId(driverId);
|
|
if (!preference) {
|
|
this.logger.info(`Creating default preferences for new driver: ${driverId}.`);
|
|
preference = NotificationPreference.createDefault(driverId);
|
|
await this.save(preference);
|
|
}
|
|
return preference;
|
|
}
|
|
}
|