refactor use cases
This commit is contained in:
@@ -1,41 +1,72 @@
|
||||
/**
|
||||
* Application Use Case: GetUnreadNotificationsUseCase
|
||||
*
|
||||
*
|
||||
* Retrieves unread notifications for a recipient.
|
||||
*/
|
||||
|
||||
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 { Notification } from '../../domain/entities/Notification';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
|
||||
export interface UnreadNotificationsResult {
|
||||
export type GetUnreadNotificationsInput = {
|
||||
recipientId: string;
|
||||
};
|
||||
|
||||
export interface GetUnreadNotificationsResult {
|
||||
notifications: Notification[];
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
export class GetUnreadNotificationsUseCase implements AsyncUseCase<string, UnreadNotificationsResult> {
|
||||
export type GetUnreadNotificationsErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetUnreadNotificationsUseCase {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly output: UseCaseOutputPort<GetUnreadNotificationsResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(recipientId: string): Promise<UnreadNotificationsResult> {
|
||||
this.logger.debug(`Attempting to retrieve unread notifications for recipient ID: ${recipientId}`);
|
||||
async execute(
|
||||
input: GetUnreadNotificationsInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>> {
|
||||
const { recipientId } = input;
|
||||
this.logger.debug(
|
||||
`Attempting to retrieve unread notifications for recipient ID: ${recipientId}`,
|
||||
);
|
||||
|
||||
try {
|
||||
const notifications = await this.notificationRepository.findUnreadByRecipientId(recipientId);
|
||||
this.logger.info(`Successfully retrieved ${notifications.length} unread notifications for recipient ID: ${recipientId}`);
|
||||
|
||||
const notifications = await this.notificationRepository.findUnreadByRecipientId(
|
||||
recipientId,
|
||||
);
|
||||
this.logger.info(
|
||||
`Successfully retrieved ${notifications.length} unread notifications for recipient ID: ${recipientId}`,
|
||||
);
|
||||
|
||||
if (notifications.length === 0) {
|
||||
this.logger.warn(`No unread notifications found for recipient ID: ${recipientId}`);
|
||||
}
|
||||
|
||||
return {
|
||||
this.output.present({
|
||||
notifications,
|
||||
totalCount: notifications.length,
|
||||
};
|
||||
});
|
||||
|
||||
return Result.ok<void, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>(
|
||||
undefined,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to retrieve unread notifications for recipient ID: ${recipientId}`, error instanceof Error ? error : new Error(String(error)));
|
||||
throw error;
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
this.logger.error(
|
||||
`Failed to retrieve unread notifications for recipient ID: ${recipientId}`,
|
||||
err,
|
||||
);
|
||||
|
||||
return Result.err<void, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user