refactor
This commit is contained in:
@@ -6,12 +6,10 @@
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type { PrizeType, Prize } from '../../domain/entities/Prize';
|
||||
import type {
|
||||
ICreatePrizePresenter,
|
||||
CreatePrizeResultDTO,
|
||||
CreatePrizeViewModel,
|
||||
} from '../presenters/ICreatePrizePresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface CreatePrizeInput {
|
||||
leagueId: string;
|
||||
@@ -23,22 +21,26 @@ export interface CreatePrizeInput {
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface CreatePrizeResult {
|
||||
prize: Prize;
|
||||
}
|
||||
|
||||
export type CreatePrizeErrorCode = 'PRIZE_ALREADY_EXISTS';
|
||||
|
||||
export class CreatePrizeUseCase
|
||||
implements UseCase<CreatePrizeInput, CreatePrizeResultDTO, CreatePrizeViewModel, ICreatePrizePresenter>
|
||||
implements UseCase<CreatePrizeInput, void, CreatePrizeErrorCode>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: CreatePrizeInput,
|
||||
presenter: ICreatePrizePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
constructor(
|
||||
private readonly prizeRepository: IPrizeRepository,
|
||||
private readonly output: UseCaseOutputPort<CreatePrizeResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: CreatePrizeInput): Promise<Result<void, ApplicationErrorCode<CreatePrizeErrorCode>>> {
|
||||
const { leagueId, seasonId, position, name, amount, type, description } = input;
|
||||
|
||||
const existingPrize = await this.prizeRepository.findByPosition(leagueId, seasonId, position);
|
||||
if (existingPrize) {
|
||||
throw new Error(`Prize for position ${position} already exists`);
|
||||
return Result.err({ code: 'PRIZE_ALREADY_EXISTS' as const });
|
||||
}
|
||||
|
||||
const id = `prize-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
@@ -50,30 +52,15 @@ export class CreatePrizeUseCase
|
||||
name,
|
||||
amount,
|
||||
type,
|
||||
description,
|
||||
awarded: false,
|
||||
createdAt: new Date(),
|
||||
...(description !== undefined ? { description } : {}),
|
||||
};
|
||||
|
||||
const createdPrize = await this.prizeRepository.create(prize);
|
||||
|
||||
const dto: CreatePrizeResultDTO = {
|
||||
prize: {
|
||||
id: createdPrize.id,
|
||||
leagueId: createdPrize.leagueId,
|
||||
seasonId: createdPrize.seasonId,
|
||||
position: createdPrize.position,
|
||||
name: createdPrize.name,
|
||||
amount: createdPrize.amount,
|
||||
type: createdPrize.type,
|
||||
description: createdPrize.description,
|
||||
awarded: createdPrize.awarded,
|
||||
awardedTo: createdPrize.awardedTo,
|
||||
awardedAt: createdPrize.awardedAt,
|
||||
createdAt: createdPrize.createdAt,
|
||||
},
|
||||
};
|
||||
this.output.present({ prize: createdPrize });
|
||||
|
||||
presenter.present(dto);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user