refactor use cases
This commit is contained in:
@@ -6,7 +6,6 @@ import { Result as RaceResult } from '../../domain/entities/result/Result';
|
||||
import { Standing } from '../../domain/entities/Standing';
|
||||
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 CompleteRaceInput {
|
||||
raceId: string;
|
||||
@@ -46,18 +45,17 @@ export class CompleteRaceUseCase {
|
||||
private readonly resultRepository: IResultRepository,
|
||||
private readonly standingRepository: IStandingRepository,
|
||||
private readonly getDriverRating: (input: DriverRatingInput) => Promise<DriverRatingOutput>,
|
||||
private readonly output: UseCaseOutputPort<CompleteRaceResult>,
|
||||
) {}
|
||||
|
||||
async execute(command: CompleteRaceInput): Promise<
|
||||
Result<void, ApplicationErrorCode<CompleteRaceErrorCode | 'REPOSITORY_ERROR', { message: string }>>
|
||||
Result<CompleteRaceResult, ApplicationErrorCode<CompleteRaceErrorCode | 'REPOSITORY_ERROR', { message: string }>>
|
||||
> {
|
||||
try {
|
||||
const { raceId } = command;
|
||||
|
||||
const race = await this.raceRepository.findById(raceId);
|
||||
if (!race) {
|
||||
return Result.err<void, ApplicationErrorCode<CompleteRaceErrorCode, { message: string }>>({
|
||||
return Result.err<CompleteRaceResult, ApplicationErrorCode<CompleteRaceErrorCode, { message: string }>>({
|
||||
code: 'RACE_NOT_FOUND',
|
||||
details: { message: 'Race not found' },
|
||||
});
|
||||
@@ -66,7 +64,7 @@ export class CompleteRaceUseCase {
|
||||
// Get registered drivers for this race
|
||||
const registeredDriverIds = await this.raceRegistrationRepository.getRegisteredDrivers(raceId);
|
||||
if (registeredDriverIds.length === 0) {
|
||||
return Result.err<void, ApplicationErrorCode<CompleteRaceErrorCode, { message: string }>>({
|
||||
return Result.err<CompleteRaceResult, ApplicationErrorCode<CompleteRaceErrorCode, { message: string }>>({
|
||||
code: 'NO_REGISTERED_DRIVERS',
|
||||
details: { message: 'No registered drivers for this race' },
|
||||
});
|
||||
@@ -102,9 +100,9 @@ export class CompleteRaceUseCase {
|
||||
const completedRace = race.complete();
|
||||
await this.raceRepository.update(completedRace);
|
||||
|
||||
this.output.present({ raceId, registeredDriverIds });
|
||||
const result: CompleteRaceResult = { raceId, registeredDriverIds };
|
||||
|
||||
return Result.ok(undefined);
|
||||
return Result.ok(result);
|
||||
} catch (error) {
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
|
||||
Reference in New Issue
Block a user