83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
/**
|
|
* 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';
|
|
|
|
export class InMemoryNotificationRepository implements INotificationRepository {
|
|
private notifications: Map<string, Notification> = new Map();
|
|
|
|
constructor(initialNotifications: Notification[] = []) {
|
|
initialNotifications.forEach(notification => {
|
|
this.notifications.set(notification.id, notification);
|
|
});
|
|
}
|
|
|
|
async findById(id: string): Promise<Notification | null> {
|
|
return this.notifications.get(id) || null;
|
|
}
|
|
|
|
async findByRecipientId(recipientId: string): Promise<Notification[]> {
|
|
return Array.from(this.notifications.values())
|
|
.filter(n => n.recipientId === recipientId)
|
|
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
}
|
|
|
|
async findUnreadByRecipientId(recipientId: string): Promise<Notification[]> {
|
|
return Array.from(this.notifications.values())
|
|
.filter(n => n.recipientId === recipientId && n.isUnread())
|
|
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
}
|
|
|
|
async findByRecipientIdAndType(recipientId: string, type: NotificationType): Promise<Notification[]> {
|
|
return Array.from(this.notifications.values())
|
|
.filter(n => n.recipientId === recipientId && n.type === type)
|
|
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
}
|
|
|
|
async countUnreadByRecipientId(recipientId: string): Promise<number> {
|
|
return Array.from(this.notifications.values())
|
|
.filter(n => n.recipientId === recipientId && n.isUnread())
|
|
.length;
|
|
}
|
|
|
|
async create(notification: Notification): Promise<void> {
|
|
if (this.notifications.has(notification.id)) {
|
|
throw new Error(`Notification with ID ${notification.id} already exists`);
|
|
}
|
|
this.notifications.set(notification.id, notification);
|
|
}
|
|
|
|
async update(notification: Notification): Promise<void> {
|
|
if (!this.notifications.has(notification.id)) {
|
|
throw new Error(`Notification with ID ${notification.id} not found`);
|
|
}
|
|
this.notifications.set(notification.id, notification);
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
this.notifications.delete(id);
|
|
}
|
|
|
|
async deleteAllByRecipientId(recipientId: string): Promise<void> {
|
|
const toDelete = Array.from(this.notifications.values())
|
|
.filter(n => n.recipientId === recipientId)
|
|
.map(n => n.id);
|
|
|
|
toDelete.forEach(id => this.notifications.delete(id));
|
|
}
|
|
|
|
async markAllAsReadByRecipientId(recipientId: string): Promise<void> {
|
|
const toUpdate = Array.from(this.notifications.values())
|
|
.filter(n => n.recipientId === recipientId && n.isUnread());
|
|
|
|
toUpdate.forEach(n => {
|
|
const updated = n.markAsRead();
|
|
this.notifications.set(updated.id, updated);
|
|
});
|
|
}
|
|
} |