refactor racing use cases
This commit is contained in:
@@ -1,48 +1,78 @@
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { LeagueStatsOutputPort } from '../ports/output/LeagueStatsOutputPort';
|
||||
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';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
export interface GetLeagueStatsUseCaseParams {
|
||||
export interface GetLeagueStatsInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export interface GetLeagueStatsResult {
|
||||
leagueId: string;
|
||||
driverCount: number;
|
||||
raceCount: number;
|
||||
averageRating: number;
|
||||
}
|
||||
|
||||
export type GetLeagueStatsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetLeagueStatsUseCase {
|
||||
constructor(
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly getDriverRating: (input: GetDriverRatingInputPort) => Promise<GetDriverRatingOutputPort>,
|
||||
private readonly getDriverRating: (input: {
|
||||
driverId: string;
|
||||
}) => Promise<{ rating: number | null; ratingChange: number | null }>,
|
||||
private readonly output: UseCaseOutputPort<GetLeagueStatsResult>,
|
||||
) {}
|
||||
|
||||
async execute(params: GetLeagueStatsUseCaseParams): Promise<Result<LeagueStatsOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
|
||||
async execute(
|
||||
input: GetLeagueStatsInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetLeagueStatsErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const memberships = await this.leagueMembershipRepository.getLeagueMembers(params.leagueId);
|
||||
const races = await this.raceRepository.findByLeagueId(params.leagueId);
|
||||
const driverIds = memberships.map(m => m.driverId);
|
||||
|
||||
// Get ratings for all drivers using clean ports
|
||||
const ratingPromises = driverIds.map(driverId =>
|
||||
this.getDriverRating({ driverId })
|
||||
);
|
||||
|
||||
const memberships = await this.leagueMembershipRepository.getLeagueMembers(input.leagueId);
|
||||
|
||||
if (memberships.length === 0) {
|
||||
return Result.err({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found' },
|
||||
});
|
||||
}
|
||||
|
||||
const races = await this.raceRepository.findByLeagueId(input.leagueId);
|
||||
const driverIds = memberships.map(membership => String(membership.driverId));
|
||||
|
||||
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: LeagueStatsOutputPort = {
|
||||
totalMembers: memberships.length,
|
||||
totalRaces: races.length,
|
||||
|
||||
const averageRating =
|
||||
validRatings.length > 0
|
||||
? Math.round(validRatings.reduce((sum, rating) => sum + rating, 0) / validRatings.length)
|
||||
: 0;
|
||||
|
||||
const result: GetLeagueStatsResult = {
|
||||
leagueId: input.leagueId,
|
||||
driverCount: memberships.length,
|
||||
raceCount: races.length,
|
||||
averageRating,
|
||||
};
|
||||
return Result.ok(viewModel);
|
||||
} catch {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch league stats' });
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message ? error.message : 'Failed to fetch league stats';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user