refactor dtos to ports

This commit is contained in:
2025-12-19 15:07:53 +01:00
parent 499562c456
commit 8116fe888f
46 changed files with 718 additions and 266 deletions

View File

@@ -1,7 +1,8 @@
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { LeagueStatsViewModel } from '../presenters/ILeagueStatsPresenter';
import type { DriverRatingProvider } from '../ports/DriverRatingProvider';
import type { GetDriverRatingInputPort } from '../ports/input/GetDriverRatingInputPort';
import type { GetDriverRatingOutputPort } from '../ports/output/GetDriverRatingOutputPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
@@ -13,7 +14,7 @@ export class GetLeagueStatsUseCase {
constructor(
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
private readonly raceRepository: IRaceRepository,
private readonly driverRatingProvider: DriverRatingProvider,
private readonly getDriverRating: (input: GetDriverRatingInputPort) => Promise<GetDriverRatingOutputPort>,
) {}
async execute(params: GetLeagueStatsUseCaseParams): Promise<Result<LeagueStatsViewModel, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
@@ -21,9 +22,19 @@ export class GetLeagueStatsUseCase {
const memberships = await this.leagueMembershipRepository.getLeagueMembers(params.leagueId);
const races = await this.raceRepository.findByLeagueId(params.leagueId);
const driverIds = memberships.map(m => m.driverId);
const ratings = this.driverRatingProvider.getRatings(driverIds);
const validRatings = Array.from(ratings.values()).filter(r => r !== null) as number[];
// Get ratings for all drivers using clean ports
const ratingPromises = driverIds.map(driverId =>
this.getDriverRating({ driverId })
);
const ratingResults = await Promise.all(ratingPromises);
const validRatings = ratingResults
.map(result => result.rating)
.filter((rating): rating is number => rating !== null);
const averageRating = validRatings.length > 0 ? Math.round(validRatings.reduce((sum, r) => sum + r, 0) / validRatings.length) : 0;
const viewModel: LeagueStatsViewModel = {
totalMembers: memberships.length,
totalRaces: races.length,