refactor core presenters
This commit is contained in:
@@ -4,17 +4,7 @@ import type { IDriverRepository } from '../../domain/repositories/IDriverReposit
|
||||
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { GetDriverRatingInputPort } from '../ports/input/GetDriverRatingInputPort';
|
||||
import type { GetDriverRatingOutputPort } from '../ports/output/GetDriverRatingOutputPort';
|
||||
import type { GetDriverAvatarInputPort } from '../ports/input/GetDriverAvatarInputPort';
|
||||
import type { GetDriverAvatarOutputPort } from '../ports/output/GetDriverAvatarOutputPort';
|
||||
import type {
|
||||
RaceDetailViewModel,
|
||||
RaceDetailRaceViewModel,
|
||||
RaceDetailLeagueViewModel,
|
||||
RaceDetailEntryViewModel,
|
||||
RaceDetailUserResultViewModel,
|
||||
} from '../presenters/IRaceDetailPresenter';
|
||||
import type { RaceDetailOutputPort } from '../ports/output/RaceDetailOutputPort';
|
||||
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
@@ -37,7 +27,7 @@ export interface GetRaceDetailQueryParams {
|
||||
type GetRaceDetailErrorCode = 'RACE_NOT_FOUND';
|
||||
|
||||
export class GetRaceDetailUseCase
|
||||
implements AsyncUseCase<GetRaceDetailQueryParams, RaceDetailViewModel, GetRaceDetailErrorCode>
|
||||
implements AsyncUseCase<GetRaceDetailQueryParams, RaceDetailOutputPort, GetRaceDetailErrorCode>
|
||||
{
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
@@ -46,11 +36,9 @@ export class GetRaceDetailUseCase
|
||||
private readonly raceRegistrationRepository: IRaceRegistrationRepository,
|
||||
private readonly resultRepository: IResultRepository,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly getDriverRating: (input: GetDriverRatingInputPort) => Promise<GetDriverRatingOutputPort>,
|
||||
private readonly getDriverAvatar: (input: GetDriverAvatarInputPort) => Promise<GetDriverAvatarOutputPort>,
|
||||
) {}
|
||||
|
||||
async execute(params: GetRaceDetailQueryParams): Promise<Result<RaceDetailViewModel, ApplicationErrorCode<GetRaceDetailErrorCode>>> {
|
||||
async execute(params: GetRaceDetailQueryParams): Promise<Result<RaceDetailOutputPort, ApplicationErrorCode<GetRaceDetailErrorCode>>> {
|
||||
const { raceId, driverId } = params;
|
||||
|
||||
const race = await this.raceRepository.findById(raceId);
|
||||
@@ -58,105 +46,40 @@ export class GetRaceDetailUseCase
|
||||
return Result.err({ code: 'RACE_NOT_FOUND' });
|
||||
}
|
||||
|
||||
const [league, registeredDriverIds, membership] = await Promise.all([
|
||||
const [league, registrations, membership] = await Promise.all([
|
||||
this.leagueRepository.findById(race.leagueId),
|
||||
this.raceRegistrationRepository.getRegisteredDrivers(race.id),
|
||||
this.raceRegistrationRepository.findByRaceId(race.id),
|
||||
this.leagueMembershipRepository.getMembership(race.leagueId, driverId),
|
||||
]);
|
||||
|
||||
const drivers = await Promise.all(
|
||||
registeredDriverIds.map(id => this.driverRepository.findById(id)),
|
||||
registrations.map(registration => this.driverRepository.findById(registration.driverId.toString())),
|
||||
);
|
||||
|
||||
const entryList: RaceDetailEntryViewModel[] = [];
|
||||
for (const driver of drivers) {
|
||||
if (driver) {
|
||||
const ratingResult = await this.getDriverRating({ driverId: driver.id });
|
||||
const avatarResult = await this.getDriverAvatar({ driverId: driver.id });
|
||||
|
||||
entryList.push({
|
||||
id: driver.id,
|
||||
name: driver.name,
|
||||
country: driver.country,
|
||||
avatarUrl: avatarResult.avatarUrl,
|
||||
rating: ratingResult.rating,
|
||||
isCurrentUser: driver.id === driverId,
|
||||
});
|
||||
}
|
||||
}
|
||||
const validDrivers = drivers.filter((driver): driver is NonNullable<typeof driver> => driver !== null);
|
||||
|
||||
const isUserRegistered = registeredDriverIds.includes(driverId);
|
||||
const isUserRegistered = registrations.some(reg => reg.driverId.toString() === driverId);
|
||||
const isUpcoming = race.status === 'scheduled' && race.scheduledAt > new Date();
|
||||
const canRegister = !!membership && membership.status === 'active' && isUpcoming;
|
||||
|
||||
let userResultView: RaceDetailUserResultViewModel | null = null;
|
||||
let userResult: Result | null = null;
|
||||
|
||||
if (race.status === 'completed') {
|
||||
const results = await this.resultRepository.findByRaceId(race.id);
|
||||
const userResult = results.find(r => r.driverId === driverId) ?? null;
|
||||
|
||||
if (userResult) {
|
||||
const ratingChange = this.calculateRatingChange(userResult.position);
|
||||
|
||||
userResultView = {
|
||||
position: userResult.position,
|
||||
startPosition: userResult.startPosition,
|
||||
incidents: userResult.incidents,
|
||||
fastestLap: userResult.fastestLap,
|
||||
positionChange: userResult.getPositionChange(),
|
||||
isPodium: userResult.isPodium(),
|
||||
isClean: userResult.isClean(),
|
||||
ratingChange,
|
||||
};
|
||||
}
|
||||
userResult = results.find(r => r.driverId.toString() === driverId) ?? null;
|
||||
}
|
||||
|
||||
const raceView: RaceDetailRaceViewModel = {
|
||||
id: race.id,
|
||||
leagueId: race.leagueId,
|
||||
track: race.track,
|
||||
car: race.car,
|
||||
scheduledAt: race.scheduledAt.toISOString(),
|
||||
sessionType: race.sessionType,
|
||||
status: race.status,
|
||||
strengthOfField: race.strengthOfField ?? null,
|
||||
...(race.registeredCount !== undefined ? { registeredCount: race.registeredCount } : {}),
|
||||
...(race.maxParticipants !== undefined ? { maxParticipants: race.maxParticipants } : {}),
|
||||
const outputPort: RaceDetailOutputPort = {
|
||||
race,
|
||||
league,
|
||||
registrations,
|
||||
drivers: validDrivers,
|
||||
userResult,
|
||||
isUserRegistered,
|
||||
canRegister,
|
||||
};
|
||||
|
||||
const leagueView: RaceDetailLeagueViewModel | null = league
|
||||
? {
|
||||
id: league.id,
|
||||
name: league.name,
|
||||
description: league.description,
|
||||
settings: {
|
||||
...(league.settings.maxDrivers !== undefined
|
||||
? { maxDrivers: league.settings.maxDrivers }
|
||||
: {}),
|
||||
...(league.settings.qualifyingFormat !== undefined
|
||||
? { qualifyingFormat: league.settings.qualifyingFormat }
|
||||
: {}),
|
||||
},
|
||||
}
|
||||
: null;
|
||||
|
||||
const viewModel: RaceDetailViewModel = {
|
||||
race: raceView,
|
||||
league: leagueView,
|
||||
entryList,
|
||||
registration: {
|
||||
isUserRegistered,
|
||||
canRegister,
|
||||
},
|
||||
userResult: userResultView,
|
||||
};
|
||||
|
||||
return Result.ok(viewModel);
|
||||
return Result.ok(outputPort);
|
||||
}
|
||||
|
||||
private calculateRatingChange(position: number): number {
|
||||
const baseChange = position <= 3 ? 25 : position <= 10 ? 10 : -5;
|
||||
const positionBonus = Math.max(0, (20 - position) * 2);
|
||||
return baseChange + positionBonus;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user