Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueProtestsUseCase.ts
2025-12-16 13:53:23 +01:00

58 lines
2.4 KiB
TypeScript

import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IGetLeagueProtestsPresenter, GetLeagueProtestsResultDTO, GetLeagueProtestsViewModel } from '../presenters/IGetLeagueProtestsPresenter';
import type { UseCase } from '@core/shared/application/UseCase';
export interface GetLeagueProtestsUseCaseParams {
leagueId: string;
}
export interface GetLeagueProtestsResultDTO {
protests: unknown[];
races: unknown[];
drivers: { id: string; name: string }[];
}
export class GetLeagueProtestsUseCase implements UseCase<GetLeagueProtestsUseCaseParams, GetLeagueProtestsResultDTO, GetLeagueProtestsViewModel, IGetLeagueProtestsPresenter> {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly protestRepository: IProtestRepository,
private readonly driverRepository: IDriverRepository,
) {}
async execute(params: GetLeagueProtestsUseCaseParams, presenter: IGetLeagueProtestsPresenter): Promise<void> {
const races = await this.raceRepository.findByLeagueId(params.leagueId);
const protests = [];
const raceMap = new Map();
const driverIds = new Set<string>();
for (const race of races) {
raceMap.set(race.id, { id: race.id, name: race.name, date: race.scheduledAt.toISOString() });
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,
submittedAt: protest.filedAt,
description: protest.comment || '',
status: protest.status,
});
driverIds.add(protest.protestingDriverId);
driverIds.add(protest.accusedDriverId);
}
}
const drivers = await this.driverRepository.findByIds(Array.from(driverIds));
const driverMap = new Map(drivers.map(d => [d.id, { id: d.id, name: d.name }]));
const dto: GetLeagueProtestsResultDTO = {
protests,
races: Array.from(raceMap.values()),
drivers: Array.from(driverMap.values()),
};
presenter.reset();
presenter.present(dto);
}
}