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,55 +1,80 @@
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IRankingService } from '../../domain/services/IRankingService';
import type { IDriverStatsService } from '../../domain/services/IDriverStatsService';
import type { GetDriverAvatarInputPort } from '../ports/input/GetDriverAvatarInputPort';
import type { GetDriverAvatarOutputPort } from '../ports/output/GetDriverAvatarOutputPort';
import type { DriversLeaderboardOutputPort, DriverLeaderboardItemOutputPort } from '../ports/output/DriversLeaderboardOutputPort';
import { SkillLevelService, type SkillLevel } from '../../domain/services/SkillLevelService';
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 { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { Driver } from '../../domain/entities/Driver';
import type { Team } from '../../domain/entities/Team';
export type GetDriversLeaderboardInput = {
leagueId: string;
seasonId?: string;
};
export interface DriverLeaderboardItem {
driver: Driver;
team?: Team;
rating: number;
skillLevel: SkillLevel;
racesCompleted: number;
wins: number;
podiums: number;
isActive: boolean;
rank: number;
avatarUrl?: string;
}
export interface GetDriversLeaderboardResult {
items: DriverLeaderboardItem[];
totalRaces: number;
totalWins: number;
activeCount: number;
}
export type GetDriversLeaderboardErrorCode = 'LEAGUE_NOT_FOUND' | 'SEASON_NOT_FOUND' | 'REPOSITORY_ERROR';
/**
* Use Case for retrieving driver leaderboard data.
* Orchestrates domain logic and returns result.
*/
export class GetDriversLeaderboardUseCase
implements AsyncUseCase<void, DriversLeaderboardOutputPort, 'REPOSITORY_ERROR'>
{
export class GetDriversLeaderboardUseCase {
constructor(
private readonly driverRepository: IDriverRepository,
private readonly rankingService: IRankingService,
private readonly driverStatsService: IDriverStatsService,
private readonly getDriverAvatar: (input: GetDriverAvatarInputPort) => Promise<GetDriverAvatarOutputPort>,
private readonly getDriverAvatar: (driverId: string) => Promise<string | undefined>,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<GetDriversLeaderboardResult>,
) {}
async execute(): Promise<Result<DriversLeaderboardOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
async execute(
_input: GetDriversLeaderboardInput,
): Promise<Result<void, ApplicationErrorCode<GetDriversLeaderboardErrorCode, { message: string }>>> {
this.logger.debug('Executing GetDriversLeaderboardUseCase');
try {
const drivers = await this.driverRepository.findAll();
const rankings = this.rankingService.getAllDriverRankings();
const avatarUrls: Record<string, string> = {};
const avatarUrls: Record<string, string | undefined> = {};
for (const driver of drivers) {
const avatarResult = await this.getDriverAvatar({ driverId: driver.id });
avatarUrls[driver.id] = avatarResult.avatarUrl;
avatarUrls[driver.id] = await this.getDriverAvatar(driver.id);
}
const driverItems: DriverLeaderboardItemOutputPort[] = drivers.map(driver => {
const ranking = rankings.find(r => r.driverId === driver.id);
const items: DriverLeaderboardItem[] = drivers.map((driver) => {
const ranking = rankings.find((r) => r.driverId === driver.id);
const stats = this.driverStatsService.getDriverStats(driver.id);
const rating = ranking?.rating ?? 0;
const racesCompleted = stats?.totalRaces ?? 0;
const skillLevel: SkillLevel = SkillLevelService.getSkillLevel(rating);
return {
id: driver.id,
name: driver.name.value,
driver,
rating,
skillLevel,
nationality: driver.country.value,
racesCompleted,
wins: stats?.wins ?? 0,
podiums: stats?.podiums ?? 0,
@@ -59,26 +84,29 @@ export class GetDriversLeaderboardUseCase
};
});
// Calculate totals
const totalRaces = driverItems.reduce((sum, d) => sum + d.racesCompleted, 0);
const totalWins = driverItems.reduce((sum, d) => sum + d.wins, 0);
const activeCount = driverItems.filter(d => d.isActive).length;
const totalRaces = items.reduce((sum, d) => sum + d.racesCompleted, 0);
const totalWins = items.reduce((sum, d) => sum + d.wins, 0);
const activeCount = items.filter((d) => d.isActive).length;
const result: DriversLeaderboardOutputPort = {
drivers: driverItems.sort((a, b) => b.rating - a.rating),
this.logger.debug('Successfully retrieved drivers leaderboard.');
this.output.present({
items: items.sort((a, b) => b.rating - a.rating),
totalRaces,
totalWins,
activeCount,
};
});
this.logger.debug('Successfully retrieved drivers leaderboard.');
return Result.ok(result);
return Result.ok(undefined);
} catch (error) {
this.logger.error('Error executing GetDriversLeaderboardUseCase', error instanceof Error ? error : new Error(String(error)));
this.logger.error(
'Error executing GetDriversLeaderboardUseCase',
error instanceof Error ? error : new Error(String(error)),
);
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message: error instanceof Error ? error.message : 'Unknown error occurred' },
});
}
}
}
}