refactor use cases
This commit is contained in:
@@ -9,7 +9,6 @@ import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepos
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
export interface CreateSponsorInput {
|
||||
name: string;
|
||||
@@ -18,7 +17,7 @@ export interface CreateSponsorInput {
|
||||
logoUrl?: string;
|
||||
}
|
||||
|
||||
type CreateSponsorResult = {
|
||||
export type CreateSponsorResult = {
|
||||
sponsor: Sponsor;
|
||||
};
|
||||
|
||||
@@ -26,12 +25,11 @@ export class CreateSponsorUseCase {
|
||||
constructor(
|
||||
private readonly sponsorRepository: ISponsorRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<CreateSponsorResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: CreateSponsorInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<'VALIDATION_ERROR' | 'REPOSITORY_ERROR', { message: string }>>> {
|
||||
): Promise<Result<CreateSponsorResult, ApplicationErrorCode<'VALIDATION_ERROR' | 'REPOSITORY_ERROR', { message: string }>>> {
|
||||
this.logger.debug('Executing CreateSponsorUseCase', { input });
|
||||
const validation = this.validate(input);
|
||||
if (validation.isErr()) {
|
||||
@@ -53,9 +51,9 @@ export class CreateSponsorUseCase {
|
||||
await this.sponsorRepository.create(sponsor);
|
||||
this.logger.info(`Sponsor ${sponsor.name} (${sponsor.id}) created successfully.`);
|
||||
|
||||
this.output.present({ sponsor });
|
||||
const result: CreateSponsorResult = { sponsor };
|
||||
this.logger.debug('CreateSponsorUseCase completed successfully.');
|
||||
return Result.ok(undefined);
|
||||
return Result.ok(result);
|
||||
} catch (error) {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
|
||||
}
|
||||
@@ -88,4 +86,4 @@ export class CreateSponsorUseCase {
|
||||
this.logger.debug('Validation successful.');
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user