refactor
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { Logger, UseCaseOutputPort, UseCase } from '@core/shared/application';
|
||||
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
|
||||
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
|
||||
import type { EngagementAction, EngagementEntityType } from '../../domain/types/EngagementEvent';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface RecordEngagementInput {
|
||||
action: EngagementAction;
|
||||
@@ -18,13 +20,15 @@ export interface RecordEngagementOutput {
|
||||
engagementWeight: number;
|
||||
}
|
||||
|
||||
export class RecordEngagementUseCase {
|
||||
export type RecordEngagementErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class RecordEngagementUseCase implements UseCase<RecordEngagementInput, RecordEngagementOutput, RecordEngagementErrorCode> {
|
||||
constructor(
|
||||
private readonly engagementRepository: IEngagementRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(input: RecordEngagementInput): Promise<RecordEngagementOutput> {
|
||||
async execute(input: RecordEngagementInput): Promise<Result<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const engagementEvent = EngagementEvent.create({
|
||||
id: crypto.randomUUID(),
|
||||
@@ -46,13 +50,19 @@ export class RecordEngagementUseCase {
|
||||
entityType: input.entityType,
|
||||
});
|
||||
|
||||
return {
|
||||
const result = Result.ok<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>({
|
||||
eventId: engagementEvent.id,
|
||||
engagementWeight: engagementEvent.getEngagementWeight(),
|
||||
};
|
||||
});
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to record engagement event', { error: error as Error, input });
|
||||
throw error;
|
||||
const err = error as Error;
|
||||
this.logger.error('Failed to record engagement event', err, { input });
|
||||
const result = Result.err<RecordEngagementOutput, ApplicationErrorCode<RecordEngagementErrorCode, { message: string }>>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message ?? 'Failed to record engagement event' },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user