refactor use cases

This commit is contained in:
2025-12-21 01:20:27 +01:00
parent c12656d671
commit 8ecd638396
39 changed files with 2523 additions and 686 deletions

View File

@@ -4,7 +4,9 @@
* Marks a notification as read.
*/
import type { AsyncUseCase , Logger } from '@core/shared/application';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
@@ -13,38 +15,86 @@ export interface MarkNotificationReadCommand {
recipientId: string; // For validation
}
export class MarkNotificationReadUseCase implements AsyncUseCase<MarkNotificationReadCommand, void> {
export interface MarkNotificationReadResult {
notificationId: string;
recipientId: string;
wasAlreadyRead: boolean;
}
export type MarkNotificationReadErrorCode =
| 'NOTIFICATION_NOT_FOUND'
| 'RECIPIENT_MISMATCH'
| 'REPOSITORY_ERROR';
export class MarkNotificationReadUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
private readonly output: UseCaseOutputPort<MarkNotificationReadResult>,
private readonly logger: Logger,
) {}
async execute(command: MarkNotificationReadCommand): Promise<void> {
this.logger.debug(`Attempting to mark notification ${command.notificationId} as read for recipient ${command.recipientId}`);
async execute(
command: MarkNotificationReadCommand,
): Promise<Result<void, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>> {
this.logger.debug(
`Attempting to mark notification ${command.notificationId} as read for recipient ${command.recipientId}`,
);
try {
const notification = await this.notificationRepository.findById(command.notificationId);
if (!notification) {
this.logger.warn(`Notification not found for ID: ${command.notificationId}`);
throw new NotificationDomainError('Notification not found');
return Result.err({
code: 'NOTIFICATION_NOT_FOUND',
details: { message: 'Notification not found' },
});
}
if (notification.recipientId !== command.recipientId) {
this.logger.warn(`Unauthorized attempt to mark notification ${command.notificationId}. Recipient ID mismatch.`);
throw new NotificationDomainError('Cannot mark another user\'s notification as read');
this.logger.warn(
`Unauthorized attempt to mark notification ${command.notificationId}. Recipient ID mismatch.`,
);
return Result.err({
code: 'RECIPIENT_MISMATCH',
details: { message: "Cannot mark another user's notification as read" },
});
}
if (!notification.isUnread()) {
this.logger.info(`Notification ${command.notificationId} is already read. Skipping update.`);
return; // Already read, nothing to do
this.logger.info(
`Notification ${command.notificationId} is already read. Skipping update.`,
);
this.output.present({
notificationId: command.notificationId,
recipientId: command.recipientId,
wasAlreadyRead: true,
});
return Result.ok(undefined);
}
const updatedNotification = notification.markAsRead();
await this.notificationRepository.update(updatedNotification);
this.logger.info(`Notification ${command.notificationId} successfully marked as read.`);
this.logger.info(
`Notification ${command.notificationId} successfully marked as read.`,
);
this.output.present({
notificationId: command.notificationId,
recipientId: command.recipientId,
wasAlreadyRead: false,
});
return Result.ok(undefined);
} catch (error) {
this.logger.error(`Failed to mark notification ${command.notificationId} as read: ${error instanceof Error ? error.message : String(error)}`);
throw error;
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error(
`Failed to mark notification ${command.notificationId} as read: ${err.message}`,
);
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
}
}
}
@@ -54,13 +104,36 @@ export class MarkNotificationReadUseCase implements AsyncUseCase<MarkNotificatio
*
* Marks all notifications as read for a recipient.
*/
export class MarkAllNotificationsReadUseCase implements AsyncUseCase<string, void> {
export interface MarkAllNotificationsReadInput {
recipientId: string;
}
export interface MarkAllNotificationsReadResult {
recipientId: string;
}
export type MarkAllNotificationsReadErrorCode = 'REPOSITORY_ERROR';
export class MarkAllNotificationsReadUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
private readonly output: UseCaseOutputPort<MarkAllNotificationsReadResult>,
) {}
async execute(recipientId: string): Promise<void> {
await this.notificationRepository.markAllAsReadByRecipientId(recipientId);
async execute(
input: MarkAllNotificationsReadInput,
): Promise<Result<void, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>> {
try {
await this.notificationRepository.markAllAsReadByRecipientId(input.recipientId);
this.output.present({ recipientId: input.recipientId });
return Result.ok(undefined);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
}
}
}
@@ -74,27 +147,78 @@ export interface DismissNotificationCommand {
recipientId: string;
}
export class DismissNotificationUseCase implements AsyncUseCase<DismissNotificationCommand, void> {
export interface DismissNotificationResult {
notificationId: string;
recipientId: string;
wasAlreadyDismissed: boolean;
}
export type DismissNotificationErrorCode =
| 'NOTIFICATION_NOT_FOUND'
| 'RECIPIENT_MISMATCH'
| 'CANNOT_DISMISS_REQUIRING_RESPONSE'
| 'REPOSITORY_ERROR';
export class DismissNotificationUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
private readonly output: UseCaseOutputPort<DismissNotificationResult>,
) {}
async execute(command: DismissNotificationCommand): Promise<void> {
const notification = await this.notificationRepository.findById(command.notificationId);
if (!notification) {
throw new NotificationDomainError('Notification not found');
}
async execute(
command: DismissNotificationCommand,
): Promise<Result<void, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>> {
try {
const notification = await this.notificationRepository.findById(
command.notificationId,
);
if (notification.recipientId !== command.recipientId) {
throw new NotificationDomainError('Cannot dismiss another user\'s notification');
}
if (!notification) {
return Result.err({
code: 'NOTIFICATION_NOT_FOUND',
details: { message: 'Notification not found' },
});
}
if (notification.isDismissed()) {
return; // Already dismissed
}
if (notification.recipientId !== command.recipientId) {
return Result.err({
code: 'RECIPIENT_MISMATCH',
details: { message: "Cannot dismiss another user's notification" },
});
}
const updatedNotification = notification.dismiss();
await this.notificationRepository.update(updatedNotification);
if (notification.isDismissed()) {
this.output.present({
notificationId: command.notificationId,
recipientId: command.recipientId,
wasAlreadyDismissed: true,
});
return Result.ok(undefined);
}
if (!notification.canDismiss()) {
return Result.err({
code: 'CANNOT_DISMISS_REQUIRING_RESPONSE',
details: { message: 'Cannot dismiss notification that requires response' },
});
}
const updatedNotification = notification.dismiss();
await this.notificationRepository.update(updatedNotification);
this.output.present({
notificationId: command.notificationId,
recipientId: command.recipientId,
wasAlreadyDismissed: false,
});
return Result.ok(undefined);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
}
}
}