refactor
This commit is contained in:
@@ -5,38 +5,41 @@
|
||||
*/
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type {
|
||||
IAwardPrizePresenter,
|
||||
AwardPrizeResultDTO,
|
||||
AwardPrizeViewModel,
|
||||
} from '../presenters/IAwardPrizePresenter';
|
||||
import type { Prize } from '../../domain/entities/Prize';
|
||||
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 AwardPrizeInput {
|
||||
prizeId: string;
|
||||
driverId: string;
|
||||
}
|
||||
|
||||
export interface AwardPrizeResult {
|
||||
prize: Prize;
|
||||
}
|
||||
|
||||
export type AwardPrizeErrorCode = 'PRIZE_NOT_FOUND' | 'PRIZE_ALREADY_AWARDED';
|
||||
|
||||
export class AwardPrizeUseCase
|
||||
implements UseCase<AwardPrizeInput, AwardPrizeResultDTO, AwardPrizeViewModel, IAwardPrizePresenter>
|
||||
implements UseCase<AwardPrizeInput, void, AwardPrizeErrorCode>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: AwardPrizeInput,
|
||||
presenter: IAwardPrizePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
constructor(
|
||||
private readonly prizeRepository: IPrizeRepository,
|
||||
private readonly output: UseCaseOutputPort<AwardPrizeResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: AwardPrizeInput): Promise<Result<void, ApplicationErrorCode<AwardPrizeErrorCode>>> {
|
||||
const { prizeId, driverId } = input;
|
||||
|
||||
const prize = await this.prizeRepository.findById(prizeId);
|
||||
if (!prize) {
|
||||
throw new Error('Prize not found');
|
||||
return Result.err({ code: 'PRIZE_NOT_FOUND' as const });
|
||||
}
|
||||
|
||||
if (prize.awarded) {
|
||||
throw new Error('Prize has already been awarded');
|
||||
return Result.err({ code: 'PRIZE_ALREADY_AWARDED' as const });
|
||||
}
|
||||
|
||||
prize.awarded = true;
|
||||
@@ -45,23 +48,8 @@ export class AwardPrizeUseCase
|
||||
|
||||
const updatedPrize = await this.prizeRepository.update(prize);
|
||||
|
||||
const dto: AwardPrizeResultDTO = {
|
||||
prize: {
|
||||
id: updatedPrize.id,
|
||||
leagueId: updatedPrize.leagueId,
|
||||
seasonId: updatedPrize.seasonId,
|
||||
position: updatedPrize.position,
|
||||
name: updatedPrize.name,
|
||||
amount: updatedPrize.amount,
|
||||
type: updatedPrize.type,
|
||||
description: updatedPrize.description,
|
||||
awarded: updatedPrize.awarded,
|
||||
awardedTo: updatedPrize.awardedTo,
|
||||
awardedAt: updatedPrize.awardedAt,
|
||||
createdAt: updatedPrize.createdAt,
|
||||
},
|
||||
};
|
||||
this.output.present({ prize: updatedPrize });
|
||||
|
||||
presenter.present(dto);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user