Files
gridpilot.gg/adapters/notifications/persistence/inmemory/InMemoryNotificationRepository.ts
2026-01-16 21:57:44 +01:00

174 lines
7.9 KiB
TypeScript

/**
* In-Memory Implementation: InMemoryNotificationRepository
*
* Provides an in-memory storage implementation for notifications.
*/
import { Notification } from '@core/notifications/domain/entities/Notification';
import type { NotificationRepository } from '@core/notifications/domain/repositories/NotificationRepository';
import type { NotificationType } from '@core/notifications/domain/types/NotificationTypes';
import type { Logger } from '@core/shared/domain/Logger';
export class InMemoryNotificationRepository implements NotificationRepository {
private notifications: Map<string, Notification> = new Map();
private readonly logger: Logger;
constructor(logger: Logger, initialNotifications: Notification[] = []) {
this.logger = logger;
this.logger.info('InMemoryNotificationRepository initialized.');
initialNotifications.forEach(notification => {
this.notifications.set(notification.id.value, notification);
this.logger.debug(`Seeded notification: ${notification.id.value}`);
});
}
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 instanceof Error ? error : new Error(String(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 instanceof Error ? error : new Error(String(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 instanceof Error ? error : new Error(String(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 instanceof Error ? error : new Error(String(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 instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async create(notification: Notification): Promise<void> {
this.logger.debug(`Creating notification: ${notification.id.value}`);
try {
if (this.notifications.has(notification.id.value)) {
this.logger.warn(`Notification with ID ${notification.id.value} already exists. Throwing error.`);
throw new Error(`Notification with ID ${notification.id.value} already exists`);
}
this.notifications.set(notification.id.value, notification);
this.logger.info(`Notification ${notification.id.value} created successfully.`);
} catch (error) {
this.logger.error(`Error creating notification ${notification.id.value}:`, error instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async update(notification: Notification): Promise<void> {
this.logger.debug(`Updating notification: ${notification.id.value}`);
try {
if (!this.notifications.has(notification.id.value)) {
this.logger.warn(`Notification with ID ${notification.id.value} not found for update. Throwing error.`);
throw new Error(`Notification with ID ${notification.id.value} not found`);
}
this.notifications.set(notification.id.value, notification);
this.logger.info(`Notification ${notification.id.value} updated successfully.`);
} catch (error) {
this.logger.error(`Error updating notification ${notification.id.value}:`, error instanceof Error ? error : new Error(String(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 instanceof Error ? error : new Error(String(error)));
throw error;
}
}
async deleteAllByRecipientId(recipientId: string): Promise<void> {
this.logger.debug(`Deleting all notifications for recipient ID: ${recipientId}`);
try {
const toDelete = Array.from(this.notifications.values())
.filter(n => n.recipientId === recipientId)
.map(n => n.id.value);
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 instanceof Error ? error : new Error(String(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.value, 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 instanceof Error ? error : new Error(String(error)));
throw error;
}
}
}