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

@@ -6,9 +6,10 @@ import type { ISocialGraphRepository } from '@core/social/domain/repositories/IS
import type { DriverExtendedProfileProvider } from '../ports/DriverExtendedProfileProvider';
import type { Driver } from '../../domain/entities/Driver';
import type { Team } from '../../domain/entities/Team';
import type { ProfileOverviewOutputPort } from '../ports/output/ProfileOverviewOutputPort';
import type { TeamMembership } from '../../domain/types/TeamMembership';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application';
interface ProfileDriverStatsAdapter {
rating: number | null;
@@ -30,10 +31,67 @@ interface DriverRankingEntry {
overallRank: number | null;
}
export interface GetProfileOverviewParams {
export type GetProfileOverviewInput = {
driverId: string;
};
export interface ProfileOverviewStats {
totalRaces: number;
wins: number;
podiums: number;
dnfs: number;
avgFinish: number | null;
bestFinish: number | null;
worstFinish: number | null;
finishRate: number | null;
winRate: number | null;
podiumRate: number | null;
percentile: number | null;
rating: number | null;
consistency: number | null;
overallRank: number | null;
}
export interface ProfileOverviewFinishDistribution {
totalRaces: number;
wins: number;
podiums: number;
topTen: number;
dnfs: number;
other: number;
}
export interface ProfileOverviewTeamMembership {
team: Team;
membership: TeamMembership;
}
export interface ProfileOverviewSocialSummary {
friendsCount: number;
friends: Driver[];
}
export interface ProfileOverviewDriverInfo {
driver: Driver;
totalDrivers: number;
globalRank: number | null;
consistency: number | null;
rating: number | null;
}
export type GetProfileOverviewResult = {
driverInfo: ProfileOverviewDriverInfo;
stats: ProfileOverviewStats | null;
finishDistribution: ProfileOverviewFinishDistribution | null;
teamMemberships: ProfileOverviewTeamMembership[];
socialSummary: ProfileOverviewSocialSummary;
extendedProfile: unknown;
};
export type GetProfileOverviewErrorCode =
| 'DRIVER_NOT_FOUND'
| 'REPOSITORY_ERROR';
export class GetProfileOverviewUseCase {
constructor(
private readonly driverRepository: IDriverRepository,
@@ -44,16 +102,24 @@ export class GetProfileOverviewUseCase {
private readonly driverExtendedProfileProvider: DriverExtendedProfileProvider,
private readonly getDriverStats: (driverId: string) => ProfileDriverStatsAdapter | null,
private readonly getAllDriverRankings: () => DriverRankingEntry[],
private readonly output: UseCaseOutputPort<GetProfileOverviewResult>,
) {}
async execute(params: GetProfileOverviewParams): Promise<Result<ProfileOverviewOutputPort, ApplicationErrorCode<'DRIVER_NOT_FOUND' | 'REPOSITORY_ERROR'>>> {
async execute(
input: GetProfileOverviewInput,
): Promise<
Result<void, ApplicationErrorCode<GetProfileOverviewErrorCode, { message: string }>>
> {
try {
const { driverId } = params;
const { driverId } = input;
const driver = await this.driverRepository.findById(driverId);
if (!driver) {
return Result.err({ code: 'DRIVER_NOT_FOUND', message: 'Driver not found' });
return Result.err({
code: 'DRIVER_NOT_FOUND',
details: { message: 'Driver not found' },
});
}
const [statsAdapter, teams, friends] = await Promise.all([
@@ -62,15 +128,15 @@ export class GetProfileOverviewUseCase {
this.socialRepository.getFriends(driverId),
]);
const driverSummary = this.buildDriverSummary(driver, statsAdapter);
const driverInfo = this.buildDriverInfo(driver, statsAdapter);
const stats = this.buildStats(statsAdapter);
const finishDistribution = this.buildFinishDistribution(statsAdapter);
const teamMemberships = await this.buildTeamMemberships(driver.id, teams as Team[]);
const socialSummary = this.buildSocialSummary(friends as Driver[]);
const extendedProfile = this.driverExtendedProfileProvider.getExtendedProfile(driverId);
const outputPort: ProfileOverviewOutputPort = {
driver: driverSummary,
const result: GetProfileOverviewResult = {
driverInfo,
stats,
finishDistribution,
teamMemberships,
@@ -78,35 +144,36 @@ export class GetProfileOverviewUseCase {
extendedProfile,
};
return Result.ok(outputPort);
} catch {
return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch profile overview' });
this.output.present(result);
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message:
error instanceof Error
? error.message
: 'Failed to load profile overview',
},
});
}
}
private buildDriverSummary(
private buildDriverInfo(
driver: Driver,
stats: ProfileDriverStatsAdapter | null,
): ProfileOverviewOutputPort['driver'] {
): ProfileOverviewDriverInfo {
const rankings = this.getAllDriverRankings();
const fallbackRank = this.computeFallbackRank(driver.id, rankings);
const totalDrivers = rankings.length;
return {
id: driver.id,
name: driver.name.value,
country: driver.country.value,
avatarUrl: this.imageService.getDriverAvatar(driver.id),
iracingId: driver.iracingId?.value ?? null,
joinedAt:
driver.joinedAt instanceof Date
? driver.joinedAt
: new Date(driver.joinedAt.value),
rating: stats?.rating ?? null,
driver,
totalDrivers,
globalRank: stats?.overallRank ?? fallbackRank,
consistency: stats?.consistency ?? null,
bio: driver.bio?.value ?? null,
totalDrivers,
rating: stats?.rating ?? null,
};
}
@@ -123,7 +190,7 @@ export class GetProfileOverviewUseCase {
private buildStats(
stats: ProfileDriverStatsAdapter | null,
): ProfileOverviewStatsViewModel | null {
): ProfileOverviewStats | null {
if (!stats) {
return null;
}
@@ -132,12 +199,9 @@ export class GetProfileOverviewUseCase {
const dnfs = stats.dnfs;
const finishedRaces = Math.max(totalRaces - dnfs, 0);
const finishRate =
totalRaces > 0 ? (finishedRaces / totalRaces) * 100 : null;
const winRate =
totalRaces > 0 ? (stats.wins / totalRaces) * 100 : null;
const podiumRate =
totalRaces > 0 ? (stats.podiums / totalRaces) * 100 : null;
const finishRate = totalRaces > 0 ? (finishedRaces / totalRaces) * 100 : null;
const winRate = totalRaces > 0 ? (stats.wins / totalRaces) * 100 : null;
const podiumRate = totalRaces > 0 ? (stats.podiums / totalRaces) * 100 : null;
return {
totalRaces,
@@ -159,7 +223,7 @@ export class GetProfileOverviewUseCase {
private buildFinishDistribution(
stats: ProfileDriverStatsAdapter | null,
): ProfileOverviewFinishDistributionViewModel | null {
): ProfileOverviewFinishDistribution | null {
if (!stats || stats.totalRaces <= 0) {
return null;
}
@@ -189,8 +253,8 @@ export class GetProfileOverviewUseCase {
private async buildTeamMemberships(
driverId: string,
teams: Team[],
): Promise<ProfileOverviewOutputPort['teamMemberships']> {
const memberships: ProfileOverviewOutputPort['teamMemberships'] = [];
): Promise<ProfileOverviewTeamMembership[]> {
const memberships: ProfileOverviewTeamMembership[] = [];
for (const team of teams) {
const membership = await this.teamMembershipRepository.getMembership(
@@ -200,33 +264,22 @@ export class GetProfileOverviewUseCase {
if (!membership) continue;
memberships.push({
teamId: team.id,
teamName: team.name.value,
teamTag: team.tag?.value ?? null,
role: membership.role,
joinedAt:
membership.joinedAt instanceof Date
? membership.joinedAt
: new Date(membership.joinedAt),
isCurrent: membership.status === 'active',
team,
membership,
});
}
memberships.sort((a, b) => a.joinedAt.getTime() - b.joinedAt.getTime());
memberships.sort(
(a, b) => a.membership.joinedAt.getTime() - b.membership.joinedAt.getTime(),
);
return memberships;
}
private buildSocialSummary(friends: Driver[]): ProfileOverviewOutputPort['socialSummary'] {
private buildSocialSummary(friends: Driver[]): ProfileOverviewSocialSummary {
return {
friendsCount: friends.length,
friends: friends.map(friend => ({
id: friend.id,
name: friend.name.value,
country: friend.country.value,
avatarUrl: this.imageService.getDriverAvatar(friend.id),
})),
friends,
};
}
}
}