refactor
This commit is contained in:
@@ -3,19 +3,23 @@ import type { ISeasonRepository } from '../../domain/repositories/ISeasonReposit
|
||||
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
||||
import type { LeagueScoringPresetProvider } from '../ports/LeagueScoringPresetProvider';
|
||||
import type {
|
||||
ILeagueScoringConfigPresenter,
|
||||
LeagueScoringConfigData,
|
||||
LeagueScoringConfigViewModel,
|
||||
} from '../presenters/ILeagueScoringConfigPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { LeagueScoringConfigData } from '../presenters/ILeagueScoringConfigPresenter';
|
||||
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
type GetLeagueScoringConfigErrorCode =
|
||||
| 'LEAGUE_NOT_FOUND'
|
||||
| 'NO_SEASONS'
|
||||
| 'NO_ACTIVE_SEASON'
|
||||
| 'NO_SCORING_CONFIG'
|
||||
| 'GAME_NOT_FOUND';
|
||||
|
||||
/**
|
||||
* Use Case for retrieving a league's scoring configuration for its active season.
|
||||
* Orchestrates domain logic and delegates presentation to the presenter.
|
||||
*/
|
||||
export class GetLeagueScoringConfigUseCase
|
||||
implements UseCase<{ leagueId: string }, LeagueScoringConfigData, LeagueScoringConfigViewModel, ILeagueScoringConfigPresenter>
|
||||
implements AsyncUseCase<{ leagueId: string }, LeagueScoringConfigData, GetLeagueScoringConfigErrorCode>
|
||||
{
|
||||
constructor(
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
@@ -25,40 +29,40 @@ export class GetLeagueScoringConfigUseCase
|
||||
private readonly presetProvider: LeagueScoringPresetProvider,
|
||||
) {}
|
||||
|
||||
async execute(params: { leagueId: string }, presenter: ILeagueScoringConfigPresenter): Promise<void> {
|
||||
async execute(params: { leagueId: string }): Promise<Result<LeagueScoringConfigData, ApplicationErrorCode<GetLeagueScoringConfigErrorCode>>> {
|
||||
const { leagueId } = params;
|
||||
|
||||
const league = await this.leagueRepository.findById(leagueId);
|
||||
if (!league) {
|
||||
throw new Error(`League ${leagueId} not found`);
|
||||
return Result.err({ code: 'LEAGUE_NOT_FOUND' });
|
||||
}
|
||||
|
||||
const seasons = await this.seasonRepository.findByLeagueId(leagueId);
|
||||
if (!seasons || seasons.length === 0) {
|
||||
throw new Error(`No seasons found for league ${leagueId}`);
|
||||
return Result.err({ code: 'NO_SEASONS' });
|
||||
}
|
||||
|
||||
|
||||
const activeSeason =
|
||||
seasons.find((s) => s.status === 'active') ?? seasons[0];
|
||||
|
||||
|
||||
if (!activeSeason) {
|
||||
throw new Error(`No active season could be determined for league ${leagueId}`);
|
||||
return Result.err({ code: 'NO_ACTIVE_SEASON' });
|
||||
}
|
||||
|
||||
|
||||
const scoringConfig =
|
||||
await this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id);
|
||||
if (!scoringConfig) {
|
||||
throw new Error(`No scoring config found for season ${activeSeason.id}`);
|
||||
return Result.err({ code: 'NO_SCORING_CONFIG' });
|
||||
}
|
||||
|
||||
const game = await this.gameRepository.findById(activeSeason.gameId);
|
||||
if (!game) {
|
||||
throw new Error(`Game ${activeSeason.gameId} not found`);
|
||||
return Result.err({ code: 'GAME_NOT_FOUND' });
|
||||
}
|
||||
|
||||
const presetId = scoringConfig.scoringPresetId;
|
||||
const preset = presetId ? this.presetProvider.getPresetById(presetId) : undefined;
|
||||
|
||||
|
||||
const data: LeagueScoringConfigData = {
|
||||
leagueId: league.id,
|
||||
seasonId: activeSeason.id,
|
||||
@@ -69,7 +73,6 @@ export class GetLeagueScoringConfigUseCase
|
||||
championships: scoringConfig.championships,
|
||||
};
|
||||
|
||||
presenter.reset();
|
||||
presenter.present(data);
|
||||
return Result.ok(data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user