refactor racing use cases
This commit is contained in:
@@ -2,78 +2,129 @@ import type { ILeagueRepository } from '../../domain/repositories/ILeagueReposit
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
||||
import type { GetLeagueScoringPresetByIdInputPort } from '../ports/input/GetLeagueScoringPresetByIdInputPort';
|
||||
import type { LeagueScoringPresetOutputPort } from '../ports/output/LeagueScoringPresetOutputPort';
|
||||
import type { LeagueScoringConfigOutputPort } from '../ports/output/LeagueScoringConfigOutputPort';
|
||||
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
||||
import type { LeagueScoringPreset } from '../../../bootstrap/LeagueScoringPresets';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
import type { Season } from '../../domain/entities/season/Season';
|
||||
import type { LeagueScoringConfig } from '../../domain/entities/LeagueScoringConfig';
|
||||
import type { Game } from '../../domain/entities/Game';
|
||||
|
||||
type GetLeagueScoringConfigErrorCode =
|
||||
export type GetLeagueScoringConfigInput = {
|
||||
leagueId: string;
|
||||
};
|
||||
|
||||
export type GetLeagueScoringConfigResult = {
|
||||
league: League;
|
||||
season: Season;
|
||||
scoringConfig: LeagueScoringConfig;
|
||||
game: Game;
|
||||
preset?: LeagueScoringPreset;
|
||||
};
|
||||
|
||||
export type GetLeagueScoringConfigErrorCode =
|
||||
| 'LEAGUE_NOT_FOUND'
|
||||
| 'NO_SEASONS'
|
||||
| 'NO_ACTIVE_SEASON'
|
||||
| 'NO_SCORING_CONFIG'
|
||||
| 'GAME_NOT_FOUND';
|
||||
| 'GAME_NOT_FOUND'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
/**
|
||||
* Use Case for retrieving a league's scoring configuration for its active season.
|
||||
*/
|
||||
export class GetLeagueScoringConfigUseCase
|
||||
implements AsyncUseCase<{ leagueId: string }, LeagueScoringConfigOutputPort, GetLeagueScoringConfigErrorCode>
|
||||
{
|
||||
export class GetLeagueScoringConfigUseCase {
|
||||
constructor(
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly seasonRepository: ISeasonRepository,
|
||||
private readonly leagueScoringConfigRepository: ILeagueScoringConfigRepository,
|
||||
private readonly gameRepository: IGameRepository,
|
||||
private readonly getLeagueScoringPresetById: (input: GetLeagueScoringPresetByIdInputPort) => Promise<LeagueScoringPresetOutputPort | undefined>,
|
||||
private readonly presetProvider: {
|
||||
getPresetById(presetId: string): LeagueScoringPreset | undefined;
|
||||
},
|
||||
private readonly output: UseCaseOutputPort<GetLeagueScoringConfigResult>,
|
||||
) {}
|
||||
|
||||
async execute(params: { leagueId: string }): Promise<Result<LeagueScoringConfigOutputPort, ApplicationErrorCode<GetLeagueScoringConfigErrorCode>>> {
|
||||
async execute(
|
||||
params: GetLeagueScoringConfigInput,
|
||||
): Promise<
|
||||
Result<void, ApplicationErrorCode<GetLeagueScoringConfigErrorCode, { message: string }>>
|
||||
> {
|
||||
const { leagueId } = params;
|
||||
|
||||
const league = await this.leagueRepository.findById(leagueId);
|
||||
if (!league) {
|
||||
return Result.err({ code: 'LEAGUE_NOT_FOUND' });
|
||||
try {
|
||||
const league = await this.leagueRepository.findById(leagueId);
|
||||
if (!league) {
|
||||
return Result.err({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found' },
|
||||
});
|
||||
}
|
||||
|
||||
const seasons = await this.seasonRepository.findByLeagueId(leagueId);
|
||||
if (!seasons || seasons.length === 0) {
|
||||
return Result.err({
|
||||
code: 'NO_SEASONS',
|
||||
details: { message: 'No seasons found for league' },
|
||||
});
|
||||
}
|
||||
|
||||
const activeSeason =
|
||||
seasons.find((s) => s.status === 'active') ?? seasons[0];
|
||||
|
||||
if (!activeSeason) {
|
||||
return Result.err({
|
||||
code: 'NO_ACTIVE_SEASON',
|
||||
details: { message: 'No active season found for league' },
|
||||
});
|
||||
}
|
||||
|
||||
const scoringConfig =
|
||||
await this.leagueScoringConfigRepository.findBySeasonId(
|
||||
activeSeason.id,
|
||||
);
|
||||
if (!scoringConfig) {
|
||||
return Result.err({
|
||||
code: 'NO_SCORING_CONFIG',
|
||||
details: { message: 'Scoring configuration not found' },
|
||||
});
|
||||
}
|
||||
|
||||
const game = await this.gameRepository.findById(activeSeason.gameId);
|
||||
if (!game) {
|
||||
return Result.err({
|
||||
code: 'GAME_NOT_FOUND',
|
||||
details: { message: 'Game not found for season' },
|
||||
});
|
||||
}
|
||||
|
||||
const presetId = scoringConfig.scoringPresetId;
|
||||
const preset = presetId
|
||||
? this.presetProvider.getPresetById(presetId)
|
||||
: undefined;
|
||||
|
||||
const result: GetLeagueScoringConfigResult = {
|
||||
league,
|
||||
season: activeSeason,
|
||||
scoringConfig,
|
||||
game,
|
||||
...(preset !== undefined ? { preset } : {}),
|
||||
};
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'Failed to load league scoring config',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const seasons = await this.seasonRepository.findByLeagueId(leagueId);
|
||||
if (!seasons || seasons.length === 0) {
|
||||
return Result.err({ code: 'NO_SEASONS' });
|
||||
}
|
||||
|
||||
const activeSeason =
|
||||
seasons.find((s) => s.status === 'active') ?? seasons[0];
|
||||
|
||||
if (!activeSeason) {
|
||||
return Result.err({ code: 'NO_ACTIVE_SEASON' });
|
||||
}
|
||||
|
||||
const scoringConfig =
|
||||
await this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id);
|
||||
if (!scoringConfig) {
|
||||
return Result.err({ code: 'NO_SCORING_CONFIG' });
|
||||
}
|
||||
|
||||
const game = await this.gameRepository.findById(activeSeason.gameId);
|
||||
if (!game) {
|
||||
return Result.err({ code: 'GAME_NOT_FOUND' });
|
||||
}
|
||||
|
||||
const presetId = scoringConfig.scoringPresetId;
|
||||
const preset = presetId ? await this.getLeagueScoringPresetById({ presetId }) : undefined;
|
||||
|
||||
const output: LeagueScoringConfigOutputPort = {
|
||||
leagueId: league.id,
|
||||
seasonId: activeSeason.id,
|
||||
gameId: game.id,
|
||||
gameName: game.name,
|
||||
...(presetId !== undefined ? { scoringPresetId: presetId } : {}),
|
||||
...(preset !== undefined ? { preset } : {}),
|
||||
championships: scoringConfig.championships,
|
||||
};
|
||||
|
||||
return Result.ok(output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user