This commit is contained in:
2025-12-16 21:05:01 +01:00
parent f61e3a4e5a
commit 7532c7ed6d
207 changed files with 7861 additions and 2606 deletions

View File

@@ -3,15 +3,12 @@ import type { ILeagueRepository } from '../../domain/repositories/ILeagueReposit
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type {
IRaceResultsDetailPresenter,
RaceResultsDetailViewModel,
RaceResultsPenaltySummaryViewModel,
} from '../presenters/IRaceResultsDetailPresenter';
import type { UseCase } from '@core/shared/application/UseCase';
import type { RaceResultsDetailViewModel, RaceResultsPenaltySummaryViewModel } from '../presenters/IRaceResultsDetailPresenter';
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { League } from '../../domain/entities/League';
import type { Result } from '../../domain/entities/Result';
import type { Driver } from '../../domain/entities/Driver';
import type { Result as DomainResult } from '../../domain/entities/Result';
import type { Penalty } from '../../domain/entities/Penalty';
export interface GetRaceResultsDetailParams {
@@ -19,76 +16,10 @@ export interface GetRaceResultsDetailParams {
driverId?: string;
}
function buildPointsSystem(league: League | null): Record<number, number> | undefined {
if (!league) return undefined;
const pointsSystems: Record<string, Record<number, number>> = {
'f1-2024': {
1: 25,
2: 18,
3: 15,
4: 12,
5: 10,
6: 8,
7: 6,
8: 4,
9: 2,
10: 1,
},
indycar: {
1: 50,
2: 40,
3: 35,
4: 32,
5: 30,
6: 28,
7: 26,
8: 24,
9: 22,
10: 20,
11: 19,
12: 18,
13: 17,
14: 16,
15: 15,
},
};
type GetRaceResultsDetailErrorCode = 'RACE_NOT_FOUND';
const customPoints = league.settings.customPoints;
if (customPoints) {
return customPoints;
}
const preset = pointsSystems[league.settings.pointsSystem];
if (preset) {
return preset;
}
return pointsSystems['f1-2024'];
}
function getFastestLapTime(results: Result[]): number | undefined {
if (results.length === 0) return undefined;
return Math.min(...results.map((r) => r.fastestLap));
}
function mapPenaltySummary(penalties: Penalty[]): RaceResultsPenaltySummaryViewModel[] {
return penalties.map((p) => ({
driverId: p.driverId,
type: p.type,
...(p.value !== undefined ? { value: p.value } : {}),
}));
}
export class GetRaceResultsDetailUseCase
implements
UseCase<
GetRaceResultsDetailParams,
RaceResultsDetailViewModel,
RaceResultsDetailViewModel,
IRaceResultsDetailPresenter
>
{
export class GetRaceResultsDetailUseCase implements AsyncUseCase<GetRaceResultsDetailParams, RaceResultsDetailViewModel, GetRaceResultsDetailErrorCode> {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly leagueRepository: ILeagueRepository,
@@ -97,27 +28,13 @@ export class GetRaceResultsDetailUseCase
private readonly penaltyRepository: IPenaltyRepository,
) {}
async execute(
params: GetRaceResultsDetailParams,
presenter: IRaceResultsDetailPresenter,
): Promise<void> {
presenter.reset();
async execute(params: GetRaceResultsDetailParams): Promise<Result<RaceResultsDetailViewModel, ApplicationErrorCode<GetRaceResultsDetailErrorCode>>> {
const { raceId, driverId } = params;
const race = await this.raceRepository.findById(raceId);
if (!race) {
const errorViewModel: RaceResultsDetailViewModel = {
race: null,
league: null,
results: [],
drivers: [],
penalties: [],
...(driverId ? { currentDriverId: driverId } : {}),
error: 'Race not found',
};
presenter.present(errorViewModel);
return;
return Result.err({ code: 'RACE_NOT_FOUND' });
}
const [league, results, drivers, penalties] = await Promise.all([
@@ -130,9 +47,9 @@ export class GetRaceResultsDetailUseCase
const effectiveCurrentDriverId =
driverId ?? (drivers.length > 0 ? drivers[0]!.id : undefined);
const pointsSystem = buildPointsSystem(league as League | null);
const fastestLapTime = getFastestLapTime(results);
const penaltySummary = mapPenaltySummary(penalties);
const pointsSystem = this.buildPointsSystem(league);
const fastestLapTime = this.getFastestLapTime(results);
const penaltySummary = this.mapPenaltySummary(penalties);
const viewModel: RaceResultsDetailViewModel = {
race: {
@@ -156,6 +73,67 @@ export class GetRaceResultsDetailUseCase
...(effectiveCurrentDriverId ? { currentDriverId: effectiveCurrentDriverId } : {}),
};
presenter.present(viewModel);
return Result.ok(viewModel);
}
private buildPointsSystem(league: League | null): Record<number, number> | undefined {
if (!league) return undefined;
const pointsSystems: Record<string, Record<number, number>> = {
'f1-2024': {
1: 25,
2: 18,
3: 15,
4: 12,
5: 10,
6: 8,
7: 6,
8: 4,
9: 2,
10: 1,
},
indycar: {
1: 50,
2: 40,
3: 35,
4: 32,
5: 30,
6: 28,
7: 26,
8: 24,
9: 22,
10: 20,
11: 19,
12: 18,
13: 17,
14: 16,
15: 15,
},
};
const customPoints = league.settings.customPoints;
if (customPoints) {
return customPoints;
}
const preset = pointsSystems[league.settings.pointsSystem];
if (preset) {
return preset;
}
return pointsSystems['f1-2024'];
}
private getFastestLapTime(results: DomainResult[]): number | undefined {
if (results.length === 0) return undefined;
return Math.min(...results.map((r) => r.fastestLap));
}
private mapPenaltySummary(penalties: Penalty[]): RaceResultsPenaltySummaryViewModel[] {
return penalties.map((p) => ({
driverId: p.driverId,
type: p.type,
...(p.value !== undefined ? { value: p.value } : {}),
}));
}
}