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

@@ -4,27 +4,41 @@ import type { IDriverRepository } from '../../domain/repositories/IDriverReposit
import type { AsyncUseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { GetLeagueProtestsResultDTO, ProtestDTO } from '../dto/GetLeagueProtestsResultDTO';
import type { GetLeagueProtestsOutputPort, ProtestOutputPort, RaceOutputPort, DriverOutputPort } from '../ports/output/GetLeagueProtestsOutputPort';
export interface GetLeagueProtestsUseCaseParams {
leagueId: string;
}
export class GetLeagueProtestsUseCase implements AsyncUseCase<GetLeagueProtestsUseCaseParams, GetLeagueProtestsResultDTO, 'NO_ERROR'> {
export class GetLeagueProtestsUseCase implements AsyncUseCase<GetLeagueProtestsUseCaseParams, GetLeagueProtestsOutputPort, 'NO_ERROR'> {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly protestRepository: IProtestRepository,
private readonly driverRepository: IDriverRepository,
) {}
async execute(params: GetLeagueProtestsUseCaseParams): Promise<Result<GetLeagueProtestsResultDTO, ApplicationErrorCode<'NO_ERROR'>>> {
async execute(params: GetLeagueProtestsUseCaseParams): Promise<Result<GetLeagueProtestsOutputPort, ApplicationErrorCode<'NO_ERROR'>>> {
const races = await this.raceRepository.findByLeagueId(params.leagueId);
const protests: ProtestDTO[] = [];
const raceMap = new Map();
const protests: ProtestOutputPort[] = [];
const racesById: Record<string, RaceOutputPort> = {};
const driversById: Record<string, DriverOutputPort> = {};
const driverIds = new Set<string>();
for (const race of races) {
raceMap.set(race.id, { id: race.id, name: race.track, date: race.scheduledAt.toISOString() });
racesById[race.id] = {
id: race.id,
leagueId: race.leagueId,
scheduledAt: race.scheduledAt.toISOString(),
track: race.track,
trackId: race.trackId,
car: race.car,
carId: race.carId,
sessionType: race.sessionType.toString(),
status: race.status,
strengthOfField: race.strengthOfField,
registeredCount: race.registeredCount,
maxParticipants: race.maxParticipants,
};
const raceProtests = await this.protestRepository.findByRaceId(race.id);
for (const protest of raceProtests) {
protests.push({
@@ -32,26 +46,48 @@ export class GetLeagueProtestsUseCase implements AsyncUseCase<GetLeagueProtestsU
raceId: protest.raceId,
protestingDriverId: protest.protestingDriverId,
accusedDriverId: protest.accusedDriverId,
submittedAt: protest.filedAt,
description: protest.comment || '',
status: protest.status,
incident: {
lap: protest.incident.lap,
description: protest.incident.description,
timeInRace: protest.incident.timeInRace,
},
comment: protest.comment,
proofVideoUrl: protest.proofVideoUrl,
status: protest.status.toString(),
reviewedBy: protest.reviewedBy,
decisionNotes: protest.decisionNotes,
filedAt: protest.filedAt.toISOString(),
reviewedAt: protest.reviewedAt?.toISOString(),
defense: protest.defense ? {
statement: protest.defense.statement.toString(),
videoUrl: protest.defense.videoUrl?.toString(),
submittedAt: protest.defense.submittedAt.toDate().toISOString(),
} : undefined,
defenseRequestedAt: protest.defenseRequestedAt?.toISOString(),
defenseRequestedBy: protest.defenseRequestedBy,
});
driverIds.add(protest.protestingDriverId);
driverIds.add(protest.accusedDriverId);
}
}
const drivers: { id: string; name: string }[] = [];
for (const driverId of driverIds) {
const driver = await this.driverRepository.findById(driverId);
if (driver) {
drivers.push({ id: driver.id, name: driver.name });
driversById[driver.id] = {
id: driver.id,
iracingId: driver.iracingId.toString(),
name: driver.name.toString(),
country: driver.country.toString(),
bio: driver.bio?.toString(),
joinedAt: driver.joinedAt.toDate().toISOString(),
};
}
}
return Result.ok({
protests,
races: Array.from(raceMap.values()),
drivers,
racesById,
driversById,
});
}
}