refactor use cases
This commit is contained in:
@@ -4,11 +4,10 @@
|
||||
* Marks a notification as read.
|
||||
*/
|
||||
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Logger } 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';
|
||||
|
||||
export interface MarkNotificationReadCommand {
|
||||
notificationId: string;
|
||||
@@ -29,13 +28,12 @@ export type MarkNotificationReadErrorCode =
|
||||
export class MarkNotificationReadUseCase {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly output: UseCaseOutputPort<MarkNotificationReadResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
command: MarkNotificationReadCommand,
|
||||
): Promise<Result<void, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>> {
|
||||
): Promise<Result<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>> {
|
||||
this.logger.debug(
|
||||
`Attempting to mark notification ${command.notificationId} as read for recipient ${command.recipientId}`,
|
||||
);
|
||||
@@ -45,7 +43,7 @@ export class MarkNotificationReadUseCase {
|
||||
|
||||
if (!notification) {
|
||||
this.logger.warn(`Notification not found for ID: ${command.notificationId}`);
|
||||
return Result.err<void, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
return Result.err<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
code: 'NOTIFICATION_NOT_FOUND',
|
||||
details: { message: 'Notification not found' },
|
||||
});
|
||||
@@ -55,7 +53,7 @@ export class MarkNotificationReadUseCase {
|
||||
this.logger.warn(
|
||||
`Unauthorized attempt to mark notification ${command.notificationId}. Recipient ID mismatch.`,
|
||||
);
|
||||
return Result.err<void, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
return Result.err<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
code: 'RECIPIENT_MISMATCH',
|
||||
details: { message: "Cannot mark another user's notification as read" },
|
||||
});
|
||||
@@ -65,12 +63,11 @@ export class MarkNotificationReadUseCase {
|
||||
this.logger.info(
|
||||
`Notification ${command.notificationId} is already read. Skipping update.`,
|
||||
);
|
||||
this.output.present({
|
||||
return Result.ok<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
notificationId: command.notificationId,
|
||||
recipientId: command.recipientId,
|
||||
wasAlreadyRead: true,
|
||||
});
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
|
||||
const updatedNotification = notification.markAsRead();
|
||||
@@ -79,19 +76,17 @@ export class MarkNotificationReadUseCase {
|
||||
`Notification ${command.notificationId} successfully marked as read.`,
|
||||
);
|
||||
|
||||
this.output.present({
|
||||
return Result.ok<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
notificationId: command.notificationId,
|
||||
recipientId: command.recipientId,
|
||||
wasAlreadyRead: false,
|
||||
});
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (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<void, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
return Result.err<MarkNotificationReadResult, ApplicationErrorCode<MarkNotificationReadErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
@@ -117,19 +112,19 @@ export type MarkAllNotificationsReadErrorCode = 'REPOSITORY_ERROR';
|
||||
export class MarkAllNotificationsReadUseCase {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly output: UseCaseOutputPort<MarkAllNotificationsReadResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: MarkAllNotificationsReadInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>> {
|
||||
): Promise<Result<MarkAllNotificationsReadResult, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>> {
|
||||
try {
|
||||
await this.notificationRepository.markAllAsReadByRecipientId(input.recipientId);
|
||||
this.output.present({ recipientId: input.recipientId });
|
||||
return Result.ok(undefined);
|
||||
return Result.ok<MarkAllNotificationsReadResult, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>({
|
||||
recipientId: input.recipientId,
|
||||
});
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
return Result.err<void, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>({
|
||||
return Result.err<MarkAllNotificationsReadResult, ApplicationErrorCode<MarkAllNotificationsReadErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
@@ -162,42 +157,40 @@ export type DismissNotificationErrorCode =
|
||||
export class DismissNotificationUseCase {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly output: UseCaseOutputPort<DismissNotificationResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
command: DismissNotificationCommand,
|
||||
): Promise<Result<void, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>> {
|
||||
): Promise<Result<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const notification = await this.notificationRepository.findById(
|
||||
command.notificationId,
|
||||
);
|
||||
|
||||
if (!notification) {
|
||||
return Result.err<void, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
code: 'NOTIFICATION_NOT_FOUND',
|
||||
details: { message: 'Notification not found' },
|
||||
});
|
||||
}
|
||||
|
||||
if (notification.recipientId !== command.recipientId) {
|
||||
return Result.err<void, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
code: 'RECIPIENT_MISMATCH',
|
||||
details: { message: "Cannot dismiss another user's notification" },
|
||||
});
|
||||
}
|
||||
|
||||
if (notification.isDismissed()) {
|
||||
this.output.present({
|
||||
return Result.ok<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
notificationId: command.notificationId,
|
||||
recipientId: command.recipientId,
|
||||
wasAlreadyDismissed: true,
|
||||
});
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
|
||||
if (!notification.canDismiss()) {
|
||||
return Result.err<void, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
code: 'CANNOT_DISMISS_REQUIRING_RESPONSE',
|
||||
details: { message: 'Cannot dismiss notification that requires response' },
|
||||
});
|
||||
@@ -206,19 +199,17 @@ export class DismissNotificationUseCase {
|
||||
const updatedNotification = notification.dismiss();
|
||||
await this.notificationRepository.update(updatedNotification);
|
||||
|
||||
this.output.present({
|
||||
return Result.ok<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
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<void, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
return Result.err<DismissNotificationResult, ApplicationErrorCode<DismissNotificationErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user