refactor racing use cases
This commit is contained in:
@@ -1,93 +1,103 @@
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { GetLeagueProtestsOutputPort, ProtestOutputPort, RaceOutputPort, DriverOutputPort } from '../ports/output/GetLeagueProtestsOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Race } from '../../domain/entities/Race';
|
||||
import type { Protest } from '../../domain/entities/Protest';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
|
||||
export interface GetLeagueProtestsUseCaseParams {
|
||||
export interface GetLeagueProtestsInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export class GetLeagueProtestsUseCase implements AsyncUseCase<GetLeagueProtestsUseCaseParams, GetLeagueProtestsOutputPort, 'NO_ERROR'> {
|
||||
export type GetLeagueProtestsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export interface LeagueProtestWithEntities {
|
||||
protest: Protest;
|
||||
race: Race | null;
|
||||
protestingDriver: Driver | null;
|
||||
accusedDriver: Driver | null;
|
||||
}
|
||||
|
||||
export interface GetLeagueProtestsResult {
|
||||
league: League;
|
||||
protests: LeagueProtestWithEntities[];
|
||||
}
|
||||
|
||||
export class GetLeagueProtestsUseCase {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly protestRepository: IProtestRepository,
|
||||
private readonly driverRepository: IDriverRepository,
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly output: UseCaseOutputPort<GetLeagueProtestsResult>,
|
||||
) {}
|
||||
|
||||
async execute(params: GetLeagueProtestsUseCaseParams): Promise<Result<GetLeagueProtestsOutputPort, ApplicationErrorCode<'NO_ERROR'>>> {
|
||||
const races = await this.raceRepository.findByLeagueId(params.leagueId);
|
||||
const protests: ProtestOutputPort[] = [];
|
||||
const racesById: Record<string, RaceOutputPort> = {};
|
||||
const driversById: Record<string, DriverOutputPort> = {};
|
||||
const driverIds = new Set<string>();
|
||||
async execute(
|
||||
input: GetLeagueProtestsInput,
|
||||
): Promise<Result<void, ApplicationErrorCode<GetLeagueProtestsErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const league = await this.leagueRepository.findById(input.leagueId);
|
||||
|
||||
for (const race of races) {
|
||||
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({
|
||||
id: protest.id,
|
||||
raceId: protest.raceId,
|
||||
protestingDriverId: protest.protestingDriverId,
|
||||
accusedDriverId: protest.accusedDriverId,
|
||||
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,
|
||||
if (!league) {
|
||||
return Result.err({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found' },
|
||||
});
|
||||
driverIds.add(protest.protestingDriverId);
|
||||
driverIds.add(protest.accusedDriverId);
|
||||
}
|
||||
}
|
||||
|
||||
for (const driverId of driverIds) {
|
||||
const driver = await this.driverRepository.findById(driverId);
|
||||
if (driver) {
|
||||
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(),
|
||||
};
|
||||
const races = await this.raceRepository.findByLeagueId(input.leagueId);
|
||||
const protests: Protest[] = [];
|
||||
const racesById: Record<string, Race> = {};
|
||||
const driversById: Record<string, Driver> = {};
|
||||
const driverIds = new Set<string>();
|
||||
|
||||
for (const race of races) {
|
||||
racesById[race.id] = race;
|
||||
const raceProtests = await this.protestRepository.findByRaceId(race.id);
|
||||
for (const protest of raceProtests) {
|
||||
protests.push(protest);
|
||||
driverIds.add(protest.protestingDriverId);
|
||||
driverIds.add(protest.accusedDriverId);
|
||||
}
|
||||
}
|
||||
|
||||
for (const driverId of driverIds) {
|
||||
const driver = await this.driverRepository.findById(driverId);
|
||||
if (driver) {
|
||||
driversById[driver.id] = driver;
|
||||
}
|
||||
}
|
||||
|
||||
const protestsWithEntities: LeagueProtestWithEntities[] = protests.map(protest => ({
|
||||
protest,
|
||||
race: racesById[protest.raceId] ?? null,
|
||||
protestingDriver: driversById[protest.protestingDriverId] ?? null,
|
||||
accusedDriver: driversById[protest.accusedDriverId] ?? null,
|
||||
}));
|
||||
|
||||
const result: GetLeagueProtestsResult = {
|
||||
league,
|
||||
protests: protestsWithEntities,
|
||||
};
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
const message =
|
||||
error && typeof error === 'object' && 'message' in error && typeof (error as any).message === 'string'
|
||||
? (error as any).message
|
||||
: 'Failed to load league protests';
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
return Result.ok({
|
||||
protests,
|
||||
racesById,
|
||||
driversById,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user