import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository'; import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository'; import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository'; import type { IGameRepository } from '../../domain/repositories/IGameRepository'; import type { ILeagueFullConfigPresenter, LeagueFullConfigData, LeagueConfigFormViewModel, } from '../presenters/ILeagueFullConfigPresenter'; import type { UseCase } from '@gridpilot/shared/application/UseCase'; import { EntityNotFoundError } from '../errors/RacingApplicationError'; /** * Use Case for retrieving a league's full configuration. * Orchestrates domain logic and delegates presentation to the presenter. */ export class GetLeagueFullConfigUseCase implements UseCase<{ leagueId: string }, LeagueFullConfigData, LeagueConfigFormViewModel, ILeagueFullConfigPresenter> { constructor( private readonly leagueRepository: ILeagueRepository, private readonly seasonRepository: ISeasonRepository, private readonly leagueScoringConfigRepository: ILeagueScoringConfigRepository, private readonly gameRepository: IGameRepository, ) {} async execute(params: { leagueId: string }, presenter: ILeagueFullConfigPresenter): Promise { const { leagueId } = params; const league = await this.leagueRepository.findById(leagueId); if (!league) { throw new EntityNotFoundError({ entity: 'league', id: leagueId }); } const seasons = await this.seasonRepository.findByLeagueId(leagueId); const activeSeason = seasons && seasons.length > 0 ? seasons.find((s) => s.status === 'active') ?? seasons[0] : undefined; let scoringConfig = await (async () => { if (!activeSeason) return undefined; return this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id); })(); let game = await (async () => { if (!activeSeason || !activeSeason.gameId) return undefined; return this.gameRepository.findById(activeSeason.gameId); })(); const data: LeagueFullConfigData = { league, ...(activeSeason ? { activeSeason } : {}), ...(scoringConfig ? { scoringConfig } : {}), ...(game ? { game } : {}), }; presenter.reset(); presenter.present(data); } }