refactor use cases
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* Retrieves unread notifications for a recipient.
|
||||
*/
|
||||
|
||||
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 { Notification } from '../../domain/entities/Notification';
|
||||
@@ -24,13 +24,12 @@ export type GetUnreadNotificationsErrorCode = 'REPOSITORY_ERROR';
|
||||
export class GetUnreadNotificationsUseCase {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly output: UseCaseOutputPort<GetUnreadNotificationsResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetUnreadNotificationsInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>> {
|
||||
): Promise<Result<GetUnreadNotificationsResult, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>> {
|
||||
const { recipientId } = input;
|
||||
this.logger.debug(
|
||||
`Attempting to retrieve unread notifications for recipient ID: ${recipientId}`,
|
||||
@@ -48,14 +47,10 @@ export class GetUnreadNotificationsUseCase {
|
||||
this.logger.warn(`No unread notifications found for recipient ID: ${recipientId}`);
|
||||
}
|
||||
|
||||
this.output.present({
|
||||
return Result.ok<GetUnreadNotificationsResult, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>({
|
||||
notifications,
|
||||
totalCount: notifications.length,
|
||||
});
|
||||
|
||||
return Result.ok<void, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>(
|
||||
undefined,
|
||||
);
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
this.logger.error(
|
||||
@@ -63,7 +58,7 @@ export class GetUnreadNotificationsUseCase {
|
||||
err,
|
||||
);
|
||||
|
||||
return Result.err<void, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>({
|
||||
return Result.err<GetUnreadNotificationsResult, ApplicationErrorCode<GetUnreadNotificationsErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
@@ -74,4 +69,4 @@ export class GetUnreadNotificationsUseCase {
|
||||
/**
|
||||
* Additional notification query/use case types (e.g., listing or counting notifications)
|
||||
* can be added here in the future as needed.
|
||||
*/
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user