Files
gridpilot.gg/packages/notifications/application/use-cases/MarkNotificationReadUseCase.ts
2025-12-11 11:25:22 +01:00

88 lines
2.5 KiB
TypeScript

/**
* Application Use Case: MarkNotificationReadUseCase
*
* Marks a notification as read.
*/
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
export interface MarkNotificationReadCommand {
notificationId: string;
recipientId: string; // For validation
}
export class MarkNotificationReadUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(command: MarkNotificationReadCommand): Promise<void> {
const notification = await this.notificationRepository.findById(command.notificationId);
if (!notification) {
throw new NotificationDomainError('Notification not found');
}
if (notification.recipientId !== command.recipientId) {
throw new NotificationDomainError('Cannot mark another user\'s notification as read');
}
if (!notification.isUnread()) {
return; // Already read, nothing to do
}
const updatedNotification = notification.markAsRead();
await this.notificationRepository.update(updatedNotification);
}
}
/**
* Application Use Case: MarkAllNotificationsReadUseCase
*
* Marks all notifications as read for a recipient.
*/
export class MarkAllNotificationsReadUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string): Promise<void> {
await this.notificationRepository.markAllAsReadByRecipientId(recipientId);
}
}
/**
* Application Use Case: DismissNotificationUseCase
*
* Dismisses a notification.
*/
export interface DismissNotificationCommand {
notificationId: string;
recipientId: string;
}
export class DismissNotificationUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(command: DismissNotificationCommand): Promise<void> {
const notification = await this.notificationRepository.findById(command.notificationId);
if (!notification) {
throw new NotificationDomainError('Notification not found');
}
if (notification.recipientId !== command.recipientId) {
throw new NotificationDomainError('Cannot dismiss another user\'s notification');
}
if (notification.isDismissed()) {
return; // Already dismissed
}
const updatedNotification = notification.dismiss();
await this.notificationRepository.update(updatedNotification);
}
}