/** * Application Use Case: GetUnreadNotificationsUseCase * * Retrieves unread notifications for a recipient. */ 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 type GetUnreadNotificationsInput = { recipientId: string; }; export interface GetUnreadNotificationsResult { notifications: Notification[]; totalCount: number; } export type GetUnreadNotificationsErrorCode = 'REPOSITORY_ERROR'; export class GetUnreadNotificationsUseCase { constructor( private readonly notificationRepository: INotificationRepository, private readonly output: UseCaseOutputPort, private readonly logger: Logger, ) {} async execute( input: GetUnreadNotificationsInput, ): Promise>> { 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}`, ); if (notifications.length === 0) { this.logger.warn(`No unread notifications found for recipient ID: ${recipientId}`); } this.output.present({ notifications, totalCount: notifications.length, }); return Result.ok>( undefined, ); } catch (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>({ code: 'REPOSITORY_ERROR', details: { message: err.message }, }); } } } /** * Additional notification query/use case types (e.g., listing or counting notifications) * can be added here in the future as needed. */