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,67 +1,102 @@
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { RaceResultsDetailOutputPort } from '../ports/output/RaceResultsDetailOutputPort';
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
import { Result } from '@core/shared/application/Result';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { Driver } from '../../domain/entities/Driver';
import type { League } from '../../domain/entities/League';
import type { Result as DomainResult } from '../../domain/entities/Result';
import type { Penalty } from '../../domain/entities/Penalty';
import type { Race } from '../../domain/entities/Race';
import type { Penalty } from '../../domain/entities/penalty/Penalty';
import type { Result as DomainResult } from '../../domain/entities/result/Result';
export interface GetRaceResultsDetailParams {
export type GetRaceResultsDetailInput = {
raceId: string;
driverId?: string;
}
};
export type GetRaceResultsDetailErrorCode =
| 'RACE_NOT_FOUND'
| 'REPOSITORY_ERROR';
type GetRaceResultsDetailErrorCode = 'RACE_NOT_FOUND';
export type GetRaceResultsDetailResult = {
race: Race;
league: League | null;
results: DomainResult[];
drivers: Driver[];
penalties: Penalty[];
pointsSystem?: Record<number, number>;
fastestLapTime?: number;
currentDriverId?: string;
};
export class GetRaceResultsDetailUseCase implements AsyncUseCase<GetRaceResultsDetailParams, RaceResultsDetailOutputPort, GetRaceResultsDetailErrorCode> {
export class GetRaceResultsDetailUseCase {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly leagueRepository: ILeagueRepository,
private readonly resultRepository: IResultRepository,
private readonly driverRepository: IDriverRepository,
private readonly penaltyRepository: IPenaltyRepository,
private readonly output: UseCaseOutputPort<GetRaceResultsDetailResult>,
) {}
async execute(params: GetRaceResultsDetailParams): Promise<Result<RaceResultsDetailOutputPort, ApplicationErrorCode<GetRaceResultsDetailErrorCode>>> {
const { raceId, driverId } = params;
async execute(
params: GetRaceResultsDetailInput,
): Promise<
Result<void, ApplicationErrorCode<GetRaceResultsDetailErrorCode, { message: string }>>
> {
try {
const { raceId, driverId } = params;
const race = await this.raceRepository.findById(raceId);
const race = await this.raceRepository.findById(raceId);
if (!race) {
return Result.err({ code: 'RACE_NOT_FOUND' });
if (!race) {
return Result.err({
code: 'RACE_NOT_FOUND',
details: { message: 'Race not found' },
});
}
const [league, results, drivers, penalties] = await Promise.all([
this.leagueRepository.findById(race.leagueId),
this.resultRepository.findByRaceId(raceId),
this.driverRepository.findAll(),
this.penaltyRepository.findByRaceId(raceId),
]);
const effectiveCurrentDriverId =
driverId ?? (drivers.length > 0 ? drivers[0]!.id : undefined);
const pointsSystem = this.buildPointsSystem(league);
const fastestLapTime = this.getFastestLapTime(results);
const result: GetRaceResultsDetailResult = {
race,
league,
results,
drivers,
penalties,
...(pointsSystem ? { pointsSystem } : {}),
...(fastestLapTime !== undefined ? { fastestLapTime } : {}),
...(effectiveCurrentDriverId ? { currentDriverId: effectiveCurrentDriverId } : {}),
};
this.output.present(result);
return Result.ok(undefined);
} catch (error: unknown) {
const message =
error instanceof Error && typeof error.message === 'string'
? error.message
: 'Failed to load race results detail';
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
});
}
const [league, results, drivers, penalties] = await Promise.all([
this.leagueRepository.findById(race.leagueId),
this.resultRepository.findByRaceId(raceId),
this.driverRepository.findAll(),
this.penaltyRepository.findByRaceId(raceId),
]);
const effectiveCurrentDriverId =
driverId ?? (drivers.length > 0 ? drivers[0]!.id : undefined);
const pointsSystem = this.buildPointsSystem(league);
const fastestLapTime = this.getFastestLapTime(results);
const outputPort: RaceResultsDetailOutputPort = {
race,
league,
results,
drivers,
penalties,
...(pointsSystem ? { pointsSystem } : {}),
...(fastestLapTime !== undefined ? { fastestLapTime } : {}),
...(effectiveCurrentDriverId ? { currentDriverId: effectiveCurrentDriverId } : {}),
};
return Result.ok(outputPort);
}
private buildPointsSystem(league: League | null): Record<number, number> | undefined {
@@ -114,7 +149,6 @@ export class GetRaceResultsDetailUseCase implements AsyncUseCase<GetRaceResultsD
private getFastestLapTime(results: DomainResult[]): number | undefined {
if (results.length === 0) return undefined;
return Math.min(...results.map((r) => r.fastestLap));
return Math.min(...results.map(r => r.fastestLap.toNumber()));
}
}
}