refactor adapters
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* In-Memory Implementation: InMemoryNotificationRepository
|
||||
*
|
||||
* Provides an in-memory storage implementation for notifications.
|
||||
*/
|
||||
|
||||
import { Notification } from '../../domain/entities/Notification';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
import type { NotificationType } from '../../domain/types/NotificationTypes';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryNotificationRepository implements INotificationRepository {
|
||||
private notifications: Map<string, Notification> = new Map();
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger, initialNotifications: Notification[] = []) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryNotificationRepository initialized.');
|
||||
initialNotifications.forEach(notification => {
|
||||
this.notifications.set(notification.id, notification);
|
||||
this.logger.debug(`Seeded notification: ${notification.id}`);
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Notification | null> {
|
||||
this.logger.debug(`Finding notification by ID: ${id}`);
|
||||
try {
|
||||
const notification = this.notifications.get(id) || null;
|
||||
if (notification) {
|
||||
this.logger.info(`Found notification with ID: ${id}`);
|
||||
} else {
|
||||
this.logger.warn(`Notification with ID ${id} not found.`);
|
||||
}
|
||||
return notification;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding notification by ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByRecipientId(recipientId: string): Promise<Notification[]> {
|
||||
this.logger.debug(`Finding notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const notifications = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId)
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.info(`Found ${notifications.length} notifications for recipient ID: ${recipientId}.`);
|
||||
return notifications;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findUnreadByRecipientId(recipientId: string): Promise<Notification[]> {
|
||||
this.logger.debug(`Finding unread notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const notifications = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread())
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.info(`Found ${notifications.length} unread notifications for recipient ID: ${recipientId}.`);
|
||||
return notifications;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding unread notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByRecipientIdAndType(recipientId: string, type: NotificationType): Promise<Notification[]> {
|
||||
this.logger.debug(`Finding notifications for recipient ID: ${recipientId}, type: ${type}`);
|
||||
try {
|
||||
const notifications = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.type === type)
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.info(`Found ${notifications.length} notifications for recipient ID: ${recipientId}, type: ${type}.`);
|
||||
return notifications;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding notifications for recipient ID ${recipientId}, type ${type}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async countUnreadByRecipientId(recipientId: string): Promise<number> {
|
||||
this.logger.debug(`Counting unread notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const count = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread())
|
||||
.length;
|
||||
this.logger.info(`Counted ${count} unread notifications for recipient ID: ${recipientId}.`);
|
||||
return count;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error counting unread notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create(notification: Notification): Promise<void> {
|
||||
this.logger.debug(`Creating notification: ${notification.id}`);
|
||||
try {
|
||||
if (this.notifications.has(notification.id)) {
|
||||
this.logger.warn(`Notification with ID ${notification.id} already exists. Throwing error.`);
|
||||
throw new Error(`Notification with ID ${notification.id} already exists`);
|
||||
}
|
||||
this.notifications.set(notification.id, notification);
|
||||
this.logger.info(`Notification ${notification.id} created successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error creating notification ${notification.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async update(notification: Notification): Promise<void> {
|
||||
this.logger.debug(`Updating notification: ${notification.id}`);
|
||||
try {
|
||||
if (!this.notifications.has(notification.id)) {
|
||||
this.logger.warn(`Notification with ID ${notification.id} not found for update. Throwing error.`);
|
||||
throw new Error(`Notification with ID ${notification.id} not found`);
|
||||
}
|
||||
this.notifications.set(notification.id, notification);
|
||||
this.logger.info(`Notification ${notification.id} updated successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error updating notification ${notification.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.logger.debug(`Deleting notification: ${id}`);
|
||||
try {
|
||||
if (this.notifications.delete(id)) {
|
||||
this.logger.info(`Notification ${id} deleted successfully.`);
|
||||
} else {
|
||||
this.logger.warn(`Notification with ID ${id} not found for deletion.`);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting notification ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAllByRecipientId(recipientId: string): Promise<void> {
|
||||
this.logger.debug(`Deleting all notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const initialCount = this.notifications.size;
|
||||
const toDelete = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId)
|
||||
.map(n => n.id);
|
||||
|
||||
toDelete.forEach(id => this.notifications.delete(id));
|
||||
this.logger.info(`Deleted ${toDelete.length} notifications for recipient ID: ${recipientId}.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting all notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async markAllAsReadByRecipientId(recipientId: string): Promise<void> {
|
||||
this.logger.debug(`Marking all notifications as read for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const toUpdate = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread());
|
||||
|
||||
this.logger.info(`Found ${toUpdate.length} unread notifications to mark as read for recipient ID: ${recipientId}.`);
|
||||
|
||||
toUpdate.forEach(n => {
|
||||
const updated = n.markAsRead();
|
||||
this.notifications.set(updated.id, updated);
|
||||
});
|
||||
this.logger.info(`Marked ${toUpdate.length} notifications as read for recipient ID: ${recipientId}.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error marking all notifications as read for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user