refactor core presenters

This commit is contained in:
2025-12-19 19:42:19 +01:00
parent 8116fe888f
commit 94fc538f44
228 changed files with 2817 additions and 3097 deletions

View File

@@ -3,7 +3,8 @@ 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 { DriversLeaderboardResultDTO } from '../presenters/IDriversLeaderboardPresenter';
import type { DriversLeaderboardOutputPort, DriverLeaderboardItemOutputPort } from '../ports/output/DriversLeaderboardOutputPort';
import type { SkillLevel } from '../../domain/services/SkillLevelService';
import type { AsyncUseCase, Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -13,7 +14,7 @@ import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorC
* Orchestrates domain logic and returns result.
*/
export class GetDriversLeaderboardUseCase
implements AsyncUseCase<void, DriversLeaderboardResultDTO, 'REPOSITORY_ERROR'>
implements AsyncUseCase<void, DriversLeaderboardOutputPort, 'REPOSITORY_ERROR'>
{
constructor(
private readonly driverRepository: IDriverRepository,
@@ -23,34 +24,52 @@ export class GetDriversLeaderboardUseCase
private readonly logger: Logger,
) {}
async execute(): Promise<Result<DriversLeaderboardResultDTO, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
async execute(): Promise<Result<DriversLeaderboardOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
this.logger.debug('Executing GetDriversLeaderboardUseCase');
try {
const drivers = await this.driverRepository.findAll();
const rankings = this.rankingService.getAllDriverRankings();
const stats: DriversLeaderboardResultDTO['stats'] = {};
const avatarUrls: DriversLeaderboardResultDTO['avatarUrls'] = {};
const avatarUrls: Record<string, string> = {};
for (const driver of drivers) {
const driverStats = this.driverStatsService.getDriverStats(driver.id);
if (driverStats) {
stats[driver.id] = driverStats;
}
const avatarResult = await this.getDriverAvatar({ driverId: driver.id });
avatarUrls[driver.id] = avatarResult.avatarUrl;
}
const dto: DriversLeaderboardResultDTO = {
drivers,
rankings,
stats,
avatarUrls,
const driverItems: DriverLeaderboardItemOutputPort[] = drivers.map(driver => {
const ranking = rankings.find(r => r.driverId === driver.id);
const stats = this.driverStatsService.getDriverStats(driver.id);
return {
id: driver.id,
name: driver.name.value,
rating: ranking?.rating ?? 0,
skillLevel: 'Pro' as SkillLevel, // TODO: map from domain
nationality: driver.country.value,
racesCompleted: stats?.totalRaces ?? 0,
wins: stats?.wins ?? 0,
podiums: stats?.podiums ?? 0,
isActive: true, // TODO: determine from domain
rank: ranking?.overallRank ?? 0,
avatarUrl: avatarUrls[driver.id],
};
});
// 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 result: DriversLeaderboardOutputPort = {
drivers: driverItems.sort((a, b) => b.rating - a.rating),
totalRaces,
totalWins,
activeCount,
};
this.logger.debug('Successfully retrieved drivers leaderboard.');
return Result.ok(dto);
return Result.ok(result);
} catch (error) {
this.logger.error('Error executing GetDriversLeaderboardUseCase', error instanceof Error ? error : new Error(String(error)));
return Result.err({