refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,8 +1,24 @@
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { AsyncUseCase , Logger } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { ReopenRaceCommandDTO } from '../dto/ReopenRaceCommandDTO';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { Race } from '../../domain/entities/Race';
export type ReopenRaceInput = {
raceId: string;
reopenedById: string;
};
export type ReopenRaceErrorCode =
| 'RACE_NOT_FOUND'
| 'UNAUTHORIZED'
| 'INVALID_RACE_STATE'
| 'REPOSITORY_ERROR';
export type ReopenRaceResult = {
race: Race;
};
/**
* Use Case: ReopenRaceUseCase
@@ -13,42 +29,67 @@ import type { ReopenRaceCommandDTO } from '../dto/ReopenRaceCommandDTO';
* - delegates transition rules to the Race domain entity via `reopen()`
* - persists the updated race via the repository.
*/
export class ReopenRaceUseCase
implements AsyncUseCase<ReopenRaceCommandDTO, void, string> {
export class ReopenRaceUseCase {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<ReopenRaceResult>,
) {}
async execute(command: ReopenRaceCommandDTO): Promise<Result<void, ApplicationErrorCode<string>>> {
const { raceId } = command;
async execute(
input: ReopenRaceInput,
): Promise<Result<void, ApplicationErrorCode<ReopenRaceErrorCode, { message: string }>>> {
const { raceId } = input;
this.logger.debug(`[ReopenRaceUseCase] Executing for raceId: ${raceId}`);
try {
const race = await this.raceRepository.findById(raceId);
if (!race) {
this.logger.warn(`[ReopenRaceUseCase] Race with ID ${raceId} not found.`);
return Result.err({ code: 'RACE_NOT_FOUND' });
return Result.err({
code: 'RACE_NOT_FOUND',
details: { message: `Race with ID ${raceId} not found` },
});
}
const reopenedRace = race.reopen();
await this.raceRepository.update(reopenedRace);
this.logger.info(`[ReopenRaceUseCase] Race ${raceId} re-opened successfully.`);
const result: ReopenRaceResult = {
race: reopenedRace,
};
this.output.present(result);
return Result.ok(undefined);
} catch (error) {
if (error instanceof Error && error.message.includes('already scheduled')) {
this.logger.warn(`[ReopenRaceUseCase] Domain error re-opening race ${raceId}: ${error.message}`);
return Result.err({ code: 'RACE_ALREADY_SCHEDULED' });
return Result.err({
code: 'INVALID_RACE_STATE',
details: { message: error.message },
});
}
if (error instanceof Error && error.message.includes('running race')) {
this.logger.warn(`[ReopenRaceUseCase] Domain error re-opening race ${raceId}: ${error.message}`);
return Result.err({ code: 'CANNOT_REOPEN_RUNNING_RACE' });
return Result.err({
code: 'INVALID_RACE_STATE',
details: { message: error.message },
});
}
this.logger.error(
`[ReopenRaceUseCase] Unexpected error re-opening race ${raceId}`,
error instanceof Error ? error : new Error(String(error)),
);
return Result.err({ code: 'UNEXPECTED_ERROR' });
const message = error instanceof Error ? error.message : 'Failed to reopen race';
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
});
}
}
}