/** * Use Case: GetRaceProtestsUseCase * * Returns all protests filed for a specific race, with driver details. * Orchestrates domain logic and delegates presentation to the presenter. */ import type { IProtestRepository } from '../../domain/repositories/IProtestRepository'; import type { IDriverRepository } from '../../domain/repositories/IDriverRepository'; import type { IRaceProtestsPresenter } from '../presenters/IRaceProtestsPresenter'; export class GetRaceProtestsUseCase { constructor( private readonly protestRepository: IProtestRepository, private readonly driverRepository: IDriverRepository, public readonly presenter: IRaceProtestsPresenter, ) {} async execute(raceId: string): Promise { const protests = await this.protestRepository.findByRaceId(raceId); // Load all driver details in parallel const driverIds = new Set(); protests.forEach(protest => { driverIds.add(protest.protestingDriverId); driverIds.add(protest.accusedDriverId); if (protest.reviewedBy) { driverIds.add(protest.reviewedBy); } }); const drivers = await Promise.all( Array.from(driverIds).map(id => this.driverRepository.findById(id)) ); const driverMap = new Map(); drivers.forEach(driver => { if (driver) { driverMap.set(driver.id, driver.name); } }); this.presenter.present(protests, driverMap); } }